Introduction
Adding two matrices involves summing the corresponding elements of the two matrices to produce a new matrix. This guide will show you how to write a C program that adds two matrices provided by the user.
Problem Statement
Create a C program that:
- Takes two matrices as input from the user.
- Adds the corresponding elements of the two matrices.
- Displays the resulting matrix.
Example:
- Input:
- Matrix 1:
1 2 3 4 5 6 7 8 9 - Matrix 2:
9 8 7 6 5 4 3 2 1
- Matrix 1:
- Output: Resultant Matrix:
10 10 10 10 10 10 10 10 10
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the two input matrices, the resultant matrix, and loop counters.
- Input the Dimensions and Elements of the Matrices: Use loops to take input from the user for both matrices.
- Add the Corresponding Elements of the Matrices: Use nested loops to iterate through each element of the matrices and add them.
- Display the Resulting Matrix: Use loops to display the resulting matrix.
C Program to Add Two Matrices
#include <stdio.h>
int main() {
// Step 1: Declare variables to hold the matrices and dimensions
int rows, cols;
int matrix1[100][100], matrix2[100][100], result[100][100];
int i, j;
// Step 2: Prompt the user to enter the dimensions of the matrices
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d %d", &rows, &cols);
// Step 3: Input the elements of the first matrix
printf("Enter elements of the first matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix1[i][j]);
}
}
// Step 4: Input the elements of the second matrix
printf("Enter elements of the second matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix2[i][j]);
}
}
// Step 5: Add the corresponding elements of the matrices
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Step 6: Display the resulting matrix
printf("Resultant matrix after addition:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0; // Step 7: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- The variables
rowsandcolsstore the dimensions of the matrices.matrix1,matrix2, andresultare 2D arrays to store the input matrices and the resulting matrix, respectively. The variablesiandjare used as loop counters.
Step 2: Input the Dimensions of the Matrices
- The program prompts the user to enter the number of rows and columns for the matrices using
scanf.
Step 3: Input the Elements of the First Matrix
- The program uses nested
forloops to take input for each element of the first matrix from the user.
Step 4: Input the Elements of the Second Matrix
- Similarly, the program uses nested
forloops to take input for each element of the second matrix.
Step 5: Add the Corresponding Elements of the Matrices
- The program uses nested
forloops to iterate through each element of the matrices:- The corresponding elements from
matrix1andmatrix2are added and stored in theresultmatrix.
- The corresponding elements from
Step 6: Display the Resulting Matrix
- The program uses nested
forloops to display the elements of the resulting matrix after addition.
Step 7: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example:
Enter the number of rows and columns of the matrices: 3 3
Enter elements of the first matrix:
1 2 3
4 5 6
7 8 9
Enter elements of the second matrix:
9 8 7
6 5 4
3 2 1
Resultant matrix after addition:
10 10 10
10 10 10
10 10 10
Another Example:
Enter the number of rows and columns of the matrices: 2 2
Enter elements of the first matrix:
1 1
2 2
Enter elements of the second matrix:
3 3
4 4
Resultant matrix after addition:
4 4
6 6
Conclusion
This C program demonstrates how to add two matrices by iterating through each element and summing the corresponding elements from the two matrices. It covers basic concepts such as arrays, loops, and matrix operations, making it a useful example for beginners learning C programming.