0% found this document useful (0 votes)
5 views

Tutorial 06

The document discusses declaring two 3x3 matrices and displaying their sum. It shows sample 3x3 matrices and the process of adding them element-wise to get the sum matrix. The C code declares the two input matrices, initializes a sum matrix, and uses nested for loops to add the elements and print the first matrix.

Uploaded by

Ranidu Ravishka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Tutorial 06

The document discusses declaring two 3x3 matrices and displaying their sum. It shows sample 3x3 matrices and the process of adding them element-wise to get the sum matrix. The C code declares the two input matrices, initializes a sum matrix, and uses nested for loops to add the elements and print the first matrix.

Uploaded by

Ranidu Ravishka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Programming with C Language

Tutorial 06 – Working with Multi-Dimensional Arrays (Matrix)


Write a C program for the followings;

Declare two 3 x 3 square matrices and display the matrix sum.


Following illustration shows the process of calculating the matrix sum. The values are used as
samples.

3 2 4 2 6 3 5 8 7
1 4 6 + 4 3 2 = 5 7 8
4 3 2 5 1 7 9 4 9

#include <stdio.h>
int main(){
int matrix1[3][3] = {{3, 2, 4}, {1, 4, 6}, {4, 3, 2}};
int matrix2[3][3] = {{2, 6, 3}, {4, 3, 2}, {5, 1, 7}};
int sumMatrix[3][3];
for (int i = 0; i < 3; ++i){
for (int j = 0; j < 3; ++j)
{
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("Matrix 1:\n");
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
printf("%d ", matrix1[i][j]);
}
printf("\n");
}
}

You might also like