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...
Solve this question in C++ language. DO NOT use loops. Use recursive function. Keep the program...
Solve this question in C++ language. DO NOT use loops. Use recursive function. Keep the program simple. Q (5) Suppose you have been given the task to design a text editor which will take any multiline text from user and then display the statistics like total number of characters i.e., characters_count (excluding the white space and punctuations), words_count, and redundant_words_count. Create a structure named Text_Editor having four type members namely inserted_text (of type string), characters_count (of type unsigned int), words_count...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.    REQUIREMENTS: - Your...
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...
C++ Language Create Login Register program without open .txt file simple program also if you can...
C++ Language Create Login Register program without open .txt file simple program also if you can hide password input option menu 1) Login 2) Register 3) exit
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should prompt the user for the operation they wish to perform followed by the numbers they wish to operate on. You should have a function for each operation and use branches to determine which function to call. I need this to make any integers given, into decimal numbers, such as 3 to 3.0, or 2 to 2.0, also, so that I can multiply or add...
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...
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