Lecture 7 - Arrays (P3) - 2D Array
Lecture 7 - Arrays (P3) - 2D Array
Arrays can have any number of dimensions., we will introduce the most
common; two-dimensional arrays (2D).
Two-Dimensional Arrays
A 2D array is also known as a matrix (a table of rows and columns).
The first dimension represents the number of rows [2], while the second
dimension represents the number of columns [3]. The values are placed
in row-order, and can be visualized like this:
You must always specify the second dimension even if you are specifying
elements during the declaration. Let’s understand this with the help of few
examples –
/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second dimension*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }
Method 1
int multi_dim[4][3]={10,20,30,40,50,60,20,80,90,100,110,120};
Method 2
int multi_dim[4][3]={{10,20,30},{40,50,60},{70,80,90},{100,110,120}};
Accessing two-dimensional arrays can be done using row index value and
column index value.
Name_of_the arrays[row_index][column_index];
This statement accesses the value of the element in the first row
(0) and third column (2) of the matrix array.
Example
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
The following example will change the value of the element in the first
row (0) and first column (0):
Example
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;
Example
#include <stdio.h>
int main() {
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d\n", matrix[i][j]);
}
}
return 0;}
EXAMPLE: Find the average rating by the reviewer in row 2.
int sum = 0;
sum += rating[2][col];
#include <stdio.h>
int main() {
int i, j;
printf("%4d", arr[i][j]);
printf("\n");
return 0;
Output
1 2 3 4 5
10 20 30 40 50
5 10 15 20 25
Example of Multiplication of Two-dimensional
Array (Matrix)
The following program performs the multiplication of two matrices.
Open Compiler
#include<stdio.h>
int main(){
int mat1[3][3] = { {2, 4, 1} , {2, 3, 9} , {3, 1, 8} };
int mat2[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 4, 7} };
int mat3[3][3],
int sum=0, i, j, k;
printf("\nMatrix 2 ...\n");
for(i=0; i<3; i++){
for(j=0; j<3; j++)
printf("%d\t", mat2[i][j]);
printf("\n");
}
printf("\nMultiplication of the two given Matrices: \n");
for(i=0; i<3; i++){
for(j=0; j<3; j++)
printf("%d\t", mat3[i][j]);
printf("\n");
}
return 0;
}
Output
Matrix 1 ...
2 4 1
2 3 9
3 1 8
Matrix 2 ...
1 2 3
3 6 1
2 4 7
this C program performs matrix multiplication of two 3x3 matrices, mat1 and mat2, and
stores the result in a new matrix, mat3. Here’s a simple breakdown of each part of the
code:
1. Initializing Matrices:
o mat1 and mat2 are 3x3 matrices, each initialized with specific values.
o mat3 is a 3x3 matrix that will store the result of multiplying mat1 and
mat2.
2. Matrix Multiplication Logic:
o The outer for loops (with indices i and j) go through each position in
mat3, where i represents rows and j represents columns.
o The innermost for loop (using k) performs the actual matrix
multiplication calculation. It goes through each element in row i of mat1
and multiplies it with the corresponding element in column j of mat2.
These products are summed up to give the element for mat3[i][j].
3. Printing Matrices:
o After multiplying, the program prints each matrix (mat1, mat2, and mat3)
in a nicely formatted way to show the results.
o It uses nested loops to print each row of the matrices.
4. Explanation of Key Parts:
o sum = sum + mat1[i][k] * mat2[k][j]; - This line is the heart of
matrix multiplication, where each element from a row in mat1 is
multiplied by each corresponding element from a column in mat2.
o mat3[i][j] = sum; - After the innermost loop finishes, sum holds the
computed value for mat3[i][j].