Problem: Your friend the biology lab assistant needs to calculate the average ti
ID: 3593155 • Letter: P
Question
Problem:
Your friend the biology lab assistant needs to calculate the average time it takes each of his rats to run through a given maze. For three days he runs each rat through the maze once, and records the amount of time it takes. Write a C++ program that will, after the test runs are all complete, calculate the average time it took for each of his rats to run the maze, and print a report to a file.
Input: The user should be prompted to input the name and 3 maze times for each rat. The name may have spaces in it. The three maze times will be positive integers. After the user has input the data for each rat, your program should ask the user if they have data for another rat. If they type “Y” or “y”, your program should allow them to input data for another rat.
Processing: Compute the average maze time for each of the rats by computing the mean of their 3 maze times.
Output: The program should output a lab report to a file named “lab_report.txt”. The first row of the report should be a list of column headers that label the data to be output. This should be followed by a row for each rat that lists their name, each of the three maze times, and the average. This should be followed by the word “Smart” if the average is less than 30. If it is 30 or above, it should be followed by the word “Normal”. The final average should be formatted to 1 decimal place. The numerical data in each column should line up under its column header.
Name
Black Fred 28 40
Lima Bean 28 31
Master Splinter 25 27
Additional Requirements:
•
For full credit, you must use a for-loop to input the three maze times.
Time 1 Time 2 Time 3 Avg Rating
29 32.3 Normal
25 28.0 Smart
39 30.3 Normal
For full credit, your program should work for any number of rats (not only three).
For full credit, your program should allow spaces in the rat names. You will receive a small point deduction if your program works only for rats without spaces in their names. Hint: use cin >> ws; immediately before getline.
Hint: set up your output file and output the column headers BEFORE the loop that processes the rat names and times.
Hint: output the data to the file immediately after it is input from the user, or computed by your program.
All of the output data should be LEFT justified for this assignment.
The name should be displayed in a width of 20 characters, the numerical data
should be displayed in a width of 7 characters each.
Note that the column headers may be output as one long string with spaces
embedded in the proper places.
Use proper data types (only use float and double when necessary). Your program must compile and run, otherwise you will receive a 0.
Style:
See the Style Guidelines document on the course website. The grader will deduct points if your program violates the style guidelines.
Explanation / Answer
#include<iostream> // header file for i/p o/p functions
#include <iomanip> // header file setting float point precision
#include <fstream> // header file for file printing
using namespace std; // using namespace
int main(){ // main function
char name[10][20],ch; // character array and a character declarations
int race1[10],race2[10],race3[10],i=0,n=0; // integer arrays for races and required integers declarations
float mean[10]; // float array to find mean of races
ofstream fd; // file stream
do{ // do while loop
cout<<"Enter "<<i+1<<" rat name: "; // asking rat name
cin>>ws; // with spaces
cin.getline(name[i], 20); // getting name
cout<<"Enter "<<i+1<<" rat "<<1<<" race time: "; // first race
cin>>race1[i];
cout<<"Enter "<<i+1<<" rat "<<2<<" race time: "; // second race
cin>>race2[i];
cout<<"Enter "<<i+1<<" rat "<<3<<" race time: "; // third race
cin>>race3[i];
cout<<"Want to enter data for another rat (y/n): "; // asking whether to enter another data or not
cin>>ch;
i++; // increase i for next iterartions
n++; // increase n for couting how many rats data is entered
}while(ch=='y'||ch=='Y'); // untill n/N is entered
for(i=0;i<n;i++) // loop to calculate mean for 3 races
mean[i]=((float)race1[i]+(float)race2[i]+(float)race3[i])/n;
fd.open("lab_report.txt"); // opens the file
if(!fd) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
fd<<"Name Time1 Time2 Time3 Avg Rating"<<endl; // header column
for (i=0; i<n;i++){ // values
fd<<fixed<<setprecision(1); // setting precision to 1 for float values
fd<<name[i]<<" "<<race1[i]<<" "<<race2[i]<<" "<<race1[i]<<" "<<mean[i]<<" ";
if(mean[i]>30) // rating decider
fd<<"Normal"<<endl;
else
fd<<"Smart"<<endl;
}
fd.close(); // closing file
return 0;
} // end of main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.