Download the attched file, you will note that there are three lines of numbers.
ID: 3550392 • Letter: D
Question
Download the attched file, you will note that there are three lines of numbers. These numbers represent the amount of hours, minutes, and seconds that three marathon runners have completed a face in. For instance, if the line reads
3 20 33 the runner finished in 3 hours 20 minutes and 33 seconds.
Write a program that reads in each line from the file and converts it to the total number of seconds run. The program should then average the three times. Write this average number of seconds to an output file named Runners.txt. For instance if the files read:
3 20 33
2 45 12
3 50 34
The average number of seconds is 11926 (runner one took 12033 seconds, runner two 9912 seconds , the third runner 13834 seconds. The total of all of these is 11926) . Therefore the output file should read Seconds: 11926
Explanation / Answer
// average is 13437
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// open the file
ifstream infile("test.txt");
if (!infile.is_open())
{
cout << "Failed to open the file ";
return 0;
}
// read data
int numLines = 0;
int totalSeconds = 0;
int hour, minu, sec;
int avg;
while (infile >> hour >> minu >> sec)
{
numLines++;
totalSeconds += hour * 3600 + minu * 60 + sec;
}
// output the result
ofstream outfile("Runners.txt");
avg = totalSeconds / numLines;
cout << avg;
outfile << avg;
outfile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.