not getting proper output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

not getting proper output

I hv written a program to multiply 2 matrices but i m not getting the correct output due to some logical error. code link: https://code.sololearn.com/cJfMHJ1ff6Aw #include<stdio.h> int main() { int i, j,m,n,p,k; int a[10][10],b[10][10],c[10][10]; printf("\n Please Enter Number of rows and columns : "); scanf("%d %d", &m, &n); printf("\n Please Enter Number of rows and columns : "); scanf("%d %d", &p, &k); if(m!=k) { printf("Product not possible!"); } printf("\n Please Enter the Matrix Elements \n"); for(i = 0; i <= m-1; i++) { for(j = 0;j <= n-1;j++) { scanf("%d", &a[i][j]); } } printf("\n Please Enter the Matrix Elements \n"); for(i = 0; i <= p - 1; i++) { for(j = 0;j <= k-1;j++) { scanf("%d", &b[i][j]); } } for(i = 0; i <= m-1; i++) { for(j = 0; j<= p-1;j++) { for(k=0;k<=n-1;k++) { c[i][j]=0; c[i][j]= c[i][j]+a[i][k]*b[k][j]; } } } printf("product of 2 matrices \n"); for(i = 0; i <= m- 1; i++) { for(j = 0;j <= p-1;j++) { printf("%d\t",c[i][j]); } printf("\n"); } return 0; }

9th Jul 2022, 10:58 AM
Zane Vijay Falcao
Zane Vijay Falcao - avatar
1 Answer
+ 1
Your code is: for(i = 0; i <= m-1; i++) { for(j = 0; j<= p-1;j++) { for(k=0;k<=n-1;k++) Prroperly code should loops - In outer loop rows of matrix1. - Next loop columns of matrix2. - Last Inner loop columns of matrix1. And the line c[i][j]=0; has to be cancelled in above mentioned inner loop.
9th Jul 2022, 12:29 PM
JaScript
JaScript - avatar