Certain Cs Professor Gives

1. Explain the following patterns in your own words.

(a) simple decision

(b) two-way decision

(c) multi-way decision

2. The following is a (silly) decision structure.

a, b, c = input('Enter three numbers: ') if a > b:

print "Spam Please!" else:

print "It's a late parrot!" elif b > c:

print "Cheddar" elif a < b:

print "Swiss"

else:

print "Chestnut" else:

print "Larch" print "Done"

Show the output that would result from each of the following possible inputs.

3. Many companies pay time-and-a-half for any hours worked above 40 in a given week. Write a program to input the number of hours worked and the hourly rate and calculate the total wages for the week.

4. A certain CS professor gives 5-point quizzes that are graded on the scale 5-A, 4-B, 3-C, 2-D, 1-F, 0-F. Write a program that accepts a quiz score as an input and uses a decision structure to calculate the corresponding grade.

5. A certain CS professor gives 100-point exams that are graded on the scale 90-100:A, 80-89:B, 70-79:C, 60-69:D, <60:F. Write a program that accepts an exam score as input and uses a decision structure to calculate the corresponding grade.

6. A certain college classifies students according to credits earned. A student with less than 7 credits is a Freshman. At least 7 credits are required to be a Sophomore, 16 to be a Junior and 26 to be classified as a Senior. Write a program that calculates class standing from the number of credits earned.

7. The body mass index (BMI) is calculated as a person's weight (in pounds) times 720, divided by the square of the person's height (in inches). A BMI in the range 19-25, inclusive, is considered healthy. Write a program that calculates a person's BMI and prints a message telling whether they are above, within or below the healthy range.

8. The speeding ticket fine policy in Podunksville is $50 plus $5 for each mph over the limit plus a penalty of $200 for any speed over 90 mph. Write a program that accepts a speed limit and a clocked speed and either prints a message indicating the speed was legal or prints the amount of the fine, if the speed is illegal.

9. A babysitter charges $2.50 an hour until 9:00 PM when the rate drops to $1.75 an hour (the children are in bed). Write a program that accepts a starting time and ending time in hours and minutes and calculates the total babysitting bill. You may assume that the starting and ending times are in a single 24 hour period. Partial hours should be appropriately prorated.

10. A person is eligible to be a US senator if they are at least 30 years old and have been a US citizen for at least 9 years. To be a US representative these numbers are 25 and 7, respectively. Write a program that accepts a person's age and years of citizenship as input and outputs their eligibility for the Senate and House.

11. A formula for computing Easter in the years 1982-2048, inclusive, is as follows: let a — year%19, b = year%4, c = year%7, d = (19a+ 24)%30, e= (26+ 4c+ 6d+ 5)%7. The date of Easter is March 22 d e (which could be in April). Write a program that inputs a year, verifies that it is in the proper range and then prints out the date of Easter that year.

12. The formula for Easter in the previous problem works for every year in the range 1900-2099 except for 1954, 1981, 2049 and 2076. For these 4 years it produces a date that is one week too late. Modify the above program to work for the entire range 1900-2099.

13. A year is a leap year if it is divisible by 4, unless it is a century year that is not divisible by 400. (1800 and 1900 are not leap years while 1600 and 2000 are.) Write a program that calculates whether a year is a leap year.

14. Write a program that accepts a date in the form month/day/year and outputs whether or not the date is valid. For example 5/24/1962 is valid, but 9/31/2000 is not. (September has only 30 days.)

15. The days of the year are often numbered from 1 to through 365 (or 366). This number can be computed in three steps using int arithmetic:

(b) if the month is after February subtract (4month+ 23)/10

(c) if it's a leap year and after February 29, add 1

Write a program that accepts a date as month/day/year, verifies that it is a valid date (see previous problem) and then calculates the corresponding day number.

16. Take a favorite programming problem from a previous chapter and add decisions and/or exception handling as required to make it truly robust (will not crash on any inputs).

17. Archery Scorer. Write a program that draws an archery target (see exercises from Chapter 5) and allows the user to click 5 times to represent arrows shot at the target. Using 5-band scoring, a bulls-eye (yellow) is worth 9 points and each successive ring is worth 2 fewer points down to 1 for white. The program should output a score for each click and keep track of a running sum for the entire series.

0 0

Post a comment