Assemble an Array of at most 10 x 10, which the user chooses the order in a function, and then assemble the matrix in another function

Asked

Viewed 18 times

-1

Write a program that reads an array (maximum 10 x 10). The order matrix is chosen by the user. Next use a function to:

a) Read integer values from matrix;

b) Show matrix values;

However, it is giving error, the matrix is not being assembled, or is generating random numbers...

#include <stdio.h>

int ler(int l, int c,int matriz[10][10]){                                     

    printf("Digite a quantidade de linhas da matriz:");
    scanf("%d", &l);
    printf("Digite a quantidade de colunas da matriz:");
    scanf("%d", &c);

    int l2 = l;
    int c2 = c;

    for(l = 0; l < l2; l++){
        for(c = 0; c < c2; c++){
            printf("Linha = [%d] Coluna = [%d] = ", l, c);
            scanf("%d", &matriz[l][c]);
        }
    }
}

int escrever(int *l, int *c, int matriz[*l][*c]){
    int l3, c3;

    l3 = *l;
    c3 = *c;

    printf("Matriz:\n");
    for(*l = 0; *l < l3; *l++){
        for(*c = 0; *c < c3; *c++){
            printf("%d \t", matriz[*l][*c]);
        }
        printf("\n");
    }
    printf("\n");
}


int main(){
   int matriz[10][10], l, c;

   ler(l, c, matriz);
   escrever(&l, &c, matriz);
}

1 answer

0

Some flaws and bad-Smells in the code:

  1. You need the values obtained in the function ler remain in the variables l and c in function main, then one needs to pass the reference of these variables.
  2. Already the function escrever do not need to change the values of the variables l and c.
  3. Check that input values do not exceed row and column limits.
  4. Perform the function escrever only if the function ler finish successfully.
  5. The parameter int matriz[*l][*c] does not work, this should be int matriz[10][10].
  6. Review the logic of loop count variables for.

See the code below for the reviews described above:

#include <stdio.h>

int ler(int *l, int *c,int matriz[10][10]){                                     

    printf("Digite a quantidade de linhas da matriz:");
    scanf("%d", l);
    printf("Digite a quantidade de colunas da matriz:");
    scanf("%d", c);

    int i, j;

    if (*l > 10 || *c > 10) {
        // Erro
        return -1;
    }

    for(i = 0; i < *l; ++i){
        for(j = 0; j < *c; ++j){
            printf("Linha = [%d] Coluna = [%d] = ", i, j);
            scanf("%d", &matriz[i][j]);
        }
    }

    return 0;
}

int escrever(int l, int c, int matriz[10][10]){
    if (l > 10 || c > 10) {
        // Erro
        return -1;
    }

    int i, j;

    printf("Matriz:\n");
    for(i = 0; i < l; i++){
        for(j = 0; j < c; j++){
            printf("%d \t", matriz[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    return 0;
}


int main(){
   int matriz[10][10], l, c;

   if (ler(&l, &c, matriz) == 0) {
      escrever(l, c, matriz);
   }

   return 0;
}

Browser other questions tagged

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