-3
I have a question I need:
- Read the proof feedback;
- Read the students' names;
- Read each student’s answers.
At the end of the program I must print: the name of the student + how many questions he got right.
In my input I put:
- Template ('test template'): a, b, c, d, a;
- Students and their responses: Matheus, a, b, c, d, a; // maria, a, a, a, a, a; // marcos, c, c, c, c, c, c;
The problem is in output. I should print the names and how many questions each one hit (not which ones!), but I get the following one::
matheus: 1
maria: 1
marcos: 1
Follows my code:
#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;
}
You are counting the hits on the variable aux, but for each student the variable is reset, so just print the hits of the last student
– Bernardo Lopes
Even with this exchange follows with the following error: Matheus: -1384471575 maria: -1384471575 marcos: -1384471575
– Baianator
the vector
r[5]
has 5 positions, only stores 5 of the 15 responses(are 5 responses for each student. 3* 5=15), and is being overwritten– Bernardo Lopes