write a program in c language for the multiplication of two
matrices using pointers?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
why return type of main is not necessary in linux
1234554321 1234 4321 123 321 12 21 1 1 12 21 123 321 1234 4321 1234554321
Write a program of prime number using recursion.
prog for 1st five prime numbers in 2^x - 1
Table of Sudoku n*n
i want to job in your company, so how it will be possible.
how does a general function , that accepts an array as a parameter, "knows" the size of the array ? How should it define it parameters list ?
Which of these statements are false w.r.t File Functions? i)fputs() ii)fdopen() iii)fgetpos() iv)ferror() A)ii B)i,ii C)iii D)iv
What are the preprocessors?
When you call malloc() to allocate memory for a local pointer, do you have to explicitly free() it?
how to set Nth bit of a variable?
Why & is used in c?