Multi-Dimensional Arrays www.hndit.com Multi-Dimensional Arrays
Multi-dimensional Arrays www.hndit.com Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single dimension. But think about a situation where we want to store elements of a 3x4 matrix. The C language allows to have multi- dimensional arrays for this kind of situations.
2 Dimensional Arrays - 2D Also called as 2D arrays. www.hndit.com 2 Dimensional Arrays - 2D Column Index No. Also called as 2D arrays. Take the example of the 3x4 matrix. To store the elements of this we can declare a 2D array as follows: int matrix[3][4]; 0 1 2 3 1 2 2 6 9 4 5 12 10 16 7 3 18 Raw Index No. Raw Column
Assigning Values to a 2D Array Elements www.hndit.com Example 1: You can refer by row column indices int matrix[3][4]; matrix[0][0] = 2; matrix[0][1] = 6; Example 2: You can initialize row wise int matrix[3][4] = { {2,6,9,4}, {5,12,10,16}, {7,3,3,18} };
Array Exercise www.hndit.com Declare a 2D array and store the values shown in the figure. Read the elements from the user After storing all the values, display the content of the matrix. Column Index No. 0 1 2 3 1 2 2 6 9 4 5 12 10 16 7 3 18 Raw Index No.
www.hndit.com Homework Write a program to read values for a 2x3 matrix from a user and multiply with a scalar value and display the result. The scalar value also you must read from the user. Write a program to multiply two matrices of integers. 1st matrix is 3x2 and the 2nd matrix is 2x4. Display the result. Note : No need to read the matrices values from the user
Thank You ! www.hndit.com