[go: up one dir, main page]

0% found this document useful (0 votes)
51 views6 pages

សេចក្ដីណែនាំអំពី Array

Uploaded by

lysarith09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views6 pages

សេចក្ដីណែនាំអំពី Array

Uploaded by

lysarith09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

សេចក្ដីណែន ាំអាំពី Array

- បណដ ុំ្ នៃទៃ


ិ នៃយ
័ ។
- ទិៃនៃ័យទុំងន ោះ ដាច់ខាតត្តូវមាៃត្បនេទនៃទិៃនៃ័យដូចៗគ្នន។
- ទៃ
ិ នៃ័យទុំងន ោះ ដាច់ខាតត្តូវតតខដសៗគ្នននដាយ Index(វ ិមាត្ត)។ Index ត្តូវរាប់ចាប់ពី 0,1,2,…។
១. Array មួយវ ិមាត្ត(One Dimensional Array)
ទុំរង់ទូនៅ៖
data-type array-name[size] = {value1, value2, … , valuen);
នេអាចសរនសរថា៖
- array-name[0] = value1;
- array-name[1] = value2;
- ……………………………………………………………
- array-name[n-1] = valuen;
ចុំណ៖ុំ
- array-name: ន្មោះរបស់ Array
- Index(វ ិមាត្ត): 0, 1, 2,…, n-1
- ធាតដ, ទៃ
ិ នៃ័យ, តុំនៃរបស់ Array: value1, value2, …, valuen
Example 1: នេមាៃ Array អាយដ
int age[5] = {23, 21, 18, 19, 23};
នេអាចសរនសរ៖
- age[0] = 23;
- age[1] = 21;
- age[2] = 18;
- age[3] = 19;
- age[4] = 23;
C++ Code:
#include<iostream>
using namespace std;

int main(){
//One Dimensional Array
//Array age
int age[5] = {23, 21, 18, 19, 23};
cout << "------------------" << endl;
cout << "Age[0]: " << age[0] << endl;//Age[0]: 23
cout << "Age[1]: " << age[1] << endl;//Age[1]: 21
cout << "Age[2]: " << age[2] << endl;//Age[2]: 18
cout << "Age[3]: " << age[3] << endl;//Age[3]: 19
cout << "Age[4]: " << age[4] << endl;//Age[4]: 23
cout << "------------------" << endl;
return 0;
}
C++ Code: for loop
#include<iostream>
using namespace std;

int main(){
//One Dimensional Array - for loop
int x, size;
//Array age
int age[5] = {23, 21, 18, 19, 23};
size = sizeof(age)/sizeof(int);//5
cout << "------------------" << endl;
for(x=0; x<size; x++){
cout << "Age["<< x <<"]: " << age[x] << endl;
}
cout << "------------------" << endl;
return 0;
}

Output:

Example 2: នេមាៃ Array ន្មោះ


string fullname[4] = {
“Dara Veasna”,
“Sok Neary”,
“Sao Tola”,
“Pech Nita”
};

នេអាចសរនសរ៖
- fullname[0] = “Dara Veasna”;
- fullname[1] = “Sok Neary”;
- fullname[2] = “Sao Tola”;
- fullname[3] = “Pech Nita”;
C++ Code: foreach
#include<iostream>
using namespace std;

int main(){
//One Dimensional Array - foreach loop
//Array fullname
string fullname[4] = {
"Dara Veasna",
"Sok Neary",
"Sao Tola",
"Pech Nita"
};
//Practice: foreach loop
cout << "************************" << endl;
for(string singlename:fullname){
cout << singlename << endl;
}
cout << "************************" << endl;
return 0;
}
Output:

Example 3: បនងកីតកមមវ ិធីមួយតាមបុំរាប់ខាងនត្ោម៖


- បញ្ចូ ៃចុំៃួៃសស
ិ ស តាងនដាយអនេរ size
- បញ្ចូ ៃពិៃដ ស
ទ ិសសមានក់ៗ តាងនដាយ Array score
- រកៃទធផៃរបស់សស
ិ សមានក់ៗ តាងនដាយ Array result
- ៃកខខ័ណឌ៖ ត្បសិៃនបីពិៃដ ណមួ
ទ យតិចជាង ៥០ េឺធាាក់
C++ Code:
#include<iostream>
using namespace std;

int main(){
//One Dimensional Array - for loop
//Variable Declaration
int x, size;
int score[40];//Array: integer
string result[40];//Array: string
//Read Number of student
cout << "Read Number of Student:";
cin >> size;//5
for(x=0; x<size; x++){
cout << "--Read Score["<< x <<"]:";
cin >> score[x];
//Find Array result
//Conditional(Ternary) Operator
result[x] = (score[x]<50)?"Failed":"Pass";
cout << "**Result: " << result[x] << endl;
cout << "-------------------------" << endl;
}
return 0;
}
Output:

Example 4: បនងកីតកមមវ ិធីមួយតាមបុំរាប់ខាងនត្ោម៖


- បញ្ចូ ៃចុំៃួៃទុំៃិញ តាងនដាយអនេរ size
- បញ្ចូ ៃតុំនៃទុំៃិញៃីមួយៗ តាងនដាយ Array price
- រក៖
o ត្ាក់បញ្ចដ ោះតុំនៃៃីមួយៗ តាងនដាយ Array discount
o ត្ាក់ចុំណយៃីមួយៗ តាងនដាយ Array paid
- ៃកខខ័ណឌ៖ ត្បសៃ
ិ នបត
ី ុំនៃទុំៃញ
ិ ណមួយនៃស
ី ៣០ ដដល្លារ ត្តូវបញ្ចដ ោះតុំនៃ ៥%
C++ Code:
#include<iostream>
using namespace std;

int main(){
//One Dimensional Array - for loop
//Variable Declaration
int x, size;
float price[10], discount[10], paid[10];//Array: Float
//Read Number of student
cout << "Read Number of Price:";
cin >> size;//5
for(x=0; x<size; x++){
cout << "--Read Price["<< x <<"]:";
cin >> price[x];
//Find Array discount
if(price[x] > 30){
discount[x] = price[x] * 0.05;
}else{
discount[x] = 0;
}
//Find Array paid
paid[x] = price[x] - discount[x];
//Print
cout << "**Discount["<< x <<"]: " << discount[x] << endl;
cout << "**Paid["<< x <<"]: " << paid[x] << endl;
cout << "-----------------------------" << endl;

}
return 0;
}
Output:

Example 5: បនងកីតកមមវ ិធីមួយតាមបុំរាប់ខាងនត្ោម៖


- បញ្ចូ ៃចុំៃួៃទុំៃញ
ិ តាងនដាយអនេរ size
- បញ្ចូ ៃតុំនៃទុំៃិញៃីមួយៗ តាងនដាយ Array price
- រក៖
o ត្ាក់បញ្ចដ ោះតុំនៃៃីមួយៗ តាងនដាយ Array discount
o ត្ាក់ចុំណយៃម
ី ួយៗ តាងនដាយ Array paid
o បញ្ចដ ោះតុំនៃសរដប តាងនដាយអនេរ grand_discount
o ត្ាក់ចុំណយសរដប តាងនដាយអនេរ grand_paid
- ៃកខខ័ណឌ៖ ត្បសិៃនបីតុំនៃទុំៃិញណមួយនៃីស ៣០ ដដល្លារ ត្តូវបញ្ចដ ោះតុំនៃ ៥%
C++ Code:
#include<iostream>
using namespace std;

int main(){
//One Dimensional Array - for loop
//Variable Declaration
int x, size, grand_discount, grand_paid;
float price[10], discount[10], paid[10];//Array: Float
grand_discount = 0;
grand_paid = 0;
//Read Number of student
cout << "Read Number of Price:";
cin >> size;//3
for(x=0; x<size; x++){
cout << "--Read Price["<< x <<"]:";
cin >> price[x];
//Find Array discount
if(price[x] > 30){
discount[x] = price[x] * 0.05;
}else{
discount[x] = 0;
}
//Find Array paid
paid[x] = price[x] - discount[x];
//Find Grand Discount and Paid
grand_discount += discount[x];//0+0+2+4
grand_paid += paid[x];//0+24+38+76
//Print
cout << "**Discount["<< x <<"]: " << discount[x] << endl;
cout << "**Paid["<< x <<"]: " << paid[x] << endl;
cout << "-----------------------------" << endl;
}
cout << "**************************" << endl;
cout << "Grand Discount: " << grand_discount << endl;//6
cout << "Grand Paid: " << grand_paid << endl;//138
cout << "**************************" << endl;
return 0;
}
Output:

You might also like