Beruflich Dokumente
Kultur Dokumente
Declaration
//datatype array-name [size]; int num[5]; declares an array of 5 int num[0] num[1] num[2] num[3] num[4]
Partial assignment:
int list[5]={1,2}; declares an array of 5 int, and initialize the first 2 elements to 1 and 2, and set the rest elements to zero list[0] 1 list [1] 2 list [2] 0 list [3] 0 list [4] 0 int list[10]={0}; array of 10 int, and all elements equal to zero
But, the result of the second statement (int list[ ] = {2,1};) is: list[0] list [1] 2 1
//2. Reading data into an array // cin>>list; illegal cout<<"Enter three double values:"; for ( index=0; index<3; index++) cin>>list[index]; cout<<endl;
//3. Printing an array // cout<<list; illegal cout<<"The values = "; for ( index=0; index<3; index++) cout<<list[index]<<" "; cout<<endl;
//4.Finding the sum and average of an array for ( index=0; index<3; index++) sum+=list[index]; average = sum /3.0; cout<<"Sum ="<<sum<<endl <<"Average ="<<average<<endl;
//finding the largest element in the array int maxIndex = 0; int max=list[0]; for ( index=1; index<3; index++) if(list[index]>max) { maxIndex = index; max = list[index]; } cout<<"Maximum "<<max <<" found at index:"<<maxIndex; system("pause"); return 0; }