Professional Documents
Culture Documents
ការប្រើប្រាស់ Array
ការប្រើប្រាស់ Array
. Array
Array
Array
Array
Array
Array
Array
Datatype
Array [ Array];
VariableName[size of array]
Array
Array
Array a, b, x, y
Array int a
int Array a
int
Array int b
b[0][0]
b[1][0]
b[2][0]
b[3][0]
b[0][1]
b[1][1]
b[2][1]
b[3][1]
b b[1][1]
int Array b
int
x[1],x[4] x[1]
float
Array float y
y[0][0]
y[1][0]
y[2][0]
y[0][1]
y[1][1]
y[2][1]
y[0][2]
y[1][2]
y[2][2]
. Array
Array
. Array
Ex1: Read
Array x Keyboard
#include<iostream>
#include<conio.h>
using namespace std;
float Add (float a, float b){
cout<<"Addition
== ";
return (a+b);
}
int sub (int a, int b){
cout<<"Subtraction == ";
return (a-b);
}
int mul (float a, float b){
cout<<"multiplication== ";
return (a*b);
}
float div(float a, float b){
cout<<"Divide
== ";
return (a/b);
}
int main (){
float arr[2];
cout<<"GROUP NAME : ARRAY"<<endl;
cout<<" DATE : Mornning"<<endl;
cout<<"Marjoring : COMPUTER SCIENCE "<<endl;
cout<<"\t\t\t_____________________________"<<endl;
cout<<"INPUT FIRST = "; cin>>arr[0];
cout<<"INPUT SECOUND= "; cin>>arr[1];
cout<<Add(arr[0], arr[1])<<endl;
cout<<sub(arr[0], arr[1])<<endl;
cout<<mul(arr[0], arr[1])<<endl;
cout<<div(arr[0], arr[1])<<endl;
getch();
return 0;
. Array
Array Create Value Declaration :
int x[3] = { 7, 4, 5 } ;
Declaration,
Array
x[0] = 7; x[1] = 4; x[2] = 5 ;
. Array Characters :
Declaration
char text1 [5] = { D, C, f, s, p };
char text2 [5] = DCfsp;
char text3 [ ] = DCfsp;
text[0] = D; text[1] = C; text[2] = f; text[3] = s; text[4] = p;
. Array
short array
array
Algorithm
array
Ex1: array
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std ;
int main(){
system("color 3A");
int b[100];
int i, j, n, temp;
cout<<"\t\tNAME : SANG SOPHEAP"<<endl;
cout<<"\t\tID : 32429"<<endl;
cout<<"\t\tMAJOR: INFORMATION TECHNOLOGY"<<endl;
cout<<"Number array in the array ?"<<endl;
cin>>n;
cout<<"Enter the element :"<<endl;
for(i=0;i<=n-1;++i){
cin>>b[i];}
cout<<"Content of the array is "<<endl;
for(i=0;i<=n-1;++i){
cout<<b[i]<<"\t";}
cout<<endl;
for(i=0;i<=n-1;++i){
for(j=0;j<=n-1;++j)
if(b[i]>b[j]){
temp=b[i];
b[i]=b[j];
b[j]=temp;}}
cout<<"Content of the array (short big to small form) is "<<endl;
for(i=0;i<=n-1;++i){
cout<<b[i]<<"\t";
}
cout<<endl;
getch();
}
Ex2. Array
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std ;
int main(){
system("color 4A");
int b[100];
int i, j, n, temp;
cout<<"\t\tNAME : SANG SOPHEAP"<<endl;
cout<<"\t\tID : 32429"<<endl;
cout<<"\t\tMAJOR: INFORMATION TECHNOLOGY"<<endl;
cout<<"Number array in the array ?"<<endl;
cin>>n;
cout<<"Enter the element :"<<endl;
for(i=0;i<=n-1;++i){
cin>>b[i];}
cout<<"Content of the array is "<<endl;
for(i=0;i<=n-1;++i){
cout<<b[i]<<"\t";}
cout<<endl;
for(i=0;i<=n-1;++i){
for(j=0;j<=n-1;++j)
if(b[i]<b[j]){
temp=b[i];
b[i]=b[j];
b[j]=temp;}}
C / C ++ ()
"
dUcrUbxageRkam
x[0,0]
x[0,1]
x[0,2]
x[0,3]
x[0,4]
x[1,0]
x[1,1]
x[1,2]
x[1,3]
x[1,4]
x[2,0]
x[2,1]
x[2,2]
x[2,3]
x[2,4]
0
1
2
{} braces
{} braces
,
SALES
series
( )
#include <iostream>
using namespace std;
int main(){
const int SIZE = 6; //size of array
(Initializing Arrays)
.
, DAYS , 12 days_per_month
#include <iostream>
using namespace std;
int main(){
int month, day, total_days;
int days_per_month[12] = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
cout << \nEnter month (1 to 12): ; //get date
cin >> month;
cout << Enter day (1 to 31): ;
10
. (Array Fundamentals)
#include <iostream>
using namespace std;
int main(){
int age[4];
for(int j=0; j<4; j++) //get 4 ages{
cout << Enter an age: ;
cin >> age[j]; //access array element
}
for(j=0; j<4; j++) //display 4 ages
11
.
(Initializing Multidimensional Arrays)
,
SALEMON
SALEINIT
#include <iostream>
#include <iomanip>
using namespace std;
const int DISTRICTS = 4; //array dimensions
const int MONTHS = 3;
int main(){
int d, m;
//initialize array elements
12
double sales[DISTRICTS][MONTHS]
= { { 1432.07, 234.50, 654.01 },
{ 322.00, 13838.32, 17589.88 },
{ 9328.34, 934.00, 4492.30 },
{ 12838.29, 2332.63, 32.93 } };
cout << \n\n;
cout << Month\n;
cout << 1 2 3;
for(d=0; d<DISTRICTS; d++)
{
cout <<\nDistrict << d+1;
for(m=0; m<MONTHS; m++)
cout << setw(10) << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint) << setprecision(2)
<< sales[d][m]; //access array element
}
cout << endl;
return 0;
}
Out put
{ 1432.07, 234.50, 654.01 }
. Arrays of Structures
structures
structures
#include <iostream>
using namespace std;
const int SIZE = 4; //number of parts in array
13
////////////////////////////////////////////////////////////////
struct part //specify a structure
{
int modelnumber; //ID number of widget
int partnumber; //ID number of widget part
float cost; //cost of part
};
////////////////////////////////////////////////////////////////
int main()
{
int n;
part apart[SIZE]; //define array of structures
for(n=0; n<SIZE; n++) //get values for all members
{
cout << endl;
cout << Enter model number: ;
cin >> apart[n].modelnumber; //get model number
cout << Enter part number: ;
cin >> apart[n].partnumber; //get part number
cout << Enter cost: ;
cin >> apart[n].cost; //get cost
}
cout << endl;
for(n=0; n<SIZE; n++) //show values for all members
{
cout << Model << apart[n].modelnumber;
cout << Part << apart[n].partnumber;
14
15
Function Sort_Array
Etmg .
16
p=&numbers[2]; *p=30;
p=numbers+3;
*p=40;
p=numbers; *(p+4)=50;
for(int n=0; n<5; n++){
if(n<4) cout<<numbers[n]<<",";
else cout<<numbers[n]<<".";
}
getch();
return 0;
}
17