Mike A.
asked 02/08/21Matrix Multiplication in C programming
Given an n x k matrix A and an k x m matrix B, with 1≤n,m,k≤300, write a C program that computes the matrix product C=AB. All entries in matrices A and B are integers with abolute value less than 1000, so you don't need to worry about overflow.
If matrices A and B do not have the right dimensions to be multiplied, the product matrix C should have its number of rows and columns both set to zero.
Input/Output: please use scanf and printf to handle the data input and output. Input
format:
Line 1: Two space-separated integers, n and k.
Line 2 to n+1: Each line contains k integers separated by sapces: Each row of matrix A.
Line n+2: Two space-separated integers, k and m.
Line n+3 to n+k+4: Each line contains m integers separated by spaces: Each row of matrix B.
Sample Input:
3 2
1 1
1 2
-4 0
2 3
1 2 1
3 2 1
Output format:
Line 1: two space-separated n and m, the dimension of matrix C.
Line 2 to m+1: Each line contains m space-separated integers: Each row of matrix C.
Sample Output:
3 3
4 4 2
7 6 3
-4 -8 -4
please use 2D arrays and do not use a library for auto matrix multiplication.
1 Expert Answer
Patrick B. answered 02/08/21
Math and computer tutor/teacher
#include <stdio.h>
#define MAX_MATRIX_DIMENSION (100)
typedef struct _TMatrix
{
int numRows;
int numColumns;
double matrix[MAX_MATRIX_DIMENSION][MAX_MATRIX_DIMENSION];
} * TMatrix;
int Matrix_UserInput(TMatrix matrix)
{
int i,j;
double x;
printf(" Please input the # of rows :>");
scanf("%d",&matrix->numRows);
printf("Please input the # of columns :>");
scanf("%d",&matrix->numColumns);
for (i=0; i<matrix->numRows; i++)
{
for (j=0; j<matrix->numColumns; j++)
{
printf(" Please input the value of the matrix entry row %d and column %d :>",(i+1),(j+1));
scanf("%lf",&x);
matrix->matrix[i][j]=x;
}
}
}
int Matrix_ReadInput(char * filename,TMatrix matrix1, TMatrix matrix2)
{
int iReturn=0;
int rowLoop,columnLoop;
FILE * fptr = fopen(filename,"r");
if (fptr!=NULL)
{
//reads first matrix
fscanf(fptr,"%d %d",&matrix1->numRows,&matrix1->numColumns);
//debug
printf(" dimensions of first matrix: %d %d \n",matrix1->numRows,matrix1->numColumns);
for (rowLoop=0; rowLoop<matrix1->numRows; rowLoop++)
{
for (columnLoop=0; columnLoop<matrix1->numColumns; columnLoop++)
{
fscanf(fptr,"%lf",&matrix1->matrix[rowLoop][columnLoop]);
//debug
printf(" matrix1 : entry at %d %d is %lf \n",rowLoop,columnLoop,matrix1->matrix[rowLoop][columnLoop]);
}
fscanf(fptr,"\n"); //eats the newline
}
//reads second matrix
fscanf(fptr,"%d %d \n",&matrix2->numRows,&matrix2->numColumns);
printf(" dimensions of second matrix: %d %d \n",matrix2->numRows,matrix2->numColumns);
for (rowLoop=0; rowLoop<matrix2->numRows; rowLoop++)
{
for (columnLoop=0; columnLoop<matrix2->numColumns; columnLoop++)
{
fscanf(fptr,"%lf",&matrix2->matrix[rowLoop][columnLoop]);
//debug
printf(" matrix2 : entry at %d %d is %lf \n",rowLoop,columnLoop,matrix2->matrix[rowLoop][columnLoop]);
}
}
fclose(fptr);
}
else
{
iReturn=-1;
}
return(iReturn);
}
Matrix_Multiply(TMatrix matrix1, TMatrix matrix2, TMatrix matrixProduct)
{
int i,j,k;
//Enforces the rule: N by k times k by M = N by M
matrixProduct->numRows=matrix1->numRows;
matrixProduct->numColumns=matrix2->numColumns;
// Multiplying first and second matrices and storing it in result
for (i = 0; i < matrix1->numRows; ++i)
{
for (j = 0; j < matrix2->numColumns; ++j)
{
matrixProduct->matrix[i][j]=0;
for (k = 0; k < matrix1->numColumns; ++k)
{
matrixProduct->matrix[i][j] += matrix1->matrix[i][k] * matrix2->matrix[k][j];
}
printf(" matrix product at %d %d is %lf \n",i,j,matrixProduct->matrix[i][j]);
}
}
}
Matrix_WriteOutput(char * filename, TMatrix m)
{
int i,j;
int iReturn=0;
FILE * fptr = fopen(filename,"w");
if (fptr!=NULL)
{
fprintf(fptr,"%d %d \n",m->numRows,m->numColumns);
for (i=0; i<m->numRows; i++)
{
for (j=0; j<m->numColumns; j++)
{
fprintf(fptr,"%lf ",m->matrix[i][j]);
}
fprintf(fptr,"\n");
}
fclose(fptr);
}
else
{
iReturn=-1;
}
}
Matrix_Display(TMatrix matrix, char * strMessage)
{
int i,j;
if (strMessage!=NULL)
{
printf("*********************************************************\n");
printf(strMessage);
}
printf("****************************************************************\n");
printf(" The dimensions of this matrix is %d rows by %d columns \n\n",matrix->numRows,matrix->numColumns);
for (i=0; i<matrix->numRows; i++)
{
for (j=0; j<matrix->numColumns; j++)
{
printf("%6.0lf ",matrix->matrix[i][j]);
}
printf("\n");
}
}
int main()
{
struct _TMatrix matrix1;
struct _TMatrix matrix2;
struct _TMatrix matrixProduct;
printf("1st Matrix \n ------------------\n");
Matrix_UserInput(&matrix1);
printf("2nd Matrix \n--------------------\n");
Matrix_UserInput(&matrix2);
if (matrix1.numColumns!=matrix2.numRows)
{
//the sizes are incompatible: gripes and complains
printf(" Matrices do not have the proper compatible sizes to be multiplied \n");
printf(" # of columns of 1st matrix MUST EQUAL # of rows of 2nd matrix \n");
printf(" Formula is: N by k times k by M \n");
}
else
{
Matrix_Multiply(&matrix1,&matrix2,&matrixProduct);
//Matrix_WriteOutput((char*)"E:\\matrix_output.dat",&matrixProduct);
Matrix_Display(&matrixProduct,(char*)"The Product Matrix");
}
}
Mike A.
thank you but this code is way too complex and the inputs are basically user scan inputs not a file to read from as in the example, first input is 3 2 meaning that there will be a 3 rows and 2 col matrix then when you press enter you can enter the values in a matrix shape like 1 1 1 2 -4 0 and so the program knows that this is a completed matrix so then you can do the same for the second matrix02/08/21
Patrick B.
My apologies... here is a rewritten, revision, but with USER INPUT rather than file IO. The logic is not that much different.02/09/21
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
A couple of issues, but source code posted and uploaded to RESROUCES section under this link. (1) Sorry, 300 is too big for any data type. (2) You are more than welcome to change the data type from float to integer inside the matrix. Anything outside of multiplication/addition will require floats. But it works.02/08/21