the slogan is as follows: Program a function that receives an integer matrix, m rows by n columns as input. the function must return the producer of the elements of the matrix
I have a problem in the following function to calculate the product of a matrix in C:
int multiplicar(int mat[2][2]){
int resultado;
int i,j;
for ( i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
resultado = 0;
{
resultado += mat[i][j] * mat[j][i];
}
}
}
return resultado;
}
I tried it with the numbers 1 2 3 4 the correct result is 24 but it returns 16 to me if someone knows where my error is.
If you want the product of all the elements in the array, you have to do a multiplication.
resultado = 0
you are resetting the product to 0 at all times. You're not supposed to do that.resultado += mat[i][j] * mat[j][i]
you are adding. You want a product, not a sum.The code should look like this:
Notice that
resultado
it starts at 1 because it is the neutral element of the sum.