In this blog post, you will learn to write C Program to Add Two Matrices using two-dimensional arrays.
A matrix is a rectangular array of numbers, arranged in rows and columns. For example, A Matrix of order 5 x 6 looks like this:
2 8 3 9 8 9 0 6 6 9 0 0 5 8 0 9 1 0 6 7 0 3 4 5 7 2 2 3 0 4
Now let’s see the problem statement. But my request is that before looking at the solution try it out yourself first.
Problem Statement: Given two matrices add the value of the two matrices and print the resultant matrix.
Example,
Input: mat1[][] = 3 2 1 mat2[][]= 4 6 3 4 1 3 1 7 1 1 6 2 5 2 4 Output: 7 8 4 5 8 4 4 8 6
The solution of adding two matrices is simple as adding two numbers. You just need to add the numbers in the matching positions. That means the element of the 0th position of matrix-1 will be added to the 0th position element of matrix-2. But you need to remember that the two matrices must be the same size, i.e. the rows must match in size, and the columns must match in size.
Consider the below example of the addition of a 2×2 Matrix.
Now, takes an example of 2×2 matrix Mat1 and Mat2 and add them using the above-mentioned image.
The primary prerequisite to understanding the below example code:
C Program to Add Two Matrices:
The mentioned C code adds two matrices and displays the output on the console screen.
#include <stdio.h> #define MAX_SIZE 100 int main() { int mat1[MAX_SIZE][MAX_SIZE]; int mat2[MAX_SIZE][MAX_SIZE]; int sum[MAX_SIZE][MAX_SIZE]; int row, col, i,j; printf("Enter the number of rows (between 1 and %d): ", MAX_SIZE); scanf("%d", &row); printf("Enter the number of columns (between 1 and %d): ", MAX_SIZE); scanf("%d", &col); printf("\nEnter elements of 1st matrix:\n"); for (i = 0; i < row; ++i) { for (j = 0; j < col; ++j) { printf("Enter element mat1_%d%d: ", i, j); scanf("%d", &mat1[i][j]); } } printf("\n\nEnter elements of 2nd matrix:\n"); for (i = 0; i < row; ++i) { for (j = 0; j < col; ++j) { printf("Enter element mat2_%d%d: ", i, j); scanf("%d", &mat2[i][j]); } } // adding two matrices for (i = 0; i < row; ++i) { for (j = 0; j < col; ++j) { sum[i][j] = mat1[i][j] + mat2[i][j]; } } // printing the result of the sum printf("\nSum of two matrices: \n"); for (i = 0; i < row; ++i) { for (j = 0; j < col; ++j) { printf("%d ", sum[i][j]); if (j == col - 1) { printf("\n\n"); } } } return 0; }
Output:
Enter the number of rows (between 1 and 100): 2 Enter the number of columns (between 1 and 100): 2 Enter elements of 1st matrix: Enter element mat1_00: 2 Enter element mat1_01: -3 Enter element mat1_10: 4 Enter element mat1_11: 5 Enter elements of 2nd matrix: Enter element mat2_00: 1 Enter element mat2_01: 9 Enter element mat2_10: -3 Enter element mat2_11: -5 Sum of two matrices: 3 6 1 0
How Does C Program to Add Two Matrices Work?
Step-1:
I have created 2D arrays to store the value of the matrix and some local variables for intermediate value and mathematical operation.
int mat1[MAX_SIZE][MAX_SIZE]; int mat2[MAX_SIZE][MAX_SIZE]; int sum[MAX_SIZE][MAX_SIZE]; int row, col, i,j;
Step-2:
Ask the user to enter the numbers of rows and columns for both matrices which they want to add.
printf("Enter the number of rows (between 1 and %d): ", MAX_SIZE); scanf("%d", &row); printf("Enter the number of columns (between 1 and %d): ", MAX_SIZE); scanf("%d", &col);
Step-3:
Now ask the user to enter the element for both matrices.
//enter element for first matrix for (i = 0; i < row; ++i) { for (j = 0; j < col; ++j) { printf("Enter element mat1_%d%d: ", i, j); scanf("%d", &mat1[i][j]); } } //enter element for second matrix for (i = 0; i < row; ++i) { for (j = 0; j < col; ++j) { printf("Enter element mat2_%d%d: ", i, j); scanf("%d", &mat2[i][j]); } }
Step-4:
Now finally add both matrices by adding the numbers in the matching positions.
// adding two matrices for (i = 0; i < row; ++i) { for (j = 0; j < col; ++j) { sum[i][j] = mat1[i][j] + mat2[i][j]; } }
Recommended Post:
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- C program to calculate the value of nCr.
- C program to calculate the value of nPr.
- C program to find the most popular element in an array.
- C Program to find the largest and smallest element in an array.
- C program to move all zeroes to the end of array.
- Write a C program to find the sum of array elements.
- C program to find a duplicate element in an array.