Homework 4: A Simple Function

Due: Tuesday 10/4 by 1pm

Objectives

For this homework assignment you will

The Assignment

For this homework, you will write a function mpg() that computes the miles per gallon achieved by your vehicle. Your mpg() function takes three input values (a.k.a. formal parameters): the beginning odometer reading (an int value), the ending odometer reading (another int value) and the amount of gasoline added (a float value). The output of your mpg() function (a.k.a., the return value) is the gas mileage of your vehicle reported in miles per gallon (a float).

Thus, the function prototype of your mpg() should be:

float mpg(int beginMiles, int endMiles, float gallons) ;

Note: do not interchange float and double for this assignment.

Your mpg() function should not print anything to the screen (except for debugging purposes) and should not ask the user for input. The input to the function is its parameters. The output is the return value.

Write a main program that tests your mpg() function. You main program should use printf() to prompt the user for the data (beginning miles, ending miles and amount of gas) and use scanf() to read in these values from the user. Then, your main program should call your own mpg() function to calculate the gas mileage and the main program should print out the result.

Make sure that your main() function itself has return type int and that its last statement is returns 0.

int main() {

   ...

   return 0 ;
}

Then you can compile using gcc with the -Wall option. If the gcc compiler reports a warning or an error, then there is a problem with your program. For full credit, your program must compile without any warnings or errors when compiled with the -Wall option.

Name your program mpg.c. Use the script command to record yourself compiling the program using the -Wall option and testing your program in several sample runs. (Remember to exit from script when you are done.) Finally, submit your program and sample runs using:

submit cs104_chang hw04 mpg.c 
submit cs104_chang hw04 typescript


Be sure to logout completely when you have finished!