Mnkisd
Mnkisd

Reputation: 512

Can't get the second winner of a race, I only can get the winner

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(void){
    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.

Upvotes: 1

Views: 72

Answers (1)

David Ranieri
David Ranieri

Reputation: 41036

Your logic is wrong, you need an initial value to compare, an example working (simplified with integers just to show the logic):

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

int main(void)
{
    srand((unsigned)time(NULL));

    int arr[8] = {0};

    for (size_t i = 0; i < 8; ++i)
    {
        arr[i] = rand() % 100;
        printf("%d\n", arr[i]);
    }
    puts("--");

    int num1 = 0;
    int num2 = 0;
    int time1 = 100;
    int time2 = 100;

    for (int i = 0; i < 8; ++i)
    {
        if (arr[i] < time1)
        {
            time1 = arr[i];
            num1 = i;
        }
        if ((arr[i] < time2) && (arr[i] > time1))
        {
            time2 = arr[i];
            num2 = i;
        }
    }
    printf("id: %d value %d\n", num1, arr[num1]);
    printf("id: %d value %d\n", num2, arr[num2]);
}

Notice that the standard library can help in this case, qsort is your friend, less variables, less code, and less prone to errors:

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

typedef struct
{
    int id;
    float time;
} runner;

static int comp(const void *pa, const void *pb)
{
    const runner *a = pa;
    const runner *b = pb;

    return a->time < b->time ? -1 : a->time > b->time;
}

int main(void)
{
    runner runners[8] = {{0, .0f}};

    for (size_t i = 0; i < 8; ++i)
    {
        printf("\nWrite id player: ");
        scanf("%d", &runners[i].id);
        printf("\nWrite time: ");
        scanf("%f", &runners[i].time);
    }
    qsort(runners, 8, sizeof(runner), comp);
    printf("\n\nWinner: %d, with time: %f", runners[0].id, runners[0].time);
    printf("\n\nSecond: %d, with time: %f", runners[1].id, runners[1].time);
    printf("\n\n");
}

Upvotes: 2

Related Questions