I'm new to programming and I'm trying to make a C language program that multiplies a 3x3 array by its transpose and prints the result . I'm having trouble creating the algorithm of multiplication between the matrix by its transpose. Any help will be gladly given.
Follow the code:
libraries used ( 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 ( which is logically wrong, I know ). I was unable to create a pattern (i and j) for multiplication, which could be implemented as the minimum use of for
commands 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 1st multiplication of line 1 by the transpose **i0j0*i0j0 + i0j1*i1j0 + i0j2*i2j0**
and that in the 2nd multiplication of line 1 by the transpose **i0j0*i0j1 + i0j1*i1j1 + i0j2*i2j1**
and that in the 3rd multiplication of line 1 by the transpose **i0j0*i0j2 + i0j1*i1j2 + i0j2*i2j2**
and then
in the first multiplication of line 2 by the transpose **i1j0*i0j0 + i1j1*i1j0 + i1j2*i2j0**
in the 2nd multiplication of line 2 by the transpose **i1j0*i0j1 + i1j1*i1j1 + i1j2*i2j1**
in the 3rd multiplication of line 2 by the transpose **i1j0*i0j2 + i1j1*i1j2 + i1j2*i2j2**
and last
in the first multiplication of line 3 by the transpose **i2j0*i0j0 + i2j1*i1j0 + i2j2*i2j0**
in the 2nd multiplication of line 3 by the transpose **i2j0*i0j1 + i2j1*i1j1 + i2j2*i2j1**
in the 3rd multiplication of line 3 by the transpose **i2j0*i0j2 + i2j1*i1j2 + i2j2*i2j2**
An example, if the array ( mat[i][j]
) entered by the user is 10, 0, 1, 3, 5, 2, -4, 2, 3. The result array ( mat_resultado
) will have the following output numbers: 101, 32, -37, 32, 38, 4, -37, 4, 29.