write a program in c language for the multiplication of two
matrices using pointers?

Answers were Sorted based on User's Feedback



write a program in c language for the multiplication of two matrices using pointers?..

Answer / syed fakheruddin ahmad

main()
{
int mat1[3][3] = {1,2,3,4,5,6,7,8,9};
int mat2[3][3] = {1,2,3,4,5,6,7,8,9};
int res[3][3];
int i,j,k;
for (i=0; i<3; i++)
for (j=0; j<3; j++)
res[i][j] = 0;
for (i=0; i<3; i++)
for (j=0; j<3; j++)
for (k=0; k<3; k++)
*(*(res + i) + j) += (*(*(mat1 + i) + k)) * (*(*(mat2 + k)
+ j));

for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
printf ("%d\t", res[i][j]);
printf ("\n");
}
printf("\n");
}

Is This Answer Correct ?    100 Yes 45 No

write a program in c language for the multiplication of two matrices using pointers?..

Answer / ruchi

#include<stdio.h>
#include<conio.h>
int main()
{
int n,m,i,j,k;
int a[38][38],b[38][38],p[38][38];
printf("\nEnter the number of rows and coloumns ");
scanf("%d %d",&n,&m);
printf("\nEnter the elements of first matrix ");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",(*(a+i)+j));
}
}
printf("\nMatrix is ");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d",*(*(a+i)+j));
printf("\t");
}
}
printf("\nEnter the elements of second matrix ");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",(*(b+i)+j));
}
}
printf("\nMatrix is ");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d",*(*(b+i)+j));
printf("\t");
}
}
printf("\nAfter multiplication ");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
p[i][j]=0;
for(k=0;k<n;k++)
{
p[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d",*(*(p+i)+j));
printf("\t");
}
}


getch();
}

Is This Answer Correct ?    35 Yes 14 No

write a program in c language for the multiplication of two matrices using pointers?..

Answer / rohan

#include<stdio.h>
#define row 3
#define col 3
#define col2 3
int main()
{
int a[row][col],b[col][col2],c[row][col2],i,j,k;
printf("Enter the element of first matrix:->\n");
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the element of second matrix:->\n");
for(i=1;i<=col;i++)
{
for(j=1;j<=col2;j++)
{
scanf("%d",&b[i][j]);
}
}
/*printing of matrices*/
printf("\nMATRIX A\n");
for(i=1;i<=row;i++)
{
printf("\n");
for(j=1;j<=col;j++)
printf("%4d",a[i][j]);
}
printf("\nMATRIX B\n");
for(i=1;i<=col;i++)
{
printf("\n");
for(j=1;j<=col2;j++)
printf("%4d",b[i][j]);
}
/*multiplication*/
printf("\n\n");
for(i=1;i<=row;i++)
{
for(j=1;j<=col2;j++)
{
c[i][j]=0;
for(k=1;k<=col;k++)
c[i][j]=(c[i][j])+(a[i][k])*(b[k][j]);
}

}
/*printig of multiplication result*/
printf("MULTIPLICATION OF MATRIX A AND MATRIX B");
for(i=1;i<=row;i++)
{
printf("\n");
for(j=1;j<=col2;j++)
{
printf("%4d",c[i][j]);
}
}
printf("\n\n");
return 0;
}

Is This Answer Correct ?    7 Yes 7 No

write a program in c language for the multiplication of two matrices using pointers?..

Answer / asjad farrukh

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<r;i++)
{
for(j=0;j<c;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<r;i++)
{
printf("\n");
for(j=0;j<c;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<r1;i++)
{
for(j=0;j<c2;j++)
{
*(z+i*c2+j)=0;
for(k=0;k<r1;k++)
{
*(z+i*c2+j)+=*(p+k*2+j)*(*(q+i*c2+k));
}
}

}
}

Is This Answer Correct ?    7 Yes 10 No

write a program in c language for the multiplication of two matrices using pointers?..

Answer / arum kumar mishra

9

Is This Answer Correct ?    22 Yes 28 No

write a program in c language for the multiplication of two matrices using pointers?..

Answer / tutu

#include<stdio.h>
#include<conio.h>
int main()
{
int mul(int,int);
int n1,n2,f=1,ans;
while(f==1)
{
printf("\n***MULTIPLICATION OF TWO NATURAL
NUMBERS***");
printf("\nEnter Two Numbers: ");
scanf("%d %d",&n1,&n2);
if(n1<=0||n2<=0)
{
printf("\nInvalid Choice...");
printf("\nDo You Want To Try
Again...\n1.Yes\n2.No");
scanf("%d",&f);
}
else
{
printf("\nAns = %d",mul(n1,n2),"\n");
printf("\nDo You Want To
Continue:\n1.Yes\n2.No\n");
scanf("%d",&f);
}
}
}
int mul(int a,int b)
{
return((b-1)*a+a);
}

Is This Answer Correct ?    19 Yes 26 No

write a program in c language for the multiplication of two matrices using pointers?..

Answer / shankey narang

// write a program to multipication of a matrix
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int *ptr1,*ptr2,*ptr3;
int m,n,i,j,k;
printf("enter m & n=");
scanf("%d%d",&m,&n);
ptr1=(int*)malloc((m*n)*sizeof(int));
ptr2=(int*)malloc((m*n)*sizeof(int));
ptr3=(int*)malloc((m*n)*sizeof(int));
printf("enter elements of 1st matrix=");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",((ptr1+i)+j)) ;
}
}
printf("enter elements of 2nd matrix=");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",((ptr2+i)+j)) ;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
*((ptr3+i)+j)=0;
for(k=0;k<n;k++)
{
*((ptr3+i)+j)=*((ptr3+i)+j)+(*((ptr1+i)+j))*(*((ptr2+j)+k));
}
}
}
printf("multipication is=\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",*((ptr3+i)+j)) ;
}
printf("\n");
}
getch();
}

Is This Answer Correct ?    13 Yes 21 No

write a program in c language for the multiplication of two matrices using pointers?..

Answer / sharanya

#include<stdio.h>
#include<conio.h>
#define row 50
#define column 50
void main()
{
int A[row][column],B[row][column],r1,r2,c1,c2,i,j;
void add(int A[row][column],int B[row][column],int c1,int r1);
clrscr();
printf("Enter Row and Column Sizes of A,B : ");
scanf("%d%d%d%d",&r1,&r2,&c1,&c2);
printf("Enter Elements Of A : ");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("Enter Elements of B : ");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&B[i][j]);
}
}
if(r1==r2 && c1==c2);
else
printf("Addition is Not Possible.");
if(r2==c1)
{
multi(A,B,r1,r2,c2);
}
else
printf("Multiplication is Not Possible.");
}

void add(int A[row][column],int B[row][column],int r1,int c1)
{
int i,j;
printf("Addition Of Two Matrices: \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\t%d",A[i][j]+B[i][j]);
}
print("\n");
}
}

void multi(int A[row][column],int B[row][column],int r1,int r2,int c2)
{
int C[row][column],i,j,k;
printf("Multiplication of Two Matrices : \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
C[i][j]=0;
for(k=0;k<r2;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
printf("\t%d",C[i][j]);
}
printf("\n");
}
}

Is This Answer Correct ?    4 Yes 18 No

Post New Answer

More C Interview Questions

plz answer....A program that takes 3 variables e.g a,b,c in as seperate parameters and rotates the values stored so that value goes a to b, b to c and c to a .

3 Answers  


What are different types of operators?

0 Answers  


How to swap two values using a single variable ? condition: Not to use Array and Pointer ?

6 Answers  


How do you search data in a data file using random access method?

0 Answers  


difference between object file and executable file

0 Answers  






How can I read a directory in a c program?

1 Answers   CSC,


Total of how many functions are available in c?

3 Answers  


C program code int zap(int n) { if(n<=1)then zap=1; else zap=zap(n-3)+zap(n-1); } then the call zap(6) gives the values of zap [a] 8 [b] 9 [c] 6 [d] 12 [e] 15

6 Answers   TCS, Wipro,


How to print all the 26 alphabets in this order in C. AbCdEfGh..... it should print dynamically from a to z and do not print this using pgm like this print("Ab......"); Use loops or anything to print all alphabets

2 Answers   Hexaware,


program for validity of triangle from 3 side

7 Answers  


the maximum length of a character constant can be a) 1 character b) 8 characters c) 256 chaacters d) 125 characters

0 Answers  


In c programming write a program that will print 10 multiples of 3 except 15,18,21 using looping

0 Answers  


Categories