This code attempts to find the winner of a race and the second position by taking the player who put less time to complete the race.
#include <stdio.h>
int main(){
unsigned int choice;
int id[8] = {0};
float time[8] = {0};
for(size_t i=0; i<8; ++i) {
printf("\nWrite id player: ");
scanf("%d", &(id[i]));
printf("\nWrite time: ");
scanf("%f", &(time[i]));
}
size_t num1 = 0;
size_t num2 = 0;
for(size_t j=0; j<8; ++j){
if (time[num1]>time[j]){
num1 = j; // to find the winner
}
if (time[num2]>time[j] && time[num2]>time[num1]){
num2 = j; // to find the second position player
}
}
printf("\n\nWinner: %d, with time: %f", id[num1], time[num1]);
printf("\n\nSecond: %d, with time: %f", id[num2], time[num2]);
printf("\n\n");
}
Can't get why this doesn't work, it works only for the winner, but doesn't work for the second position player.
Output:
Write id player: 1838
Write time: 4
Write id player: 3492
Write time: 6
Write id player: 3219
Write time: 2
Write id player: 48929
Write time: 9
Write id player: 38928
Write time: 4
Write id player: 23892
Write time: 4
Write id player: 89498
Write time: 1
Write id player: 48929
Write time: 6
Winner: 89498, with time: 1.000000
Second: 89498, with time: 1.000000
Shouldn't the time[num2]>time[j] && time[num2]>time[num1] condition do the job? Shouldn't the &&make sure that the time of the second winner is yes minor of the j-th, but also greater than the winner.
Aucun commentaire:
Enregistrer un commentaire