please can anyone fix my code. #include <stdio.h> void printMatrix (int row, int
ID: 3764606 • Letter: P
Question
please can anyone fix my code.
#include <stdio.h>
void printMatrix (int row, int col, float m[row] [col]);
float calcEntry(float x[], float y[]);
int main (void){
int A[50][50],B[50][50],r1, c1,r2, c2,row, col,i, j, k;
printf("Enter rows and columns for matrix1: ");
scanf("%d%d", &r1, &c1);
scanf("%d",&A[r1][c1]);
printf("Enter rows and columns for matrix2: ");
scanf("%d%d", &r2, &c2);
scanf("%d",&B[r2][c2]);
if (c1 != r2){
printf("Error!!!!! ");
printf("The column of matrix1 must be equal to the row of matrix2 ");
}
else {
printf("Please enter elements for the matrix1: ");
float matrix1[row][col];
for (i=0; i<r1; i++){
for (j=0;j<c1;j++){
printf ("Give me a location[%d][%d] ", i, j);
scanf("%f", &matrix1[i][j]);
}
}
printf("Please enter elements for matrix2: ");
float matrix2[row][col];
for (k=0; k<r2; k++){
for (j=0;j<c2;j++){
printf ("Give me a location[%d][%d] ",j,k);
scanf("%f", &matrix2[j][k]);
}
}
}
}
void printMatrix (int row, int col, float m[row] [col]){
int i,j;
for (i=1; i<row; i++){
for (j=1;j<col;j++){
printf("%.2f ",m[i] [j]);
}
printf(" ");
}
}
float calcEntry(float x[], float y[]){
float i,j,k;
float A,B;
float row,col,Sum;
float C[50][50];
for (i=0; i<row; i++){
for (j=0; j<col; j++){
for (k=0;k<row; k++){
C[0][0]=i*j;
printf(" The sum:");
scanf("%f",&C[i][j]);
}
}
return 0;
}
Explanation / Answer
I believe you are trying to multiply 2 matrices. If you, this is what the working code is. If not, just let me know, what exactly you are trying to do, so that I can solve the problem for you.
#include <stdio.h>
void printMatrix (int row, int col, float m[row] [col]);
float calcEntry(float x[], float y[]);
void printMatrix (int row, int col, float m[row][col]){
int i,j;
for (i=0; i<row; i++){
for (j=0;j<col;j++){
printf("%.2f ",m[i][j]);
}
printf(" ");
}
}
void MatrixMultiply(float x[50][50], float y[50][50], float z[50][50], int r1, int c1, int r2, int c2){
int i,j,k;
for (i=0; i<r1; i++){
for (j=0; j<c2; j++){
z[i][j] = 0;
for (k=0;k<c1; k++){
z[i][j] += x[i][k] * y[k][j];
}
}
}
}
int main (void){
int r1, c1,r2, c2,row, col,i, j;
//float matrix1[10][10], matrix2[10][10];
/*Reads the dimension of matrix1.*/
printf("Enter rows and columns for matrix1: ");
scanf("%d%d", &r1, &c1);
/*Reads the dimension of matrix2.*/
printf("Enter rows and columns for matrix2: ");
scanf("%d%d", &r2, &c2);
/*If the dimensions doesn't match, generate a warning message.*/
if (c1 != r2){
printf("Error!!!!! ");
printf("The column of matrix1 must be equal to the row of matrix2 for matrix multiplication ");
}
/*If all dimensions agree upon, start reading matrices.*/
else {
/*Read the elements into matrix1.*/
printf("Please enter elements for the matrix1: ");
float matrix1[r1][c1];
for (i=0; i<r1; i++){
for (j=0;j<c1;j++){
printf ("Give me a location[%d][%d] ", i, j);
scanf("%f", &matrix1[i][j]);
}
}
/*Read the elements into matrix2.*/
printf("Please enter elements for the matrix2: ");
float matrix2[r2][c2];
for (i=0; i<r2; i++){
for (j=0;j<c2;j++){
printf ("Give me a location[%d][%d] ",i,j);
scanf("%f", &matrix2[i][j]);
}
}
float matrix3[r1][c2];
printMatrix(r1, c1, matrix1);
printMatrix(r2, c2, matrix2);
MatrixMultiply(matrix1, matrix2, matrix3, r1, c1, r2, c2);
printMatrix(r1, c2, matrix3);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.