please help me to fix my code. I\'m trying to write a C program that takes in tw
ID: 3764443 • Letter: P
Question
please help me to fix my code.
I'm trying to write a C program that takes in two matrices (A and B) and multiplies them, then outputs the result.
and I'm using this two functions:
void printMatrix(int row, int col, float m[row][col])
float calcEntry(float x[], float y[])
please i need the editing with comment.
This what I wrote:
#include <stdio.h>
void printMatrix(int row, int col, float m[row][col]);
float calcEntry(float x[], float y[]);
int main(voide)
{
int r1,r2,c1,c2,i,j,k;
float mult[10][10],A[10],B[10];
printf("Enter Row and Column for Matrix1:");
scanf("%d%d",&r1,&c1);
printMatrix(r1,c1);
printf("Emter Row and Column for Matrix2:");
scanf("%d%d",&r2,&c2);
printMatrix(r2,c2);
while (c1!=r2) {
printf("Error! Column of Matrix1 not equal to Rowof Matrix2: ");
printf(" please Enter Column and Row for Matrix1:");
scanf("%d%d",&r1,&c1);
printf("Enter Column ans Row for Mtrix2:");
scanf("%d%d",&r2,&c2);
}
printf(" Enter elements of matrix 1: ");
for (i=0; i<r1; ++i)
for (j=0; j<c1; ++j)
{
printf("Enter elements A%d%d:", i+1,j+1);
scanf("%f", &A[i][j]);
}
printf(" Enter elements of matrix 2: ");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements B%d%d: ",i+1,j+1);
scanf("%f",&B[i][j]);
}
for (i=0; i<r1; ++i)
for (j=0;j<c2;++j)
for(k=0;k<c1;++k)
{
mult[i][j]+=A[i][k]*B[k][j];
}
printf(" Output Matrix: ");
for(i=0;i<r1;++i)
for(j=0;j<c2;++j)
{
printf("%f", mult[i][j]);
if(j==c2-1)
printf(" ");
}
return 0;
}
Explanation / Answer
void printMatrix(int row, int col, float m[row][col])
float calcEntry(float x[], float y[])
please i need the editing with comment.
This what I wrote:
#include <stdio.h>
void printMatrix(int row, int col, float m[row][col]); % declaration of function to print matrix
float calcEntry(float x[], float y[]);% calculation of matrix
int main(voide)
{
int r1,r2,c1,c2,i,j,k;% initialization of variables
float mult[10][10],A[10],B[10]; % initialization of matrix
printf("Enter Row and Column for Matrix1:");
scanf("%d%d",&r1,&c1); % read the first matrix rows and columns
printMatrix(r1,c1);
printf("Emter Row and Column for Matrix2:");
scanf("%d%d",&r2,&c2); % read the second matrix rows and columns
printMatrix(r2,c2); %calling print matrix function
while (c1!=r2) {
printf("Error! Column of Matrix1 not equal to Rowof Matrix2: ");
printf(" please Enter Column and Row for Matrix1:");
scanf("%d%d",&r1,&c1);
printf("Enter Column ans Row for Mtrix2:");
scanf("%d%d",&r2,&c2);
}
printf(" Enter elements of matrix 1: ");
for (i=0; i<r1; ++i)
for (j=0; j<c1; ++j)
{
printf("Enter elements A%d%d:", i+1,j+1);
scanf("%f", &A[i][j]);
}
printf(" Enter elements of matrix 2: ");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements B%d%d: ",i+1,j+1);
scanf("%f",&B[i][j]);
}
for (i=0; i<r1; ++i)
for (j=0;j<c2;++j)
for(k=0;k<c1;++k)
{
mult[i][j]+=A[i][k]*B[k][j]; % multiplying matrix calculation
}
printf(" Output Matrix: ");
for(i=0;i<r1;++i)
for(j=0;j<c2;++j)
{
printf("%f", mult[i][j]);
if(j==c2-1)
printf(" ");
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.