Several errors in code C - How to fix?

Asked

Viewed 166 times

0

#include <stdio.h>
#include <math.h>

void rebeber_matriz (int l,int c)
{
    int M[l][c];
    for (l=0;l<4;l++)
    {
        for (c=0;c<4;c++)
        {
            printf ("Insira o valor do numero da linha: ",l," e da coluna: ",c);
            scanf ("%d",&M[l][c]);
        }
    }
}

int main ()
{

    int l = 3;
    int c = 3;

    receber_matriz(l,c);


    return 0;
}
  • 2

    Welcome to Stack Overflow! Please explain the problem better, and if possible include a example of code that reproduces what is happening, because your question is not noticeable. See Help Center How to Ask.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

  • Your printf is incorrect. Take a look at the documentation of the function. Also, the function is with wrong name - rebeber_matriz

2 answers

4

The code has several errors, from typing, to wrong logic reusing variables that must be separated, passing by function call syntax error.

#include <stdio.h>

void receber_matriz(int l, int c) {
    int M[l][c];
    for (int linha = 0; linha < l ; linha++) {
        for (int coluna = 0; coluna < c; coluna++) {
            printf("\nInsira o valor do numero da linha: %d e da coluna: %d", linha, coluna);
            scanf("%d", &M[linha][coluna]);
        }
    }
}

int main() {
    int l = 3;
    int c = 3;
    receber_matriz(l, c);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you are receiving the matrix size by parameters, it should be used and not a fixed value. Worse still be 4 in the example, since the matrix is being created with 3 elements. Obviously the parameter cannot be manipulated, it goes up worth losing its value. You have to create an auxiliary variable to increment each step.

The printf() has its own syntax to pass arguments to the string to be printed.

  • Saved by the answer :)

0

#include <stdio.h>

void receber_matriz(l, c) {
    int M[l][c];
    for (l = 0; l < 4 ; l++) {
        for (c = 0; c < 4; c++) {
            printf("\nInsira o valor do numero da linha: %d e da coluna: %d", l, c);
            scanf("%d", &M[l][c]);
        }
    }
}

int main() {

    int l = 0;
    int c = 0;

   receber_matriz(l, c);

    return 0;
}

No math library required, as it is a simple input.

I understand that you can use the same function input variable to read from the keyboard in the matrix.

Variables can be initialized with 0, since they will enter the for the same way. Initialized with 0 would be the question of logic because programming is not just writing code. It could even have negative values, but it would not be ideal.

I could use a while for both of us. It would be shorter line of code, but has the detail of: the for is is when the end is known; the while is for noncountable repeat cases or unknown end.

  • 3

    Who uses l as a variable (suggestion: linha, lin, or li) is playing with fate :-)

  • @pmg teachers get taught like this. I don’t know why, but it doesn’t make sense. I think it can stimulate thinking variables name, methods, etc already at the beginning of learning.

Browser other questions tagged

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