Several errors in C code - How to fix?

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;
}
    
asked by anonymous 10.10.2015 / 02:39

2 answers

3

The code has several errors, from typing, to wrong logic reusing variables that must be separated by passing a 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);
    return 0;
}

See running on ideone .

If you are getting the size of the array by parameters, it is the one that should be used and not a fixed value. Worse still be 4 in the example, since the array is being created with 3 elements. Obviously the parameter can not be manipulated, it pays to lose its value. You have to create an auxiliary variable to increment at each step.

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

    
10.10.2015 / 03:22
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;
}

There is no need for the math library, as it is a simple entry.

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

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

You could even use while for both. It would be less line of code, but it has the detail of: o is when the end is known; the while is for non-countable repeating cases or an unknown end.

    
14.10.2015 / 16:37