Introduction
Subtracting two matrices involves subtracting the corresponding elements of the two matrices to produce a new matrix. This guide will show you how to write a C program that subtracts one matrix from another.
Problem Statement
Create a C program that:
- Takes two matrices as input from the user.
- Subtracts the corresponding elements of the second matrix from the first matrix.
- Displays the resulting matrix.
Example:
- Input:
- Matrix 1:
5 6 7 8 9 10 11 12 13 - Matrix 2:
1 2 3 4 5 6 7 8 9
- Matrix 1:
- Output: Resultant Matrix:
4 4 4 4 4 4 4 4 4
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.
- Subtract the Corresponding Elements of the Matrices: Use nested loops to iterate through each element of the matrices and subtract them.
- Display the Resulting Matrix: Use loops to display the resulting matrix.
C Program to Subtract 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: Subtract 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 subtraction:\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: Subtract the Corresponding Elements of the Matrices
- The program uses nested
forloops to iterate through each element of the matrices:- The corresponding elements from
matrix2are subtracted frommatrix1and 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 subtraction.
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:
5 6 7
8 9 10
11 12 13
Enter elements of the second matrix:
1 2 3
4 5 6
7 8 9
Resultant matrix after subtraction:
4 4 4
4 4 4
4 4 4
Another Example:
Enter the number of rows and columns of the matrices: 2 2
Enter elements of the first matrix:
10 20
30 40
Enter elements of the second matrix:
1 2
3 4
Resultant matrix after subtraction:
9 18
27 36
Conclusion
This C program demonstrates how to subtract one matrix from another by iterating through each element and subtracting 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.