-1
In this code I should read the number of grades of 3 students compare with a template of 3 questions and check how many points the students made, but only the grade of the first student appears.
Follow the code below:
#include <stdio.h>
#include <stdlib.h>
#define tamanho 3
#define tamanho2 3
#define tamanhoVetor 3
int main()
{
int matrizQuestoes [tamanho][tamanho2];
int vetorGabarito [tamanhoVetor];
int pontosAlunos [tamanhoVetor];
int numeroAlunos [tamanho];
int contador=1, soma=0;
for(int i=1; i<=tamanho; i++)
{
for(int j=1; j<=tamanho2; j++)
{
printf("Digite a nota do aluno %d \n",contador);
scanf("%d",&matrizQuestoes[i][j]);
fflush(stdin);
if(j%5==0)
{
contador+=1;
}
}
}
/*Vetor para o Gabarito*/
for(int i=1;i<=tamanhoVetor;i++)
{
printf("Digite as respostas do Gabarito \n");
scanf("%d",&vetorGabarito[i]);
fflush(stdin);
}
/*Compara a Resposta com o gabarito*/
for(int i=1;i<=tamanho2;i++)
{
pontosAlunos[i]=0;
for(int j=1;j<=tamanho2;j++)
{
if(matrizQuestoes[i][j] == vetorGabarito[i])
{
pontosAlunos[i]+=1;
}
}
}
/*mOSTRA A QUANTIDADE PONTOS*/
for(int i=1;i<=tamanho2;i++)
{
printf("Pontos:%d\n",pontosAlunos[i]);
}
return 0;
}
Thank you so much for the answers, I didn’t really pay attention to i imagined that since he counted the lines then I would compare everything, but when I used the index with 1 I thought that because I did the com to show the order of the students then I should continue with him, but as you said then I must put size+1?
– José Augusto Valim
No.When Voce declares an array of N positions
int lista[N];
, each position of this array is accessed by a number of 0 ateh N-1 (in your case, 0 ateh 2). That is, the first position of the array is accessed by type commandlista[0] = 10
and the last one is accessed by type commandlista[N-1] = 10
. That is, as in your case the arrays have three positions,vetorGabarito[0]
,vetorGabarito[1]
,vetorGabarito[2]
are valid indices, butvetorGabarito[3]
eh eh. The same goes formatrizQuestoes
andpontosAlunos
.– Leafar
In general, if Voce is going through all positions of a Voce array it should use the lac
for
as follows:for(int i = 0; i < tamanho; i++)
. Note, in the first iteration oi
will be0
, that is the input of the first position of the array, and in the last iteration i will be equal totamanho-1
, which is the last position of the array.– Leafar