 # Thread: C program for matrix manipulation

1. ## C program for matrix manipulation

Matrix multiplication in c language: c program to multiply matrices (two dimensional array), this program multiplies two matrices which will be entered by the user. plz can anyone tell me a program for this..  Reply With Quote

2. ## Re: c program for matrix manipulation

/*2.Program to multiply two matrices of orders to be provided by the user.*/

#include <stdio.h>

main()

{

int m,n,o,p;

int i,j,k;

int *mat1,*mat2,*mat3;

printf("\n Enter the order of First matrix in the form mn : "); //getting order of 1st matrix
scanf("%d%d",&m,&n); //read order of 1st matrix

mat1=malloc(m*n*4); // creating dynamic memory for 1st matrix

printf("\n Enter the order of Second matrix in the form mn : "); //getting order of 2nd matrix

scanf("%d%d",&o,&p); //read order of 1st matrix

if(n!=o)

{

printf("Multiplication is invalid\n"); //checking if multiplication of matrix is valid

return 0;

}

else

mat2=malloc(o*p*4);
// creating dynamic memory for 2nd matrix
mat3=malloc(m*p*4);

printf("\nENTER MATRIX 1 DETAILS of order %d%d :-\n",m,n);

for(i=0 ; i<m*n ; i++)

scanf("%d",(mat1+i));

printf("\nENTER MATRIX 2 DETAILS of order %d%d :-\n",n,p);

for(i=0 ; i<n*p ; i++)

scanf("%d",(mat2+i));
for(i=0 ; i<m*p ; i++)

*(mat3+i)=0;

printf("\nThe multiplication of two matrices gives the following matrices:-\n");

for(i=0 ; i<m ; i++)

{

for(j=0 ; j<p ; j++)

{
for(k=0 ; k<n ; k++)

*(mat3+i*p+j)=*(mat3+i*p+j)+*(mat1+i*n+k)**(mat2+k*p+j);

printf("%d\t",*(mat3+i*p+j));

}

printf("\n");

}

}

---------- Post added at 07:51 AM ---------- Previous post was at 07:48 AM ----------

4.Program to calculate and print the sum of two given 3*3 matrices

#include<stdio.h>
main()
{
int i,j;
int m1,m2,m3;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\nEnter the value of matrix 1[%d%d]",i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\nEnter the value of matrix 2[%d%d]",i+1,j+1);
scanf("%d",&m2[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
printf("%d\t",m3[i][j]);
}
printf("\n");
}

}  Reply With Quote