Advanced Arrays --------------- A 2-dimensional array consists of both rows and columns of elements. For example the array of numbers 8 16 9 52 3 15 27 6 14 25 2 10 is called a 2-dimensional array. The first row contains: 8, 16, 9 and 52. The second row: 3, 15, 27, 6. The third row: 14, 25, 2, 10. The first column contains: 8, 3, 14. The rest is obvious. Each of the elements is accessed starting with the 0th position and ending with the (n-1)th position. If the array is defined such as this: int name[row][cols] Then the above table would be defined like this: int name[3][4]; There are 3 rows and 4 columns. To access the upper left element, the command is: name[0][0] The lower left element is: name[2][0] The upper right element is: name[0][3] The rest is quite intuitive. 2-dimensional arrays are useful for assignments with loops. The (i, j)th position in the table can be index with a set of nested loops. For example: for(i = 0; i < 3; i++) for(j = 0; j < 4; j++) name[i][j] = 0; This set of loops clearly assigns each element of the array to 0. There is another way to do this assignment. That is by using the array initialization at declaration time like this: int name[3][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; Problem #4 of Exercises 7.4 --------------------------- Write a C function that adds respective values of two 2-dimensional arrays named "first" and "second", respectively. Both arrays have two rows and three columns. The arrays are of type integer. The array "first" should be initialized by: int first[2][3] = {{16, 18, 23}, {54, 91, 11}}; The array "second" should be initialized by: int second[2][3] = {{24, 52, 77}, {16, 19, 59}}; Solution: Use those declarations above and the following: int sum[2][3]; int i,j; for(i = 0; i < 2; i++) for(j = 0; j< 3; j++) sum[i][j] = first[i][j] + second[i][j];