In: Computer Science
PROGRAM LANGUAGE IN C NOT C# or C++
KEEP IT SIMPLE EVEN IF IT IS REDUNDANT PLEASE.
Problem: Driving
Function driving():
Write a function driving(), that updates the odometer and fuel gauge of a car. The function will take in a reference to the variables storing the odometer and fuel gauge readings, along with a double representing the miles per gallon (mpg) the car gets and the number of miles the driver intends to go. The function should update the odometer and fuel gauge readings, accordingly. If the number of miles is greater than the user can go with the fuel he has left, then simply have the car drive until empty and update the odometer accordingly. The prototype for the function is below:
void driving(double* odomPtr, double* fuelPtr, double mpg, double distance);
Note that, you are not allowed to change the prototype of the function.
The return type of the function has to be void.
Also, there should not be any printf() in the the driving function.
main function:
In the main function, at first take the odometer reading, fuel gauge reading, and miles per gallon (mpg) as input.
Then keep taking distance to drive as input until the fuel gauge indicates 0 fuel. After taking the distance to drive, call the driving function with the proper parameters to get update about odometer and fuel and display the update. Display them up to two decimal places.
At the end display the message "No more fuel!"
Sample Input/Output 1:
Enter odometer reading, fuel gauge reading and mpg: 8000 13.5 25
Enter the distance you want to drive: 200
Current reading Odometer = 8200.00, fuel = 5.50
Enter the distance you want to drive: 100
Current reading Odometer = 8300.00, fuel = 1.50
Enter the distance you want to drive: 500
Current reading Odometer = 8337.50, fuel = 0.00
No more fuel!
Sample Input/output 2:
Enter odometer reading, fuel gauge reading and mpg: 90000 2.5 13.5
Enter the distance you want to drive: 500
Current reading Odometer = 90033.75, fuel = 0.00
No more fuel!
Rubric: 3 points
Writing the function with the specification: 1.5 points [changing the specification will result in 0]
Writing the main function with the specification: 1.5 points
Penalty:
Not passing each test case: -0.75 (total -1.5 for 2 test cases)
Bad indented code: -0.75
Code:-
#include <stdio.h>
void driving(double* odomPtr, double* fuelPtr, double mpg, double
distance)
{
if(distance < *fuelPtr*mpg)
{
*odomPtr += distance;
*fuelPtr -= (distance/mpg);
}
else
{
*odomPtr += mpg * *fuelPtr;
*fuelPtr = 0;
}
}
int main()
{
double odomPtr, fuelPtr, mpg, distance;
printf("Enter odometer reading, fuel gauge reading and
mpg: ");
scanf("%lf %lf %lf", &odomPtr, &fuelPtr,
&mpg);
while(fuelPtr != 0)
{
printf("Enter the distance you want
to drive: ");
scanf("%lf", &distance);
driving(&odomPtr, &fuelPtr,
mpg, distance);
printf("Current reading Odometer =
%.2lf, fuel = %.2lf\n", odomPtr, fuelPtr);
}
printf("\nNo more fuel!\n");
return 0;
}
Code Screenshot:-
Outputs:-
Please UPVOTE thank you...!!!