0

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?

2
Contributors
3
Replies
6
Views
4 Years
Discussion Span
Last Post by deceptikon
1

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]);
}
1

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.