Error: My matrix does not print the last line and sometimes only prints half

Asked

Viewed 66 times

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 as char name, this is occupies 1 byte. Note that in the function call, e.g. in matriz(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 declare char name[2] and continue using "M" or declare char name and start using’M'. In printfs exchange %s for %c.

  • 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 __");&#xA; for(i=0; i<=2; i++){&#xA; for(j=0; j<=3; j++){&#xA; O[i][j] = (3*M[i][j]) - (5*N[i][j]); &#xA; }&#xA; }&#xA; printMatriz(O,'O');&#xA;

  • Your commands for are considering one more position. Change the <= for <. For an array x[n] the index varies from 0 to n-1.

  • I’m not getting it. The matrix has to have 3 rows and 4 columns matriz[2][3] if I replace <= for < the repetition for goes from 0 a 1 and after 0 a 2. If I put for(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 value 0,1, 2, 1, 0. This means something, this error is always the same.

  • I put the matriz[3][4] all the for(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.

  • 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.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.