Please can anyone tell me where am i going wrong in this code? | SoloLearn: Learn to code for FREE!

+5

Please can anyone tell me where am i going wrong in this code?

#include <stdio.h> #include <stdlib.h> void readMatrix(int m[10][10],int rows,int columns) { int i,j; for(i=0;i<rows;i++) { for(j=0;j<columns;j++) { printf("Enter the element matrix %d%d ",i+1,j+1); scanf(" %d",&m[i][j]); } printf("\n"); } } void printMatrix(int m[10][10],int rows,int columns) { int i,j; for(i=0;i<rows;i++) { for(j=0;j<columns;j++) { printf(" %d\t", m[i][j]); } printf("\n"); } } int main() { int i,j,r1,r2,c1,c2; int sum,k; int a[10][10],b[10][10],result[10][10]; printf("Enter the number of rows and columns in matrix A:\n"); scanf(" %d %d",&r1,&c1); readMatrix(a,r1,c1); printMatrix(a,r1,c1); printf("Enter the number of rows and columns in matrix B:\n"); scanf(" %d %d",&r2,&c2); readMatrix(b,r1,c1); printMatrix(b,r2,c2); if(r2==c1) { for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { sum=0; for(k=0;k<r2;k++){ sum = sum +a[i][k]*b[k][j]; } }result[i][j] = sum; } printf("The product of the matrix is :\n"); printMatrix(result,r1,c2); } else { printf("The product is not possible."); } return 0; }

9/29/2018 12:51:52 PM

Jayesh Bhojwani

3 Answers

New Answer

+6

While Entering Matrix B. function readMatrix(a,&r1,&c1); had an error in it should be as follows readMatrix(a,&r2,&c2); You need to place result [i][j]=sum; Inside second for loop i.e. for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { for(k=0;k<r2;k++) { sum=sum+a[i][k]*b[k][j] } result [i][j]=sum; } }

+2

put result[i][j]=sum; inside the }bracket😉

+2

compilator must said "variable 'j' is not declarated in this scope." this is your: for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { sum=0; for(k=0;k<r2;k++) { sum = sum +a[i][k]*b[k][j]; } } result[i][j] = sum; //here } and that must be: for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { sum=0; for(k=0;k<r2;k++) { sum = sum +a[i][k]*b[k][j]; } result[i][j] = sum; //here } }