Pages

Saturday, April 5, 2014

Product of matrix


Q. Write a C program to accept value of matrix and find out the product of matrix.
OR
Q. Give a example of 2d array with suitable arithmetic operation. 

Ans.

/*program of product of two matrix*/
#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void prod_mat(int [MAX][MAX],int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
 int x[MAX][MAX],y[MAX][MAX],z[MAX][MAX];
 int row,col;
 printf("Enter no. of rows and columns : ");
 scanf("%d%d",&row, &col);
 printf("Enter values in first matrix :
"
);
 input_mat(x,row,col);
 printf("Enter values in second matrix :
"
);
 input_mat(y,row,col);
 prod_mat(x,y,z,row,col);
 printf("
Display product of two matrices :"
);
 show_mat(z,row,col);
 getch();
 return 0;
}

void input_mat(int matA[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       scanf("%d",&matA[i][j]);
  }
}


void prod_mat(int matA[MAX][MAX],int matB[MAX][MAX],int matC[MAX][MAX],int r,int c)
{
 int i,j,k;
 for(i=0; i<r; i++)
 {
  for(j=0;j<c; j++)
  {
    matC[i][j]=0;
    for(k=0;k<c; k++)
    {
     matC[i][j]=matC[i][j]+matA[i][j]*matB[i][j];
    }
  }
 }
}

void show_mat(int mat[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       printf(" %d",mat[i][j]);
    printf("
"
);
  }
}

Output:-

Enter no. of rows and columns : 3 3
Enter values in First matrix :
4 5 3
2 1 9
6 7 5
Enter values in second matrix :
1 3 2
7 6 4
2 1 0
Display product of two matrices :
12 45 18
42 18 108
36 21 0

You might also like:
  1. Difference of two matrix C program
  2. Transpose of matrix C program
  3. Sum of two matrix C program
  4. Sum of diagonal elements C program
  5. Sum of all corner elements C program

Related Posts by Categories

0 comments:

Post a Comment