Showing posts with label addition of matrix. Show all posts
Showing posts with label addition of matrix. Show all posts

Monday, September 19, 2011

Addition 3X3 Matrix using function

/*program of addition of two given 3X3 matrix through function*/
#include<stdio.h>
#include<conio.h>
void show_matrix(int mat[3][3]);
void add_matrix(int matA[3][3], int matB[3][3], int matSum[3][3]);
int main()
{
 int x[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
 int y[3][3] = { {1,4,7}, {2,5,8}, {4,1,2} };
 int z[3][3];
 add_matrix(x,y,z);
 printf("\nFirst matrix is :\n");
 show_matrix(x);
 printf("\nSecond matrix is :\n");
 show_matrix(y);
 printf("\nNew addition matrix is :\n");
 show_matrix(z);
 getch();
 return 0;
}


void add_matrix(int matA[3][3], int matB[3][3], int matSum[3][3])
{
  int r,c;
  for(r=0; r<3; r++)
  {
    for(c=0; c<3; c++)
        matSum[r][c]=matA[r][c]+matB[r][c];
  }
}


void show_matrix(int mat[3][3])
{
  int r,c;
  for(r=0; r<3; r++)
  {
    for(c=0; c<3; c++)
        printf(" %d",mat[r][c]);
    printf("\n");
  }
}


             Output of above program : 

First matrix is :
1 2 3
4 5 6
7 8 9


Second matrix is :
1 4 7
2 5 8
4 1 2


New addition matrix is :
2  6  10
6  10 14
11 9  11

Related program:

  1. Difference of two matrix
  2. Sum of matrix
  3. Transpose of matrix
  4. Product of matrix
  5. Diagonal sum of matrix

Monday, September 12, 2011

Addition of two 3x3 matrix

Q. Write a C program for addition of two 3x3 matrix.

Ans.


#include<stdio.h>
#include<conio.h>
int main()
{
 int mata[3][3],matb[3][3],matc[3][3];
 int r,c,k;
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    printf("Enter first matrix : ");
    scanf("%d",&mata[r][c]);
  }
 }
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    printf("Enter second matrix : ");
    scanf("%d",&matb[r][c]);
  } 
 }
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    matc[r][c]=0;
    for(k=0; k<3;k++)
       matc[r][c]=mata[r][c] + matb[r][c];
  }
 }
 printf("New addition matrix : \n"); 
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
     printf(" %d",matc[r][c]);
  printf("\n");
 }
 getch();
 return 0;
}

Output of above program :

Enter first matrix :
  1 2 3
  4 5 6
  7 8 9
Enter second matrix :
  2 1 7
  4 6 3
  8 1 1

New addition matrix :
  3  3  10
  8  11 7
  15 9  10