Sunday 8 July 2012

Subtract matrices


C code to subtract matrices of any order. This program finds difference between corresponding elements of two matrices and then print the resultant matrix.

C programming code

#include<stdio.h>
 
int main()
{
  int m, n, c, d, first[10][10], second[10][10], difference[10][10];
 
  printf("Enter the number of rows and columns of matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");
 
  for (c = 0; c < m; c++)
    for (d = 0 ; d < n; d++)
      scanf("%d", &first[c][d]);
 
  printf("Enter the elements of second matrix\n");
 
  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
        scanf("%d", &second[c][d]);
 
  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
      difference[c][d] = first[c][d] - second[c][d];
 
  printf("difference of entered matrices:-\n");
 
  for (c = 0; c < m; c++)
  {
    for (d = 0; d < n; d++)
      printf("%d\t",difference[c][d]);
 
    printf("\n");
  }
 
  return 0;
}

0 comments:

Post a Comment