Chapter 2 - Array
Chapter 2 - Array
6
Initializing Array
#include<iostream>
using namespace std;
int main()
{
int myarray[3]={2,5,6};
cout<< myarray[index];
//index will be the required output
return 0;
}
Cont..
• To display the entire elements of array we need to loop
through the sequence.
#include<iostream.h>
int main()
{
int mark[3]={2,5,7};
for(int index=0;index<3;index++)
{
cout<<mark[index];
}
return 0;
}
Two Dimensional (2D) Array
• In a first (left) subscript as being the row, and two-
dimensional array, it is convenient to think of the second
(right) subscript as being the column.
• array[row][col]
Initializing a 2D Array
1. use nested braces, with each set of numbers representing a
row:
int array[3][5] ={{ 1, 2, 3, 4, 5 }, // row 0
{6, 7, 8, 9, 10 }, // row 1
{11, 12, 13, 14, 15 } // row 2
};
• The compiler can do the math to figure out what the array length is.
• However, the following is not allowed:
int array[ ][ ] = { 1, 2, 3, 4, 5, 6, 7, 8 };
• Because the inner parenthesis are ignored, the compiler can not tell
whether you intend to declare a 1×8, 2×4, 4×2, or 8×1 array in this
case.