How do you write a functions using pointers to multiply two matrices and return it to the calling function?

1 answer

Answer

1269894

2026-08-16 08:55

+ Follow

void main()

{

int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;

printf("Enter The Rows And Cloumns And Of The First Matrix:");

scanf("%d %d",&m,&n);

printf("\nEnter The Rows And Cloumns And Of The Second Matrix:");

scanf("%d %d",&p,&q);

printf("\nEnter Elements Of The First Matrix:\n");

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

{

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

{

scanf("%d",&a[i][j]);

}

}

printf("\nEnter Elements Of The Second Matrix:\n");

for(i=0;i< p;i++) {

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

scanf("%d",&b[i][j]);

}

printf("The First Matrix Is:\n"); /* Print the first matrix */

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

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

printf(" %d ",a[i][j]);

printf("\n");

}

printf("The Second Matrix Is:\n"); /* Print the second matrix */

for(i=0;i< p;i++) {

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

printf(" %d ",b[i][j]);

printf("\n");

}

if(n!=p) {

printf("Aborting./nMultiplication Of The Above Matrices Not Possible.");

exit(0);

}

else {

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

for(j=0;j< q;j++) {

c[i][j] = 0;

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

c[i][j] = c[i][j] + a[i][k] * b[k][j];

}

}

}

printf("\nThe Product Of The Two Matrices Is:\n\n");

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

for(j=0;j< q;j++) {

printf(" %d ",c[i][j]);

}

printf("\n");

}

}

return 0;

}

-----------------------------------------------------">-----------------------------------------------------

The above code is not the solution of Matrix multiplication using pointers.

the following code is absolutely correct.

void main()

{int *a,*b,*c,row1,col1,row2,col2;

clrscr();

printf("enter rows of 1at matrix");

scanf("%d",&row1);

printf("enter columns of 1at matrix");

scanf("%d",&col1);

printf("enter rows of 2nd matrix");

scanf("%d",&row2);

printf("enter columns of 2nd matrix");

scanf("%d",&col2);

if(col1!=row2)

{

printf("\nsorry\n");

}

else

{

a = (int *) malloc(row1*col1 * 2);

b = (int *) malloc(row2*col2 * 2);

c = (int *) malloc(col1*row2 * 2);

clrscr();

printf("enter 1st matrix \n");

insert(a,row1,col1);

getch();

clrscr();

printf("enter 2st matrix \n");

insert(b,row2,col2);

getch();

clrscr();

printf("1st matrix is\n");

display(a,row1,col1);

printf("\n2nd matrix is\n");

display(b,row2,col2);

prod(a,b,c,row1,col2);

printf("\nafter multiplication \n\n\n");

display(c,row1,col2);

}

getch();

}

insert(int *q,int r,int c)

{int i,j;

for(i=0;i

{

for(j=0;j

{ printf("\nEnter [%d][%d] element-- ",i,j);

scanf("%d",(q+i*c+j));

}

}

}

display(int *q,int r,int c)

{int i,j;

for(i=0;i

{

printf("\n");

for(j=0;j

{

printf("%d\t",*(q+i*c+j));

}

}

}

prod(int *p,int *q,int *z,int r1,int c2)

{int i,j,k;

for(i=0;i

{

for(j=0;j

{

*(z+i*c2+j)=0;

for(k=0;k

{

*(z+i*c2+j)+=*(p+k*2+j)*(*(q+i*c2+k));

}

}

}

}

//Designed by Asjad Farrukh.......

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.