Matrix as parameter passage in C

Asked

Viewed 47 times

1

Hello, my doubt is the following, I would like to know how I do in the code below, so that my matrix is read inside the main, and after inside the subprogram, a vector receives the values of this matrix and and and displays the matrix read?

#include <stdio.h>

#define tam 4


void exibirresultado(int provNot[][3]);

int main(){
     int provNot [tam][3];
     int i,j;

     printf("\n #########################################\n");

            for(i=1; i<=tam; i++){
            printf("Digite a nota do  %d aluno:\n",i);
                for(j=0; j<3; j++){
                    printf("Prova %d :\n",j+1);
                    scanf("%d",&provNot[i][j]); 
            }
        }
            exibirresultado(provNot);

    }   
    void exibirresultado(int provNot[][3]){
        int i,j,teste[tam][3];
                for(i=1; i<=tam; i++){
                    for(j=0;i<3;j++){
                        teste[i][j]=provNot[i][j];
                    }
                }
                    for(i=1; i<=tam; i++){
                    for(j=0;i<3;j++){
                        printf("%d === %d",i, teste[i][j]);
                    }
                }
    }

2 answers

1

Good night. To display the content contained in the array addresses you can use this code:

int matriz[LINHA][COLUNA] = {{1,2,3}, {4,5,6}, {7,8,9}};
int i, j;
for(i = 0; i < LINHA; i++)
 for(j = 0; j < COLUNA; j++)
  printf("%d \n", matriz[i][j]);

1

Arrays are indexed in 0, so the interactions stay:

for(i=0; i<tam; i++)

The index for the evidence (j), to iterate on them, always make comparison with the total evidence, no i:

for(j=0; j<3; j++)

Improve your code:

A #define tam 4 It’s not very intuitive. Seek to use names that resemble what they store/contain:

#define TOTAL_PROFS 4
#define QUANT_PROVAS 3

int notas_alunos [TOTAL_PROFS][QUANT_PROVAS];

Avoid using variable names such as i, especially when one is learning, because they are easily confused, and it is necessary to understand what they do there.

Browser other questions tagged

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