This is a C programming problem. Below is the file from exercise F: Exercise G:
ID: 3753841 • Letter: T
Question
This is a C programming problem.
Below is the file from exercise F:
Exercise G: Writing a complete program What to Do The program you will write here will be a simple utility for a long-distance runner The input will be a distance and a time, and the output will be the average times taken per kilometre and per mile. Write a program that asks a user for a distance in kilometres, as a double, and a time interval, as an int and a double: a number of minutes, and a number of seconds. If the program succeeds in reading all three numbers, it should report the output specified above Here is a sample of a successful dialogue with the program, with slanted typewriter font used to show what was typed by a user:Explanation / Answer
Solution:
#include <stdio.h>
#include <stdlib.h>
int get_int_or_die(void);
// REQUIRES:
// User has been prompted to enter an int.
// PROMISES:
// Function tries to read an int using scanf and "%d"
// On success, that int is echoed to the user,
// and the int is the function return value.
// On failure, and error message is printed and
// exit is called with an argument of 1.
double get_double_or_die(void);
// Like get_int_or_die, but tries to read a double using "%lf".
//Function prototype of get_input
void get_input(double *km_in, int *minutes_in, double *seconds_in);
int main(void)
{
//Declaring variables to store respective inputs
int i, minutes_in;
double d, km_in, seconds_in ;
//Sample input checking
printf("Testing get_int_or_die. Please enter a line of text ... ");
i = get_int_or_die();
printf("get int_or_die returned a value of %d. ", i);
printf("Testing get_double_or_die. Please enter a line of text ... ");
d = get_double_or_die();
printf("get get_double_or_die returned a value of %lf. ", d);
get_input(&km_in,&minutes_in,&seconds_in);
//Suppose km is 10 and min is 39 and sec is 47.2
// Calculating totals seconds
double totalSeconds = (minutes_in * 60) + seconds_in;//Total Seconds will be 2387.2
//Calculating average km_in
double averageSeconds = totalSeconds/km_in; //Average second is totalsecons/total kms i.e 238.72
double averageMins = averageSeconds/60; //Average min will min/seconds i.e 3.97866
int averageMin = (int)averageMins; //Storing int part only i.e 3
double averageSec = (averageMins-averageMin)*60; //Now calculating seconds i.e 0.97866 * 60
printf("Average time for 1 Km: %d minute(s), %lf second(s). ", averageMin, averageSec);
double miles = km_in * 0.621371; //1Km = 0.621371
//Calculating average tootal seconds for miles
averageSeconds = totalSeconds/miles;
averageMins = averageSeconds/60;
averageMin = (int)averageMins;
averageSec = (averageMins-averageMin)*60;
printf("Average time for 1 Km: %d minute(s), %lf second(s). ", averageMin, averageSec);
return 0;
}
//Taking integer input and showing error message
//if user doesn't type int value
int get_int_or_die(void)
{
int result;
if (1 != scanf("%d", &result)) {
//Showing error message here
printf("I could not read an int. I am quitting. ");
exit(1);
}
//Showing typed int here
printf("I read an int value of %d. ", result);
return result;
}
//Taking double input and showing error message
//if user doesn't type double value
double get_double_or_die(void)
{
double result;
if (1 != scanf("%lf", &result)) {
//Showing error message here
printf("No! You can't take me! I could not read a double. I am quitting. ");
exit(1);
}
//Showing typed double here
printf("I read an double value of %lf. ", result);
return result;
}
//this helps us to take input from the user for value km min and seconds_in
//Parameters are passed by reference
void get_input(double *km_in, int *minutes_in, double *seconds_in){
// Taking km input
printf("Please enter a distance in km, using type double ");
*km_in = get_double_or_die();
//Taking minute input
printf("Please enter a number of minute, using type int. ");
*minutes_in = get_int_or_die();
//Taking seconds input
printf("Please enter a number of seconds, using type double. ");
*seconds_in = get_double_or_die();
}
Sample Run:
Testing get_int_or_die. Please enter a line of text ...
3
I read an int value of 3.
get int_or_die returned a value of 3.
Testing get_double_or_die. Please enter a line of text ...
45.6
I read an double value of 45.600000.
get get_double_or_die returned a value of 45.600000.
Please enter a distance in km, using type double
10
I read an double value of 10.000000.
Please enter a number of minute, using type int.
39
I read an int value of 39.
Please enter a number of seconds, using type double.
47.2
I read an double value of 47.200000.
Average time for 1 Km: 3 minute(s), 58.720000 second(s).
Average time for 1 Km: 6 minute(s), 24.182719 second(s).
Note: If you have any doubt, please let me know before giving any negative feedback, i will be glad to help you out thanks!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.