Question

In: Computer Science

PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT...

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

Solutions

Expert Solution

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...!!!


Related Solutions

PROGRAM LANGUAGE IN C, PLEASE KEEP IT SIMPLE EVEN IF IT IS REDUNDANT In this problem...
PROGRAM LANGUAGE IN C, PLEASE KEEP IT SIMPLE EVEN IF IT IS REDUNDANT In this problem you will take a list of names from the user and sort them alphabetically using selection sort. The code for selection sort for integer is available in the "Lab and Weekly Coding Solution module" in webcourses. Ask the user how many names the user wants to input. Let's say the number be N. Then take N number of names and put them in an...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
USE C++ and please keep program as simple as possible and also comment so it is...
USE C++ and please keep program as simple as possible and also comment so it is easy to understad Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class with public and private members and multiple constructors.  Gain a better understanding of the building and using of classes and objects.  Practice problem solving using OOP. Overview You will implement a date and day of week calculator for the SELECTED calendar year. The calculator repeatedly reads in three numbers from the standard input that are interpreted as month, day of month, days...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
Write a simple shell in C language and show some outputs.
Write a simple shell in C language and show some outputs.
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
Problem statement: You are tasked with writing a simple program that will keep track of items...
Problem statement: You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale). Requirements: The program will now be broken up into methods and store all the inventory in an ArrayList object. The program will be able to run a report for all inventory details as well as a report for items that are low in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT