TASK: We were asked to write a program for the problem described below. Use func
ID: 3621993 • Letter: T
Question
TASK: We were asked to write a program for the problem described below. Use functions to organize the task. The functions should maximize reuse of code. Document your prototypes using contract-style comments stating what must be true before calling the function and what is true after the function has run (i.e. Needs/Constraints & Results statements). Pick appropriate functions names such that your code is self-documenting. Pass information to the functions by value where possible and by reference or array reference where appropriate. Protect arguments with the const modifier where possible. Be sure to explicitly close your files. Global variables are unacceptable except for the second dimension (columns) of any 2D char array or 2D numeric array. Should you wish you may use a struct to organize your data.PROBLEM:
Problem:
1. Read the data from the given file: raceF2010.txt into memory.
2. Calculate each racer's combined racetime which is the sum of their best red race + their best blue race.
3. Rank the racers based on the combined racetime.
4. Calculate how many seconds slower they are than the winner.
5. Write the complete data for each racer, in ranked order, to a new file.
You have a file containing the results of a ski race, race.txt . Each racer was identified with a Bib number and raced twice in the blue race course and twice in the red race course. A racer's combined time is their fastest blue-race + their fastest red-race. The fastest(lowest) total time will be the winner of the evening's race event. Rank the racers.
The datafile datatypes will conform to the following format:
char[]
char[]
char[]
int char[] int char double double double double
int char[] int char double double double double
int char[] int char double double double double
...
...
...
int char[] int char double double double double
int char[] int char double double double double
EOF
The first to third lines are char[]'s and can be considered to be lines of titles.
From the fourth line on:
the first int is the bib#;
the char[] is the racer's first and last name;
next is the int with the racer's age;
then the char for the racer's gender;
the first double is the first blue racecourse time; the second double is the first red racecourse time;
the third double is the second blue racecourse time, and the fourth double is the second red racecourse time.
N.B. Get the raceF2010.txt file from:
/usr/local/www/users/library/engr/ENGR1233/F2010
Any race time that has the value 999 is one that the racer did not finish (DNF).
PROCEDURE:
Code your program piecemeal:
i. Write one function prototype. Write needs/results documentation.
ii. Write code in the main() which will call it.
iii. Write the function stub.
iv. Write the function instructions.
v. Try the function and be sure that it works correctly.
vi. Repeat until you have all the functions working.
vii. Desk-check your race so that you know who the winner is in the race described in race.txt
Explanation / Answer
Hope this helps. Let me know if you have any questions. Please rate. :) #include #include #include using namespace std; struct racer { int bib; string name; int age; char gender; double b1, b2, r1, r2; double totalTime; }; /** * Reads in a single racer from the given ifstream parameter * Postcondition: returns a pointer to a new racer filled in with data - bib#, name, age, gender, totalTime **/ racer * readRacer(ifstream &inFile); /** * Reads in all racers from the file "raceF2010.txt" into the racers array * Precondition: ifstream parameter must be at the first racer data - titles must already have been read * Postcondition: racers[] is filled in with data for each racer- bib#, name, age, gender, totalTime * Returns the total number of racers read into the array **/ int readRacers(racer ** &racers, ifstream &inFile); /** * Resize the racer array to hold double its current capacity * This will happen in case we run out of space in the current array * Postcondition: The olde racers array is destroyed and should not be accessed until reassigned **/ racer ** resize(racer * racers[], int curSize); /** * Sorts all racers in the racers[] by the total time member * Uses a simple version of bubble sort **/ void sortRacersByTotalTime(racer * racers[], int size); /** * Outputs all racer data to the data file "RaceOutput.txt" **/ void printToFile(racer * racers[], int size, string title1, string title2, string title3); int main() { racer ** racers = NULL; int numRacers; string title1, title2, title3; ifstream inFile("raceF2010.txt"); if (!inFile.is_open()) { cout bib = atol(temp.c_str()); getline(inFile, r->name, ' '); inFile >> r->age; inFile >> r->gender; inFile >> r->b1 >> r->r1 >> r->b2 >> r->r2; if (r->b1 b2) bRace = r->b1; else bRace = r->b2; if (r->r1 r2) rRace = r->r1; else rRace = r->r2; r->totalTime = bRace + rRace; return r; } int readRacers(racer ** &racers, ifstream &inFile) { int count = -1; int size = 256; racers = new racer*[size]; while (!inFile.eof()) { count++; if (count >= size) { racers = resize(racers, size); size *= 2; } racers[count] = readRacer(inFile); } return count; } racer ** resize(racer * racers[], int curSize) { int newSize = curSize * 2; racer ** temp = new racer*[newSize]; for (int i = 0; i totalTime) { temp = racers[i]; racers[i] = racers[j]; racers[j] = temp; } } } } void printToFile(racer * racers[], int size, string title1, string title2, string title3) { ofstream outFile("RaceOutput.txt"); outFileRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.