7. Initialization of the Array int A[5]= {1,2,3,4,5}; int ARR[ ] = {1,2,3,4,5,6}; char B[20]=“Rudrapur”; float C[5]={2.4,3.5,1.5}; int D[2][3]={{1,2}, {3,4}, {5,6}};
8. Accessing Array Elements In C++ first element of the array is always at zero position. A[0] A[1] A[2] A[3] A[4] A[5]
9. Sample Program on 1-D Array #include<iostream.h> void main() { int A[10],i; for(i=0;i<10;i++) { cout<<“Enter a number: “; cin>A[i]; } cout<<“ Array Contents”; for(i=0;i<10;i++) { cout<<A[i]<<“ “; } }
10. Sample Program on 2-D Array #include<iostream.h> void main() { int A[3][4],i,j; for(i=0;i<20;i++) for(j=0j<4;j++) { cout<<“Enter a number: “; cin>A[i]; } cout<<“ Array Contents”; for(i=0;i<10;i++) { for(j=0j<4;j++) cout<<A[i]<<“ “; cout<<endl; } }
11.
12. Address Calculation in Row Major A[i][j]= B + [i*n + j] Where B=Base address of the array n = Total column of the array i = Row number of desired row j = Column number of desired row
13. Address in Column Major A[i][j]= B + [i + j*m] Where B=Base address of the array m = Total rows of the array i = Row number of desired row j = Column number of desired row