Answer:
C++ code is given below with appropriate comments
Step-by-step explanation:
#include<iostream>
#include<cstdlib>
using namespace std;
// takes in the horses int array and its size
// for each horse perform a toss
void toss(int horses[],int horse_count){
for(int i=0; i<horse_count;i++){
// if the toss result is 1 we advance the horse
// if the result is zero we dont advance
bool advance= rand()%2==1;
if(advance)horses[i]++;
}
}
//takes in the horses int array and its size
// returns true if there is a winner
// else returns false
bool isWinner(int horses[], int horse_count){
for(int i=0; i<horse_count;i++){
if(horses[i]>=15){
cout<<"Horse "<<i<<" wins!"<<endl;
return true;
}
}
return false;
}
// takes in the horses int array and its size
// prints the horses with the track
int print(int horses[], int horse_count){
for(int i=0;i<horse_count;i++){
for(int track=1; track<horses[i];track++)cout<<".";
cout<<i;
for(int track=horses[i]+1;track<=15;track++)cout<<".";
cout<<endl;
}
cout<<endl;
cout<<endl;
}
int main(){
unsigned int seed;
cout<<"Please enter a random seed: ";cin>>seed;
srand(seed);
const int HORSES_COUNT=5;
// we have taken an int array
// the numbers are all set to 1
// if the toss turns 1 we increment the value for that horse by 1
// we keep on incrementing until the number is 15
int horses[HORSES_COUNT]{1};
// repeat the loop until there is a winner
while(!isWinner(horses,HORSES_COUNT)){
toss(horses,HORSES_COUNT);
print(horses,HORSES_COUNT);
}
}