# Thread: Problem - Matrix Program

1. ## Problem - Matrix Program

Hi, everyone. I am trying to write a program that read two matrices (one in each function), and then calculates the product between the first and the second (in a function designed for that purpose only), and displays the result in the main function. However, when I compile the program, I get errors, and I believe I am making mistakes when I return each matrix to the main function, and also when I pass each matrix to a function, for example, matrices 1 and 2 to the function product(). Am I right, or my mistakes are far from that? Thanks in advance!

Code:
```#include <stdio.h>
double product (double matrix1[2][2], double matrix2[2][2], double matrix3[2][2]);

int main (void)
{

double matrix1[2][2], matrix2[2][2], matrix3[2][2];
int i, j;

matrix3 = product(matrix1, matrix2);

printf("The product of the two matrices yields:\n");

for (i = 0 ; i < 2 ; i++)
{
for (j = 0 ; j < 2 ; j++)
{
printf("%f  ", matrix3[i][j]);
}
printf("\n");
}

return 0;

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

{

int i, j;

printf("Input of matrix 1 elements.\n");

for (i = 0 ; i < 2 ; i++)
{
for (j = 0 ; j < 2 ; j++)
{
printf("Type in the element [%d][%d]: ", i, j);
scanf("%lf", &matrix1[i][j]);
}
}

return matrix1[2][2];

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

{

int i, j;

printf("Input of matrix 2 elements.\n");

for (i = 0 ; i < 2 ; i++)
{
for (j = 0 ; j < 2 ; j++)
{
printf("Type in the element [%d][%d]: ", i, j);
scanf("%lf", &matrix2[i][j]);
}
}

return matrix2[2][2];

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

double product(double matrix1[2][2], double matrix2[2][2], double matrix3[2][2])
{

int i, j, k;
double sum, total = 0;

for (i = 0 ; i < 2 ; i++)
{
for (j = 0 ; j < 2 ; j++)
{
for (k = 0 ; k < 2 ; k++)
{
sum = matrix1[i][k] * matrix2[k][j];
total = total + sum;
}
matrix3[i][j] = total;
sum = 0;
}
}

return matrix3[2][2];

}```

2. Step through your program, and you'll see the errors better.

In function product, you return the address of matrix3, and use it also. That doesn't agree with the function's declared return type, and that matrix was not a parameter for this function. You should be getting a compiler error. Please prototype your functions.

3. Found the errors, thanks! By the time you were answering my topic, I edited the code, but it still has errors here, but now I've fixed them.

4. Hello stdq ! You can refer C program for multiplication / product of two matrices. It gives the actual output that you want. Refer it.. I think, u will get the answer that you want..

5. Originally Posted by technoexam
Hello stdq ! You can refer C program for multiplication / product of two matrices. It gives the actual output that you want. Refer it.. I think, u will get the answer that you want..
Yeah... check it out... if you want an errored solution written for a compiler that's 20 years out of date.

Oh, and TechnoExam... we don't hand out free source code here.

There's an old saying: "Give a man a fish and you've fed him for a day. Teach a man to fish and you've fed his whole family for a lifetime".

What's that mean? It means that simple answers breed simpletons.