0% found this document useful (0 votes)
4 views9 pages

Lecture 7 - Arrays (P3) - 2D Array

The document explains multidimensional arrays in C, focusing on two-dimensional arrays (2D arrays or matrices). It covers their declaration, initialization, element access, modification, and iteration through nested loops, along with examples of matrix multiplication. Key concepts include the necessity of specifying dimensions and the logic behind accessing and manipulating elements within these arrays.

Uploaded by

malakmahamad376
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Lecture 7 - Arrays (P3) - 2D Array

The document explains multidimensional arrays in C, focusing on two-dimensional arrays (2D arrays or matrices). It covers their declaration, initialization, element access, modification, and iteration through nested loops, along with examples of matrix multiplication. Key concepts include the necessity of specifying dimensions and the logic behind accessing and manipulating elements within these arrays.

Uploaded by

malakmahamad376
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C Multidimensional Arrays

A multidimensional array is basically an array of arrays.

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).

To create a 2D array of integers, take a look at the following example:

int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };

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 }

Initialization of Two-Dimensional Arrays


There are two methods to initialize two-dimensional arrays.

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}};

Access the Elements of a 2D Array


To access an element of a two-dimensional array, you must specify the
index number of both the row and column.

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} };

printf("%d", matrix[0][2]); // Outputs 2

Change Elements in a 2D Array


To change the value of an element, refer to the index number of the
element in each of the dimensions:

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;

printf("%d", matrix[0][0]); // Now outputs 9 instead of 1


Loop Through a 2D Array
To loop through a multi-dimensional array, you need one loop for each of
the array's dimensions.

The following example outputs all elements in the matrix array:

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;

for (int col = 0; col <= 3; col++) {

sum += rating[2][col];

double average = (double) sum / 4;


Example of Printing Two-dimensional Array as Matrix in Row and
Columns

#include <stdio.h>

int main() {

int arr[3][5] = {1,2,3,4,5, 10,20,30,40,50, 5,10,15,20,25};

int i, j;

for (i=0; i<3; i++){

for (j=0; j<5; 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;

for(i=0; i<3; i++){


for(j=0; j<3; j++){
sum=0;
for(k=0; k<3; k++)
sum = sum + mat1[i][k] * mat2[k][j];
mat3[i][j] = sum;
}
}
printf("\nMatrix 1 ...\n");
for(i=0; i<3; i++){
for(j=0; j<3; j++)
printf("%d\t", mat1[i][j]);
printf("\n");
}

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

Multiplication of the two given Matrices:


16 32 17
29 58 72
22 44 66

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].

You might also like