#include <stdio.h>
#include<stdlib.h>
int **matrix_mul(int **m1,int **m,int a,int b,int c,int d);
main()
{
int i,j,r1,r2,c1,c2,**p,**q;
printf("Enter the number of rows and columns of first matrix :\t");
scanf("%d%d",&r1,&c1);
printf("Enter the number of rows and columns of second matrix :\t");
scanf("%d%d",&r2,&c2);
//memory allocation for m1
int **m1;
m1 = malloc(r1 * sizeof(int *));
if(m1 == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < r1; i++)
{
m1[i] = malloc(c1 * sizeof(int));
if(m1[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
//memory allocation for m1
int **m2;
m2 = malloc(r2 * sizeof(int *));
if(m2 == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < r2; i++)
{
m2[i] = malloc(c2 * sizeof(int));
if(m2[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
if(c1==r2)
{
printf("Enter the elements of first matrix :\t");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d \t",&m1[i][j]);
}
}
printf("Enter the elements of second matrix :\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d \n",&m2[i][j]);
}
}
printf("\n first matrix is:");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d \t",m1[i][j]);
}
}
printf("\n second matrix is:");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("%d \t",m2[i][j]);
}
}
p=matrix_mul(m1,m2,r1,c1,r2,c2);
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
q=(1/2*100)*p[i][j];
//display result
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\n",q[i][j]);
}
getchar();
}
}
else
printf("Multiplication not possible.\n");
getchar();
}
//FUNCTION_matrix_mul
int **matrix_mul(int **m1,int **m2,int r1,int c1,int r2,int c2 )
{
int i,j,k;
//memory allocation to store product
int **p;
p = malloc(r1 * sizeof(int *));
if(p == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < r1; i++)
{
p[i] = malloc(c2 * sizeof(int));
if(p[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
//product
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
p[i][j]=0;
for(k=0;k<r2;k++)
{
p[i][j]=p[i][j]+(m1[i][k]*m2[k][j]);
}
}
}
return p;
}
in the above matrix multiplication problem i find the product matrix and store the it in the memory location pointed by p. now i want to multiply each element in product by 50. i no longer need p but need only q. will it be ok if i declare q as
int **q;
without allocating separate memory for q??? will the elements of p in the corresponding location be replaced by elements of q???