In: Computer Science
The Durban July is an annual horse race that has been presented every year since 1897. Write a C/C++ program to handle registrations for the race. The program must meet the following specifications:
Create a structure (struct) to save the following information for each horse in the race:
Name - up to 20 characters;
Age - Age in years (integer);
Height - The height of the horse in hands (integer); and
Time that the horse achieved in the last race in seconds (real value).
Create an array of structures (structs) that are used to save the information for four horses. The user types in the information for each horse.
After the horse's information is stored in the array, the schedule for the race must be displayed based on the contents of the array. Iterate through the array and display the information on one line for each horse.
The horse with the best time for the previous race is named as the favorite. Iterate through the array, identify the horse with the best time and show it at the bottom of the schedule. Assume that each horse's time will be unique.
C++ Code:
#include<iostream>
#include<string>
#include <cstring>
#include <climits>
using namespace std;
struct Horse //structure of Horse type
{
char name[20];
int age;
int height;
int time;
};
void input(Horse &obj) //taking details from user
{
cout<<"ENTER NAME : ";
cin.sync(); //clearing buffer
cin.getline(obj.name,20);
cout<<"ENTER AGE : ";
cin>>obj.age;
cout<<"ENTER HEIGHT : ";
cin>>obj.height;
cout<<"ENTER TIME IN PREVIOUS RACE : ";
cin>>obj.time;
}
void output(Horse obj) //Printing details from user
{
cout<<"NAME : ";
cout<<obj.name;
cout<<" | AGE : ";
cout<<obj.age;
cout<<" | HEIGHT : ";
cout<<obj.height;
cout<<" | TIME IN PREVIOUS RACE : ";
cout<<obj.time<<endl;
}
void Max_Display(Horse obj[]) //finding best horse according to previous race
{
Horse Max;
int ma=INT_MAX;
for(int i=0;i<4;i++)
{
if(obj[i].time<ma)
{
ma=obj[i].time;
Max=obj[i];
}
}
cout<<"favorite HORSE ACCORDING TO PREVIOUS RACE : ";
strcpy(Max.name,"favorite");
output(Max);
}
int main()
{
Horse arr[4];
for(int i=0;i<4;i++)
{
cout<<"ENTER DETAILS OF HORSE "<<i+1<<" : \n";
input(arr[i]); //input details
cout<<"-----------------------------"<<endl;
output(arr[i]); //printing details
cout<<"-----------------------------"<<endl;
}
Max_Display(arr); //finding Best Horse
return 0;
}
Output:
If you have any doubt feel free to ask and if you like the answer please upvote it .
Thanks