0
I’ve done a lot of different things...
for(i=0; i<=2; i++){
for(j=0; j<=3; j++){
O[i][j] = (3*M[i][j]) - (5*N[i][j]);
printf(" %3d ",O[i][j]);
}
printf("\n");
}
However the matrix does not print or print incomplete if not the result of the last line comes out with random values.
Below is the complete code:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int M[2][3], N[2][3], O[2][3], i,j;
void matriz (int matriz[][3], char name[1]){ //function para pedir o usuario o valor da matriz M e N
for(i=0; i<=2; i++){
for(j=0; j<=3; j++){
printf("Matriz %s: Linha [%d] Coluna [%d]: ",name,i+1,j+1);
scanf("%d",&matriz[i][j]);
}
}
}
void printMatriz (int matriz[][3], char name[1]){ //imprensão das matrizes M e N apenas para comparação dos resultados
printf("\n\n _______ Matriz %s _______\n",name);
for(i=0; i<=2; i++){
for(j=0; j<=3; j++){
printf(" %3d ",matriz[i][j]);
}
printf("\n");
}
}
main(){
setlocale (LC_ALL, "portuguese");
printf("Forneça os valores das Matrizes abaixo\n\n");
matriz(M,"M"); //chamada da function para matriz M e abaixo N
matriz(N,"N");
printMatriz(M,"M"); //impressão das matrizes para comparar com o resultado
printMatriz(N,"N");
//imprimir na tela a matriz O = 3*M - 5*N
printf("\n\n __ Matriz O = 3*M - 5*N __\n\n");
for(i=0; i<=2; i++){
for(j=0; j<=3; j++){
O[i][j] = (3*M[i][j]) - (5*N[i][j]);
// printf(" %3d ",O[i][j]);
}
// printf("\n");
}[![inserir a descrição da imagem aqui][1]][1]
printMatriz(O,"O");
}```
[1]: https://i.stack.imgur.com/CztMk.png
Note that your statement
char name[1]
is the same aschar name
, this is occupies 1 byte. Note that in the function call, e.g. inmatriz(M,"M");
, the parameter "M" is a 1 position string and therefore occupies 2 bytes, the character followed by the terminator character ' 0' and therefore you are invading memory area not properly allocated. Or declarechar name[2]
and continue using "M" or declarechar name
and start using’M'. In printfs exchange %s for %c.– anonimo
I made exactly these changes and printed everything once. However, the O matrix at position [1][3], [2][1] to [2][3] went wrong. I ran the program again and stopped printing the O matrix. I don’t understand why this happens. I ran it again using different numbers every time and nothing. The same procedure prints the matrix M and N, but does not print O. What is wrong? Is it the calculation of the matrix O? I use the same repetition system.
printf("\n\n __ Matriz O = 3*M - 5*N __");
 for(i=0; i<=2; i++){
 for(j=0; j<=3; j++){
 O[i][j] = (3*M[i][j]) - (5*N[i][j]); 
 }
 }
 printMatriz(O,'O');

– Leonardo Pinto Silva
Your commands
for
are considering one more position. Change the<=
for<
. For an array x[n] the index varies from 0 to n-1.– anonimo
I’m not getting it. The matrix has to have 3 rows and 4 columns
matriz[2][3]
if I replace<=
for<
the repetitionfor
goes from0 a 1
and after0 a 2
. If I putfor(i=0;<3;i++){for(j=0;j<4;J++){}}
to print the matrix 3 * 4 remains the same problem. The matrix O in position[1][3], [2][1] a [2][3]
, come out the value0,1, 2, 1, 0
. This means something, this error is always the same.– Leonardo Pinto Silva
I put the
matriz[3][4]
all thefor(i=0;<3;i++){for(j=0;j<4;J++)
and it was. Printed 3 rows and 4 columns. But the matrix doesn’t start counting from 0? That’s not wrong? If anyone can clarify thank you.– Leonardo Pinto Silva
You are confusing the array size with the index variation, the index part of 0. Ex. when you declare
int x[4];
The x array has 4 elements with indices ranging from 0 to 3.– anonimo