How to write a C Program to Add Two Matrices or Matrix or how to write a program to perform the addition of two Multi-Dimensional Arrays with example.
C Program to Add Two Matrix
This program for matrix addition in C allows the user to enter the number of rows and columns of the two. Next, we are going to add those two matrices using For Loop.
#include<stdio.h> int main() { int i, j, rows, columns, a[10][10], b[10][10]; int arr[10][10]; printf("\n Please Enter Number of rows and columns : "); scanf("%d %d", &i, &j); printf("\n Please Enter the First Elements\n"); for(rows = 0; rows < i; rows++) { for(columns = 0; columns < j; columns++) { scanf("%d", &a[rows][columns]); } } printf("\n Please Enter the Second Elements\n"); for(rows = 0; rows < i; rows++) { for(columns = 0; columns < j; columns++) { scanf("%d", &b[rows][columns]); } } for(rows = 0; rows < i; rows++) { for(columns = 0; columns < j; columns++) { arr[rows][columns] = a[rows][columns] + b[rows][columns]; } } printf("\n The Sum of Two a and b = a + b \n"); for(rows = 0; rows < i; rows++) { for(columns = 0; columns < j; columns++) { printf("%d \t ", arr[rows][columns]); } printf("\n"); } return 0; }
In this C program for matrix addition, we declared 3 Two dimensional arrays, a, b, and arr, of the size of 10 * 10.
The below statements ask the User to enter the Matrices a, b sizes (Number of rows and columns. For instance, 2 Rows, 3 Columns = a[2][3] and b[2][3])
printf("\n Please Enter Number of rows and columns : "); scanf("%d %d", &i, &j);
Next, we used for loop to iterate each cell present in a[2][3]. Conditions inside the for loops ((rows < i) and (columns < j)) will ensure the Programming compiler does not exceed the Matrix limit. Otherwise, it will overflow.
The scanf statement inside the for loop will store the user entered values in every individual array element, such as a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]
Next, the for Loop in the C program for matrix addition or add stores user-entered values into b[2][3].
In the next program line, We have one more for loop to get the sum.
The user-inserted values for this matrix addition example are
a[2][3] = {{10, 20, 30}, { 40, 50, 60}}
b[2][3] = {{25, 35, 45}, { 55, 65, 75}}
C Program to Add Two Matrices – Row First Iteration: for(rows = 0; rows < 2; 0++)
The condition (0 < 2) is True. So, it will enter into the second for loop
Column First Iteration: for(columns = 0; 0 < 3; 0++)
The condition (columns < 3) is True. So, it will start executing the statements inside the loop
arr [rows][columns] = a[rows][columns] + b[rows][columns] = a[0][0] + b[0][0]
arr [0][0] = 10 + 25 = 35
Column Second Iteration: for(columns = 1; 1 < 3; 1++)
The condition (1 < 3) in this C program for matrix addition is True.
arr [0][1]= a[0][1] + b[0][1]
arr [0][1]= 20 + 35 = 55
Column Second Iteration: for(columns = 2; 2 < 3; 2++)
The condition (1 < 3) is True.
arr [0][2] = a[0][2] + b[0][2]
arr [0][2]= 30 + 45 = 75
Next, the j value will increment. After the increment, the condition inside the second for loop (columns < 3) will fail. So it will exit from the loop.
Now the value of rows is incremented (rows will become 1) and starts the second-row iteration.
Please follow the same steps where rows = 1
At last, we used another for loop to print the Addition Matrix.
Comments are closed.