Question
Please write out code in C language. The code should produce the following output using the given times in the problem. Total time: 2 hours
Please write out code in C language. The code should produce the following output using the given times in the problem.
Total time: 2 hours 8 minutes 20 seconds
Time per mile: 15 minutes 45 seconds
I have provided my code below: The total is correct but the time per mile out is wrong.
* This program should produce the total in hours, minutes and seconds for the total time rounded to the nearest second. The program should also produce the average time per mile for the given data set, also displayed in hours, minutes and seconds. */
int round_to_int (float value);
/* Given: a positive float value
* Returns: the value rounded to the nearest integer (rounding up)
*/
#include
int main(void)
{
/* Declare variables */
int count = 0;
int total_mins = 0;
float total_secs = 0;
int m;
float s;
/* While there are more times */
while (fscanf(stdin,"%d %f", &m, &s) != EOF)
{
count += 1;
total_mins += m;
total_secs += s;
}
/* Calculate total time */
float total_time = total_mins + (total_secs / 60);
/* Calculate total time in hours */
int hours = total_time / 60;
total_time -= hours * 60;
/* Calculate total minutes */
int mins = total_time;
total_time -= mins;
/* Calcultae total seconds */
int secs = 60 * total_time;
/* Print the total time in hours, minutes, and seconds */
printf("The total time is %d hours %d mins %d secs. ", hours, mins, secs);
/* Calculate average time for each mile */
float avgTime = total_mins + (total_secs / 60);
/* Calculate total time */
total_time = (avgTime/8.15);
/* Calculate total hours */
hours = total_time / 60;
total_time -= hours * 60;
/* Calculate total minutes */
mins = total_time;
total_time -= mins;
/* Calculate total seconds */
secs = 60 * total_time;
/* Print the average time for each mile */
printf("Time per mile %d hours %d mins %d secs. ", hours, mins, secs);
}
/* Initialize function */
int round_to_int (float value)
{
/* Initialize variables */
float time_rounded, b;
/* rounded value */
int a;
a = value;
b = a + 0.5;
/* Loop test */
while (value
{
/* Return to converted value */
return a;
break;
}
/* Loop test */
while (value >= b)
{
/* Loop body and update */
a = a + 1;
/* Return to converted value */
return a;
break;
}
/* Return */
return a;
}
Someone I know walked (not a runner) the 8.15 miles of the Great Aloha Run last week and kept track of his mile times on his Apple Watch during the race. Instead of waiting for the official" chip-time results, he would like to calculate his total elapsed time and average time per mile from the data collected and is asking you to write the program to process his data. He can download the mile data from the Apple Watch to a file which can be redirected as input to your program. Given the following time in minuts and seconds per segment 14 19.2 14 34.2 15 10.8 15 48.4 15 54.4 16 25.1 16 50.8 19 17.2 The program should produce the total time (in hours, minutes and seconds, rounded to the nearest second) and time per mile (in minutes and seconds similarly rounded). You can assume your round.c file from homework 1 will be linked with this code, so you may use the round.to int() function implemented there. You do not need to write that here. For the above data, the results would be: Total time: 2 hours 8 mins 20 sec Time per mile: 15 mins 45 sec Your task is to write this program using the 5-step design process
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started