Array subtraction in Two-Dimensional Array using Array in C


This program is written in C programming language and it does the following:

  • It first declares some integer variables r, c, a, b, i, j and a third 2D array 't'
  • The program then prompts the user to enter the row and column limits 'r' and 'c' respectively, which determines the size of two 2D arrays 'a' and 'b'
  • Then, it prompts the user to enter values for the two 2D arrays 'a' and 'b' using nested for loop
  • Next, it uses another nested for loop to iterate through both arrays 'a' and 'b' and performs subtraction of corresponding elements of both arrays and stores the result in another array 't'
  • The program then uses another nested for loop to print the values of the 't' array
  • The values of the subtraction are displayed in tabular format
  • The program ends by returning 0 as the value of the main function

Source Code

#include<stdio.h>
int main()
{
  int r,c,a[10][10],i,j,b[10][10],t[10][10];
  printf("\nEnter the row limit:");
  scanf("%d",&r);
  printf("\nEnter the column limit:");
  scanf("%d",&c);
  printf("\nEnter the first matrix:");
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
       scanf("%d",&a[i][j]);
    }
  }
  printf("\nEnter the second matrix:");
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
       scanf("%d",&b[i][j]);
    }
  }
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
      t[i][j]=a[i][j]-b[i][j];
    }
  }
  printf("\nTotal value is:\n");
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
      printf(" %d",t[i][j]);
    }
    printf("\n");
  }
  return 0;
 }
To download raw file Click Here

Output

Enter the row limit:2

Enter the column limit:2
Enter the first matrix:4
5
6
7
Enter the second matrix:1
2
3
4
Total value is:
 3 3
 3 3

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C