Program does not count points in the correct way

Asked

Viewed 39 times

-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;
}

1 answer

1

In that if internal, you are comparing all responses of a student with the same content of the feedback, instead of comparing each response with a different content of the feedback:

 /*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;
         }
     }
 }

The correct would be to use the index j and not the index i in the array vetorGabarito:

 /*Compara a Resposta com o gabarito*/
 for(int i=0; i < tamanho2;i++)
 {
     pontosAlunos[i]=0;
     for(int j=0; j < tamanho2;j++)
     {
         if(matrizQuestoes[i][j] == vetorGabarito[j])
         {
             pontosAlunos[i]+=1;
         }
     }
 }

In addition, the C language (as well as almost all in fact) uses arrays starting at zero index. That is, an array of size 3 (e. g., int lista[3]) has positions 0, 1, and 2.

When going through an array using loop for, start at 0 and go to N-1 (where N is the size of the array), as I did in the example above. If not, you will be accessing an invalid memory position, which will most likely cause error at some point.

  • 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?

  • 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 command lista[0] = 10 and the last one is accessed by type command lista[N-1] = 10. That is, as in your case the arrays have three positions, vetorGabarito[0], vetorGabarito[1], vetorGabarito[2] are valid indices, but vetorGabarito[3] eh eh. The same goes for matrizQuestoes and pontosAlunos.

  • 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 o i will be 0, that is the input of the first position of the array, and in the last iteration i will be equal to tamanho-1, which is the last position of the array.

Browser other questions tagged

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