Arrays are used to define a data block in the memory with number of data types, like integer numbers, floating point numbers, characters, structures, … etc. C & C++ programming language provides this data structure, called as array that stores a fixed-size of elements of the same type. Arrays are generally used to store a collection of data, and it is often more useful to store data as a collection of variables of the same type. We can also store multiple data types of variables in arrays by using structure (struct) elements.
Arrays are very good to use variables with their index numbers, instead of using each for each variables. For example we can use 6th string name element as name[5] instead of using name0, name1, name2, name3, name4, name5. All arrays consist of contiguous memory locations, their index number starts by 0, so we can not use negative numbers as a index, and we can not exceed maximum number of elements. The lowest memory address corresponds to the first element index number and the highest address to the last element index number.
Basically, we can illustrate this in general form as below,
1 2 3 |
type array [ size ]; |
Here are some array examples,
1 2 3 4 5 6 7 |
int x[10]; // array with 10 integers, indexes are 0 to 10 float y[5]; // array 5 floating point numbers, indexes are 0 to 5 char s[255]; // array 255 characters, this is ASCII format string, indexes are 0 to 255 |
1. Initializing Arrays
We can initialize array elements in C && C++ programming language as below,
1 2 3 |
int x[5]; |
1 2 3 |
int x[5]= { 0, 10, 20, 30, 40 }; |
We can init arrays without number of elements as below,
1 2 3 |
int x[]= { 0, 10, 20, 30, 40 }; |
ASCII strings are also char arrays, we can define a ASCII string as below
1 2 3 |
char s[]= "Hello"; |
2. Accessing to Elements of Arrays
We can access any array of element by using it’s index number.
1 2 3 4 5 6 7 8 9 |
int x[]= { 0, 10, 20, 30, 40 }; std::cout << x[0] << '\n'; std::cout << x[1] << '\n'; std::cout << x[2] << '\n'; std::cout << x[3] << '\n'; std::cout << x[4] << '\n'; |
We can access arrays by using for() , while() or do-while() loops, so this example can be written easily as below,
1 2 3 4 5 6 7 8 |
int x[]= { 0, 10, 20, 30, 40 }; for(int i=0; i<=4; i++) { std::cout << x[i] << '\n'; } |
We can access to each character of char arrays as below,
1 2 3 4 5 |
char s[]= "Hello"; std::cout << s[0] << ',' << s[1] << ',' << s[2] << ',' << s[3] << ',' << s[4] << '\n'; |
3. Initializing and Accessing Multi-Dimensional Arrays (2D, 3D … arrays)
All array examples given above were one dimensional arrays. We can initialize and access to 2D, 3D or multi dimensional arrays, they are also called as matrix forms. While we live in 3D environment, in mathematics we can define and do calculations more than 3 dimensions.
Here is an example to init and access to a 2D matrix form with integer numbers;
1 2 3 4 5 6 7 8 9 10 11 |
int m[5][5]; m[0][0] = 00; m[1][1] = 11; m[3][3] = 33; cout << m[0][0] << '\n'; cout << m[1][1] << '\n'; cout << m[3][3] << '\n'; |
Here is an example to init and access to a 3D matrix form with integer numbers;
1 2 3 4 5 6 7 8 9 10 11 |
int m[3][4][5]; m[0][0][0] = 000; m[1][1][1] = 111; m[2][2][2] = 222; cout << m[0][0][0] << "\n"; cout << m[1][1][1] << "\n"; cout << m[2][2][2] << "\n"; |
Generally we read and write these values by using for loops. For example for a two dimensional array we should use two loops for i & j indexes. See how we can set variables and how we can print out them as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
int m[10][10]; for( int j=0; j<10; j++) { for( int i=0; i<10; i++) { m[j][i]=j*10+i; } } for( int j=0; j<10; j++) { for( int i=0; i<10; i++) { std:: cout << m[j][i] <<","; } std:: cout << "\n" } |