Why doesn’t the matrix print out all the typed values?

Asked

Viewed 76 times

3

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main()
{
setlocale(LC_ALL, "Portuguese");

int linha=0, coluna=0, i, j, l, m;
int matriz_A[linha][coluna], matriz_B[linha][coluna], matriz_C[linha] 
[coluna];
char escolha = '0';

printf("\n Qual operação com matrizes você quer fazer? ");
printf("\n (1) Adição");
printf("\n (2) Subtração");
printf("\n (3) Multiplicação \n");
scanf("%c%*[^\n]", &escolha);

if(escolha =='1')
{
    printf("\n Informe quantas linhas terá a matriz: ");
    scanf("%i", &linha);

    printf("\n Informe quantas colunas terá a matriz: ");
    scanf("%i", &coluna);

    printf("\n Informe os valores da matriz A: \n");
      for(i=0; i<linha; i++)
      {
          for(j=0; j<coluna; j++)
          {
              scanf("%d", &matriz_A[i][j]);
          }
      }

    printf("\n Informe os valores da matriz B: \n");
      for(l=0; l<linha; l++)
      {
          for(m=0; m<coluna; m++)
          {
              scanf("%d", &matriz_B[l][m]);
          }
      }

     printf("\n Abaixo temos a matriz A \n");
       for(i=0; i<linha; i++)
       {
           for(j=0; j<coluna; j++)
           {
             printf(" %d ", matriz_B[i][j]);
           }
          printf("\n");
       }


}

return 0;
}

Below the output when I put in the matrix A 2x2, the values 1234:

3 4
3 4

Why the program always prints and repeats only the last 2 numbers I type independent of the matrix order?

I noticed that this is fixed by changing the initialization value of the variables line and spine for any value greater than 0. But if I am assigning a value for both scanf() wasn’t that supposed to be fixed? Whenever I try to initialize both with no value they pick up memory junk.

  • Note that when you declare your matrices the row and column variables are zeroed. Declare them after reading these variables (which will not work in older compiler versions) or make dynamic memory allocation.

1 answer

7


I haven’t seen everything and it looks like the code isn’t even complete, but the biggest problem there is that it’s not reserving room for the matrix. See the line:

int matriz_A[linha][coluna], matriz_B[linha][coluna], matriz_C[linha][coluna];

How much is it worth linha and coluna at the time of this statement? 0, right? So let’s rewrite with the value instead of the variables.

int matriz_A[0][0], matriz_B[0][0], matriz_C[0][0];

I put in the Github for future reference.

You are reserving space for 3 matrices with zero size.

You probably want to create these matrices after asking the values of linha and coluna.

It even works more or less, but by sheer coincidence, and we have to understand what we’re doing and determine what’s right. For some reason people believe that if they have it executed and give the expected result the code is right, when in fact it only worked, by coincidence. When it doesn’t work, then one decides to see what is happening. So it was lucky this time because it didn’t work. The learning here is that you can’t trust what happens, but rather master what the code does at each point, in every tiny detail, even the blanks you used, if you don’t do this you’re not programming.

The error must have occurred by a mania that people have to declare all variables at the beginning of the application and not only when needed, if they were to look for the correct this type of error would not occur, this mistake I even understand, someone taught that it should be so 30 years ago, and at the time it could only be like this, but everything changed and people continued teaching like this, even if it was not a good way, the new form was created because it is better and should be taught by everyone, but it is not what happens, so I question the quality of what has been taught there.

  • the other part of the code and only printing of the matrix B (with the same problem) and the printing of the sum of the 2 matrices

Browser other questions tagged

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