2
I am new to programming and I am trying to make a program in C language that multiplies a 3x3 matrix by transposing it and prints the result. I’m having a hard time creating a multiplication algorithm between the matrix and the transposta. Any help will be willingly.
Follows the code:
used libraries (stdio.h
and stdlib.h
)
int main(){
int mat[3][3], mat_transposta[3][3], mat_resultado[3][3];
int i, j;
// leitura matriz
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
scanf("%d",&mat[i][j]);
}
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
mat_transposta[i][j] = mat[j][i]; // transformação matriz principal p/ transposta (FUNCIONANDO)
}
}
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
mat[i][j] *= mat_transposta[i][j];// multiplicação da matriz pela sua transposta correspondente
}
}
for (i = 0; i < 3; i++){
for (j = 0; j < 3; j++){
printf("%d\t", mat_resultado[i][j]); // impressão matriz resultado (A SAÍDA TEM QUE SER NESSE FORMATO)
}
printf("\n");
}
return 0;
}
So the X of the question is to solve the following algorithm (that is logically wrong, I know). I could not create a pattern ( i and j) for multiplication, which could be implemented as the minimum use of commands for
to perform this multiplication.
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
mat[i][j] *= mat_transposta[i][j];// multiplicação da matriz pela sua transposta correspondente
}
}
I know that in the first multiplication of line 1 by transposta **i0j0*i0j0 + i0j1*i1j0 + i0j2*i2j0**
and in the second multiplication of line 1 by transpose **i0j0*i0j1 + i0j1*i1j1 + i0j2*i2j1**
and in the third multiplication of line 1 by transpose **i0j0*i0j2 + i0j1*i1j2 + i0j2*i2j2**
and then
in the first multiplication of line 2 by transpose **i1j0*i0j0 + i1j1*i1j0 + i1j2*i2j0**
in the second multiplication of line 2 by transpose **i1j0*i0j1 + i1j1*i1j1 + i1j2*i2j1**
in the third multiplication of line 2 by transpose **i1j0*i0j2 + i1j1*i1j2 + i1j2*i2j2**
and finally
in the first multiplication of line 3 by transpose **i2j0*i0j0 + i2j1*i1j0 + i2j2*i2j0**
in the second multiplication of line 3 by transpose **i2j0*i0j1 + i2j1*i1j1 + i2j2*i2j1**
in the 3rd multiplication of line 3 by transpose **i2j0*i0j2 + i2j1*i1j2 + i2j2*i2j2**
An example, if the matrix (mat[i][j]
) entered by the user for 10, 0, 1, 3, 5, 2, -4, 2, 3. The result matrix (mat_resultado
) will have at output the following numbers: 101, 32, -37, 32, 38, 4, -37, 4, 29.
You can’t multiply a matrix with less than 3
for
because, for each elementM[i][j]
, you have to add the elements of the linei
and the columnj
. But since you’re multiplying a matrix by transpose, you can simply make the product inside the linei
with thej
without materializing the transposta. Of course, we still can not do in-place, since we will lose the original values after the first modified cell, then you need two morefor
to copy the result to the original matrix...– Wtrmute