Homework 6: more if statements and while loops
Due: Tuesday 10/18 by 1pm
Objectives
For this homework assignment you will practice writing programs that use the if statements and while loops.
The Assignment
Starting with this assignment, you must properly indent your programs. This is very important, because poorly indented programs are hard to read and hard to debug. Follow one of the styles listed in CMSC104 Indentation Styles.
-
Write a C program that prompts the user to enter a calendar year (e.g., 2011)
and then reports whether the year is a leap year.
Recall that a year is a leap year if it is divisible by 4. For example,
next year, 2012, is a leap year and February 2012 has 29 days. However,
there are 2 exceptions to this rule. First, if a year is divisble by
100, then it is not a leap year. So, 1900 is not a leap year even
though 1900 is divisble by 4.
Second, if a year is divisible by 400, then it is a leap year
anyway. For example, 2000 is a leap year even though
2000 is divisible by 100.
Note: use the % operator to check divisbility. For example, a variable n is divisible by 5 if and only if n % 5 is equal to 0.
-
Make the following additions to your guessing program from
Classwork 12.
Note: if you worked with someone on
Classwork 12,
you still have to work individually on this homework.
- Complain if the user enters a character other than 'h', 'l' or 'y'.
- Complain if the user 'lies'. That is, your program has narrowed down the number to one possibility, but the user claims it is too high or too low.
- At the beginning, prompt the user for a largest number that he/she will guess. Use this number instead of 100.
- Make your program give up after 8 guesses. This is enough for numbers up to 255, but might not be enough guesses to guarantee a correct guess if the largest number is bigger than 255.
- (Extra Credit) It is boring to play with a program that always picks 50 as the first guess. You can make your program a bit more interesting by randomizing its guesses. (See Homework 5 for instructions on how to use the random() function.) Your program can pick a random number that is 'nearly' half way between lowEnd and highEnd. Pick a definition of 'nearly' that makes sense. You might abandon this strategy when lowEnd and highEnd are too close.
Submitting your programs
Name your programs leap.c and guess2.c. Use the script command to record several sample runs of both programs. Your sample runs should demonstrate all the features of your program, especially for the second program. Remember to exit from the script command when you are done.
Finally, submit your program and sample runs using:
submit cs104_chang hw06 leap.c submit cs104_chang hw06 guess2.c submit cs104_chang hw06 typescript
Be sure to logout completely when you have finished!