The contents of this chapter include all of the following: 2 Dimensional array, Initializing 2 Dimensional array, Memory map of 2-D array, Pointer and 2-D array, Array of pointer, Passing 2-D Array to a Function. | CSC103: Introduction to Computer and Programming Lecture No 20 Previous lecture Pointer arithmetic – a program Sorting techniques – a program Passing an Entire Array to a Function Array and pointer Today’s lecture outline 2 Dimensional array Initializing 2 Dimensional array Memory map of 2-D array Pointer and 2-D array Array of pointer Passing 2-D Array to a Function Two Dimensional Arrays It is also possible for arrays to have two or more dimensions For example you want to input roll no and mark of 4 students. Since we know 1-D array, so for this problem we can have two 1-D array One for roll number Second for marks One 2-D array can be used to store these values Example program 1 0 0 1 2 3 row column Roll Number Marks stud[0][0] stud[0][1] stud[1][0] stud[1][1] stud[2][0] stud[2][1] stud[3][0] stud[3][1] 10 85 12 65 13 89 14 92 Go to program Initializing a 2-Dimensional Array Example program Write a program that adds two 4 x 4 matrices. 3 5 6 1 5 3 2 9 1 0 -3 4 7 2 3 -9 4 2 -7 0 5 7 3 1 6 3 -1 0 8 9 -2 3 + 7 7 -1 1 10 10 5 10 7 3 -4 4 15 11 1 -6 = Go to program Memory map of 2D array Remember student array int s[4][2]; But actually memory allocated is continuous 1 0 0 1 2 3 10 85 12 65 13 89 14 92 10 85 12 65 13 89 14 92 s[0][0] s[0][1] s[1][0] s[1][1] s[2][0] s[2][1] s[3][0] s[3][1] Each row of a two-dimensional array can be thought of as a one-dimensional a[4][2] This is important to know if you want to access 2-D array elements using pointer Pointers and 2-Dimensional Arrays 1 0 0 1 2 3 10 85 12 65 13 89 14 92 Array 1 Array 2 Array 3 Array 4 Cont. 1234 56 1212 33 1434 80 1312 78 s[0][0] s[0][1] s[1][0] s[1][1] s[2][0] s[2][1] s[3][0] s[3][1] 65508 65512 65516 65520 65524 65528 65532 65536 s[0] s[1] s[2] s[3] 65508 65516 65524 65532 Cont Now we know each 1-D array Next thing is to access elements of a 1-D array Suppose we want to refer to the element s[2][1] using pointers s[2] 65524 that is that address of third 1-D array 65524 + 1 65528 . | CSC103: Introduction to Computer and Programming Lecture No 20 Previous lecture Pointer arithmetic – a program Sorting techniques – a program Passing an Entire Array to a Function Array and pointer Today’s lecture outline 2 Dimensional array Initializing 2 Dimensional array Memory map of 2-D array Pointer and 2-D array Array of pointer Passing 2-D Array to a Function Two Dimensional Arrays It is also possible for arrays to have two or more dimensions For example you want to input roll no and mark of 4 students. Since we know 1-D array, so for this problem we can have two 1-D array One for roll number Second for marks One 2-D array can be used to store these values Example program 1 0 0 1 2 3 row column Roll Number Marks stud[0][0] stud[0][1] stud[1][0] stud[1][1] stud[2][0] stud[2][1] stud[3][0] stud[3][1] 10 85 12 65 13 89 14 92 Go to program Initializing a 2-Dimensional Array Example program Write a program that adds two 4 x 4 matrices. 3 5 6 1 5 3 2 9 1 0 -3 4 7 2 3 -9 4 2 -7 0 5 7