First of all, sorry for the length of the code, I don't know how to copy and paste it without all of the extra \n's. It's supposed to be a program that receives two matrices of maximum dimensions 10x10, adds the matrices (if possible), and multiplies the matrices (if possible). I have the addition working quite well, but the multiplication is being a pain. I've defined N as 10, and use N in my matrices as parameters, but the multiplication only works if the matrices are NxN, and not if they are 3x3 for example. Any help would be appreciated!
Code:
#include
<stdio.h>
#define
N 10
int
main(void)
{
/* Declare variables. */
int i,j,NROWSA,NCOLSA,NROWSB,NCOLSB;
int mata[N][N],matb[N][N],matc[N][N],matd[N][N];
void matrixmult(int mata[N][N], int matb[N][N], int matc[N][N]);
void matrixadd(int mata[N][N], int matb[N][N], int matd[N][N]);
/* Receive input from keyboard. */
printf(
"Enter number of rows and number of columns for matrix A: \n");
scanf(
"%i %i",&NROWSA,&NCOLSA);
printf(
"Enter matrix A row by row \n");
for (i=0; i<=NROWSA-1; i++)
for (j=0; j<=NCOLSA-1; j++)
scanf(
"%i",&mata[i][j]);
for (i=0; i<=NROWSA-1; i++)
{
for (j=0; j<=NCOLSA-1; j++)
printf(
"%i ",mata[i][j]);
printf(
"\n");
}
printf(
"Enter number of rows and number of columns for matrix B: \n");
scanf(
"%i %i",&NROWSB,&NCOLSB);
printf(
"Enter matrix B row by row \n");
for (i=0; i<=NROWSB-1; i++)
for (j=0; j<=NCOLSB-1; j++)
scanf(
"%i",&matb[i][j]);
for (i=0; i<=NROWSB-1; i++)
{
for (j=0; j<=NCOLSB-1; j++)
printf(
"%i ",matb[i][j]);
printf(
"\n");
}
matrixmult(mata,matb,matc);
if (NCOLSA != NROWSB)
printf(
"Matrices cannot be multiplied. \n");
else
{
for (i=0; i<=NROWSA-1; i++)
{
for (j=0; j<=NCOLSB-1; j++)
printf(
"%i ",matc[i][j]);
printf(
"\n");
}
}
matrixadd(mata,matb,matd);
if (NROWSA != NROWSB || NCOLSA != NCOLSB)
printf(
"These matrices cannot be added. \n");
else
{
for (i=0; i<=NROWSA-1; i++)
{
for (j=0; j<=NCOLSA-1; j++)
printf(
"%i ",matd[i][j]);
printf(
"\n");
}
}
return 0;
}
/*--------------------------function matrixmult------------------------*/
/* This function multiplies two compatible matrices. The dimensions */
/* must be declared in the calling program as symbolic constants. */
/*---------------------------------------------------------------------*/
void
matrixmult(int mata[N][N], int matb[N][N], int matc[N][N])
{
/* Declare variables. */
int i,j,k;
/* Compute sums of products. */
for (i=0; i<=N-1; i++)
for (j=0; j<=N-1; j++)
{
matc[i][j] = 0;
for(k=0; k<=N-1; k++)
matc[i][j] += mata[i][k]*matb[k][j];
}
/* Void return. */
return;
}
/*---------------------------function matrixadd------------------------*/
/* This function adds two compatible matrices. The dimensions must be */
/* declared in the calling program as symbolic constants. */
/*---------------------------------------------------------------------*/
void
matrixadd(int mata[N][N], int matb[N][N], int matd[N][N])
{
/* Declare variables. */
int i,j;
/* Compute sums of products. */
for (i=0; i<=N-1; i++)
for (j=0; j<=N-1; j++)
matd[i][j]=mata[i][j] + matb[i][j];
/* Void return. */
return;
}
/*---------------------end of the function matrixadd-------------------*/