CSE110 Summer Session I
Homework #4: Functions
Due Monday, June 18 2001 5pm
(Late homeworks accepted till Tuesday midnight)
NOTE: Please remember to provide relevant comments on all
programs
Also, name your files hw4p1.c, hw4p2.c, hw4p3.c
/html/courses/cse110/hw4p1 /html/courses/cse110/hw4p2 /html/courses/cse110/hw4p3
1. [30 points] Write a function ComputePay() that computes how much money someone makes in a day at work. The function should take three arguments: a double that is the person's hourly wage, a double that is the number of hours worked that day, and a bool that determines whether it is a weekday or not (true for weekday, false otherwise). On a weekday, people will be paid their hourly wage times the number of hours worked. On the weekends, people will be paid time and a half, that is, their hourly wage times number of hours times 1.5. Write a main function that asks the user for his hourly wage, number of hours worked, and whether or not it is a weekday. Print out how much money the user has made.
% a.out What is your hourly wage? 15.00 How many hours did you work? 8.0 Is today a weekday (y/n)? y You have made $120.00 today! % a.out What is your hourly wage? 10.00 How many hours did you work? 5.0 Is today a weekday (y/n)? n You have made $75.00 today!
2. [30 Points] Write a predicate function HasEvenDigit() that takes an integer argument (you may assume it is positive) and returns true if the integer has at least one even digit in its decimal representation and false otherwise. Also write a main function that prompts the user for an integer and then tells the user if the integer has an even digit by calling HasEvenDigit(). Hint: taking a look at the program in figure 4-5 on page 122 may help you with writing this program.
% a.out Enter an integer: 123456 123456 has an even digit % a.out Enter an integer: 13579 13579 does not have an even digit
3. [40 Points] Modify your food truck program from homework 3 problem 3 so that it is split between the three functions GetMenuItem(), ComputeSubtotal(), and GetMoney(). GetMenuItem() should display the menu, prompt the user to select an item, and then return the menuItem that was selected. ComputeSubtotal() should take in two variables: a menuItem and the current subtotal. The function should then prompt the user for the quantity of the item desired, compute the new subtotal for all the items selected thus far, and finally return the new subtotal value. GetMoney() should take in the total money owed as an argument, prompt the user for payment and make change. The main function should call these three functions appropriately so that the program runs the same way as it did in homework 3.
Note: As many of you noticed, the sample solution given for hw3 kept separate totals for cheesesteaks, hoagies, etc., so if 2 cheesesteaks were chosen, and then 3 cheesesteaks were chosen, the total cost would be 3 times the cost of a cheesesteak. While this was not required, some of you did it this way, and some didn't. Either was okay. For this problem, you can again do it either way, although you might find it easier to do it the more straightforward way, so that in the given example, the total would be 5 times the cost of a cheesesteak.
Your main code can look like this: (it doesn't have to, but it is one possibility)
void main()
{
double total=0;
menuItem item;
printf("Welcome to the LunchExpress food truck!\n");
while ((item =GetMenuItem()) != DONE_PURCHASING)
total = ComputeSubtotal(item, total);
GetMoney(total);
}