Subtraction of two matrices in C
Written by
- Subtraction of matrices can be done when the matrices are compatible with each other.
- The matrices are said to be compatible with each other when the number of rows and columns in both the matrices are equal.
- Thus, if the first matrix has m rows and n columns second matrix should also have m rows and n columns.
- Thus, the elements can be added using the following formula: Ci,j = Ai,j – Bi,j, where i is the number of rows and j is the number of columns.
Approach:
- First we will take the number of rows and columns of each matrix as our input.
- Next we validate if subtraction is possible, based upon number of rows and columns of both matrices are equal or not and accordingly either proceed with subtraction if valid and if invalid inform the user of it.
- Next, using the above mentioned formula we calculate the difference of the matrices.
- In this program, we shall see how to declare functions for two dimensional array and pass 2D arrays to the function.
- While passing 2D arrays to functions it can be done similar to 1D arrays, i.e either passing it by reference by simply passing the array (i.e using array name) or using pointer to array.
- We will see this program by using the first approach of passing the array to function.
Important Notes regarding functions and 2D arrays:
- A very important point to note is the called function does not allocate space for the array and it does not need to know the overall size, so the number of rows can be omitted.
- Space is not allocated because called function does not create a local copy of the array rather it uses the original one that has been passed to it.
- The width of the array i.e. the number of columns, is still important because number of elements contained by one row has to be told to compiler in order to increment the pointer to point the next row. So the column dimension must be specified; else there is no method for the compiler to understand what does array[1][2] hold.
- Remember that, all though we see 2D array as a matrix representation in the memory the array elements are always stored in contiguous box in a linear fashion.
Code (Passing the array i.e inherent pass by reference ):
#include<stdio.h>
int sub_array(int arr1[][5], int arr2[][5], int row, int col); //function prototype
int main()
{
int arr1[5][5], arr2[5][5]; //declaring array of predefined size 5 x 5
int i, j, rows1,col1, rows2, col2;
printf ("Enter the number of rows in the first matrix\n");
scanf("%d", &rows1);
printf ("Enter the number of columns in the first matrix\n");
scanf("%d", &col1);
printf ("Enter the number of rows in the second matrix\n");
scanf("%d", &rows2);
printf ("Enter the number of columns in the second matrix\n");
scanf("%d", &col2);
if ((rows1 != rows2) || (col1!=col2))
{
printf("\nThe matrices are not compatible. In order to perform subtraction of matrices number of rows and columns of the matrices should be equal.\n");
}
else
{
printf("Enter First Matrix Elements:\n"); //First Matrix
for(i = 0; i < rows1; i++)
{
for(j = 0; j < col1; j++)
{
scanf("%d", &arr1[i][j]);
}
}
printf("\nEnter Second Matrix Elements:\n"); //Second Matrix
for(i = 0; i < rows2; i++)
{
for(j = 0; j < col2; j++)
{
scanf("%d", &arr2[i][j]);
}
}
sub_array(arr1, arr2, rows1, col1);
}
return 0;
}
int sub_array(int arr1[][5], int arr2[][5], int row, int col)
{
int i, j, sub[5][5];
for(i = 0; i < row; i++) //Performing subtraction of Matrix 1 and 2
{
for(j = 0; j < col; j++)
{
sub[i][j] = arr1[i][j] - arr2[i][j];
}
}
printf("\nSubtraction of matrices is as follows:\n");
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
printf("%d\t", sub[i][j]);
}
printf("\n");
}
}
Output:
Case 1: When number of rows and columns of both matrices do not match:
Enter the number of rows in the first matrix
2
Enter the number of columns in the first matrix
2
Enter the number of rows in the second matrix
2
Enter the number of columns in the second matrix
3
The matrices are not compatible. In order to perform subtraction of matrices number of rows and columns of the matrices should be equal.
Case 2: When number of rows and columns of both matrices match:
Enter the number of rows in the first matrix
3
Enter the number of columns in the first matrix
3
Enter the number of rows in the second matrix
3
Enter the number of columns in the second matrix
3
Enter First Matrix Elements:
1
2
3
4
1
2
10
20
0
Enter Second Matrix Elements:
2
0
4
0
1
5
0
3
01
Subtraction of matrices is as follows:
-1 2 -1
4 0 -3
10 17 -1