Wednesday, 9 November 2011

Write a program in C to compute addition/subraction of two matrices.Use functions to read,display and add/subtract the matrices.



  # include<stdio.h>
  # include<conio.h>
  void main()
  {
  void show(int c[2][2]);
  void insert(int l[2][2]);
  void add(int a[2][2],int b[2][2]);
  void sub(int a[2][2],int b[2][2]);
  int x[2][2],y[2][2],op;

  clrscr();
  printf("\n Enter First Matrix  : \n");
  insert(x);
  printf("\n Eneter Second Matrix: \n");
  insert(y);
  printf("\n First Matrix is     : \n");
  show(x);
  printf("\n");
  printf("\n Second Matrix is    : \n");
  show(y);
  printf(" \n\n");
  printf("\n 1) Addition \n");
  printf("\n 2) Subtraction \n");

  printf("\nEnter your option: \n\n");
  scanf("%d",&op);
  switch(op)
  {
   case 1 : add(x,y);
   break;
   case 2 : sub(x,y);
   break;
  }
   getch();
 }

 // insert function allow us to insert values of matrix
 void insert(int c[2][2])
 {
   int i,j;
   for(i=0; i<2; i++)
   {
    for(j=0; j<2; j++)
     {
       scanf("%d",&c[i][j]);
     } // end of inner for loop
   }   // end of outer for loop
 }    // end of insert function

  // show function allow us to display the element of matrix
  void show(int c[2][2])
  {
   int i,j;
   printf(" \n ");
   for(i=0; i<2; i++)
   {
    for(j=0; j<2; j++)
     {
       printf(" %d ",c[i][j]);
     } // end of inner for loop
   }   // end of outer for loop
 }     // end of show function

 // add function allow us to add two matrix
 void add(int a[2][2],int b[2][2])
 {
  int i,j,c[2][2];
  printf("\n\nAddition is \n\n");
  for(i=0; i<2; i++)
  {
   for(j=0; j<2; j++)
     {
      c[i][j] = a[i][j] + b[i][j]; // Addition of two matrix is perform here
      printf("\t %d ",c[i][j]);
     } // end of inner for loop
   }   // end of outer for loop
 }     // end of add function

 // sub function allow us to Subtract two matrix
 void sub(int a[2][2],int b[2][2])
 {
  int i,j,c[2][2];
  printf("\n\n Sunstraction is \n\n");
  for(i=0; i<2; i++)
  {
   for(j=0; j<2; j++)
     {
      c[i][j] = a[i][j] - b[i][j]; // Substraction of two matrix is perform here
      printf("\t %d ",c[i][j]);
     } // end of inner for loop
   }   // end of outer for loop
 }     // end of sub function

No comments:

Post a Comment