I'm working on a horse race. So in my program, a random number is produced. This random number can be 1 through 100. If the random number is 1-35, then horse one will be the winner. If the random number is 36-47, then horse two will be the winner. It goes on like this for nine horses, each with different odds of winning. I then have to save the winner of each race in an array. So say that, race 1, horse 2 wins. So the array will say 2-1. Race 2, horse three wins, so then it will be 2-1, 3-1, so horse 2 has won once, and horse 3 has won once. Then race 3, horse 2 wins again. So then it would be 2-2, 3-1. How would I do this in an array?

I'd use an array where the index represents the horse and the value stored at that index is the number of wins:

int wins[9] = {0};

// Simulate 100 races with each horse having equal probability of winning
for (int i = 0; i < 100; i++) {
    ++wins[rand() % 9];
}

// Display the formatted results
puts("Horse\tWins");

for (int i = 0; i < 9; i++) {
    printf("%d\t%d\n", i + 1, wins[i]);
}

Yes, but each horse has a different odd of winning.

Yes, but each horse has a different odd of winning.

That's completely irrelevant to how you store the number of wins.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.