Checking test answers

Viewed 71

Im trying to build an algorithm (C language) for college that:

  1. Receives the right answers (line 10 - 14);
  2. Receives the students name (3 of them), and their answers (lines 15 - 22);
  3. Compares the right answers with those given by the students (line 24-28);
  4. Print on the screen the student name and how many questions he/she got right.

The code below is how I tried to do it. Entering (input):

1º (line 10 - 14)--> a, b, c, d, a;

2º (line 15 - 22)--> matheus, a, b, c, d, a //maria, a, a, a, a, a // marcos, c, c, c, c, c;

The problem starts here!

The output (printed on screem) is:

matheus: 1
maria: 1
marcos: 1

And not how many questions each one got right (as you can see, the first student should have 5 right questions, for example)! If somebody can help, it would be amazing!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){

  int i, j, aux;
  char name[3][20], g[5], r[5];

  printf("\t======= TEST TEMPLATE =======\n\n");
  for(i = 0; i < 5; i++){
    printf("\nTemplate question. %d: ", i+1);
    scanf("%s", &g[i]);
  }
 printf("\n\n\t======= TEST =======\n\n");
  for(j = 0; j < 3; j++){
    printf("\nStudent name: ");
    scanf("%s", name[j]);
    for(i = 0; i< 5; i++){
      printf("\nQ.%d: ", i+1);
      scanf("%s", &r[i]);
    }
  }
  for(j = 0; j < 3; j++){
    aux = 0;
    for(i = 0; i < 5; i++){
          if(g[i] == r[i]){
            aux++;
          }
        }
    }
    printf("\n\n\t======= ANSWARES =======\n\n");
    for(j = 0; j < 3; j++){
     printf("%s: %d\n", name[j], aux);
    }
return 0;
}

Code working! (variables now in portuguese)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){

  char name[3][20], g[5], r[3][5];

  printf("\t======= GABARITO DA PROVA =======\n\n");
  for(int i = 0; i < 5; i++){
    printf("\nGabarito da questão %d: ", i+1);
    scanf("%s", &g[i]);
    // printf("%c\n", g[i]);
  }
 printf("\n\n\t======= PROVA =======\n\n");
  for(int j = 0; j < 3; j++){
    printf("\nNome do Aluno: ");
    scanf("%s", name[j]);
    for(int i = 0; i< 5; i++){
      printf("\nQ.%d: ", i+1);
      scanf("%s", &r[j][i]);
      //printf("%c \n", r[j][i]);
    }
  }

  printf("\n\n\t======= RESPOSTAS =======\n\n");
  
  for(int j = 0; j < 3; j++){
    int aux = 0;
    
    for(int i = 0; i < 5; i++){
      //printf("%c - %c\n", g[i], r[j][i]);
      if(g[i] == r[j][i]){
        aux+=1;
        }
      }
    printf("%s: %d\n", name[j], aux);  

    }
    

    return 0;
}
1 Answers

You can use the following logic:

Read the right answers
For every student:
    Read the student name
    Zero the count of correct answers
    For every answer of this student
        Increment the count (if correct)
    Write the name and the count

If you don't want to intertwine the reads and the writes, you can use an intermediate array for the counts.

Read the right answers
For every student:
    Read the student name
    Zero the count of correct answers for this student
    For every answer of this student
        Increment the count for this student (if correct)
For every student:
    Write the name and the count
Related