Google Search

Wednesday, October 27, 2010

Write a C program using dynamic memory allocation to perform multiplication of two matrices

Write a C program using dynamic memory allocation to perform multiplication of two matrices

void main()
{

int **a,**b,**c;
//int c[3][3];
int a_r,a_c,b_r,b_c;
int i,j,k;
clrscr();
again:
printf("\nenter rows and columns for matrix one:");
scanf("%d%d",&a_r,&a_c);
printf("\nenter rows and columns for matrix two:");
scanf("%d%d",&b_r,&b_c);
if(a_c!=b_r )
{
printf("\ncan not multiply");
goto again;
}
/* allocate memory for matrix one */
a=(int **) malloc(sizeof(int *),a_r);
for( i=0;i {
a[i]=(int *) malloc(sizeof(int*)*a_c);
}

/* allocate memory for matrix two */
b=(int **) malloc(sizeof(int)*b_r);
for( i=0;i {
b[i]=(int *) malloc(sizeof(int*)*b_c);
}
/* allocate memory for sum matrix */
c=(int **) malloc(sizeof(int *)*a_r);
for( i=0;i {
c[i]=(int *) malloc(sizeof(int)*b_c);
}


printf("\n enter matrix one %d by %d\n",a_r,a_c);
for(i=0;i {
for(j=0;j {
scanf("%d",&a[i][j]);
}
}

printf("\n enter matrix two %d by %d\n",b_r,b_c);
for(i=0;i {
for(j=0;j {
scanf("%d",&b[i][j]);
}
}

/*initialize product matrix */
for(i=0;i {
for(j=0;j {
c[i][j]=0;
}
}

/* multiply matrix one and matrix two */
for(i=0;i {
for(j=0;j {
for(k=0;k {
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
/* display result */

printf("\n Product of matrix one and two is\n");
for(i=0;i {
for(j=0;j {
printf("%d\t",c[i][j]);

}
printf("\n");
}

/*free memory*/
for(i=0;i {
free(a[i]);
}
free(a);
for(i=0;i {
free(b[i]);
}
free(b);
for(i=0;i {
free(c[i]);
}
free(c);

printf("\npress any key");
getch();
}

0 comments:

Post a Comment