• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

View

# Exercises - Conditional Loops

last edited by 2 years, 9 months ago

Complete the following exercises in any order (although the earlier exercises should be easier).

Focus on completing the (a) exercises, as they are the basics for each exercise. You can try the extensions (b, c, etc), but don't get stuck on these for no reason. Some exercises have a program solution provided... don't check this solution until you have asked for help!

1. (a) Write a program that keeps asking the user for their name until they enter "Tim".  Otherwise, keep saying "You are not Tim!" and ask for their name.

(b) Allow your name as a valid input as well.

(c) Keep track of how many times the user fails to enter the correct name, and report it when they finally get it right.

2. (a) Write a program that asks the user to enter an integer value.  Count how many numbers the user enters.  End the program when the user enters zero.  Do not count zero.  Tell the user the result.
(b) In addition to counting how many numbers were entered, also keep track of how many above zero and below zero.  Still end the program on zero, and then tell the user the result.
(c) Instead of always using zero, ask the user what number they want to use as the terminating value.

3. (a) Write a program that acts like a cash register. The user can keep entering values, and you will add those values together, reporting the new total each time. You will need some way to end your program. One good way to end your program would be to tell the user to enter a particular value when they are done (for example, -1 should not be a valid price). This is called the "sentinel" value.
(b) Modify your program so you also keep track of the total tax (in Ontario, that is 13%).
(c) Nicely format your input and output, including two decimal places for all dollar amounts.

4. (a) Write a program that asks a student for four (4) grades, and then calculates the average. Use a loop to add up the grades (i.e., do not use separate variables).
(b) Modify your program so the user can specify how many grades they want in their average, then input those grades (using a loop) and calculate the average.
(c) Modify your program so the user can enter as many grades as they want. You will need to use some value to indicate that they are done (for example, -1 is not a possible grade). This value is called the "sentinel" value, and is used to exit the loop. You will also need to track how many grades they enter so you can calculate the average.

Note: You may skip this question if you haven't learned random values yet.

5. (a) Write a program that asks the user the same single, simple, random math problem until they get it right. (turing solution)

(b) Add the option for the user to specify how many problems they want to answer. (turing solution)

(c) Don't do a fixed number of questions.  Instead, ask the user at the end of each question sequence (i.e., when they finally get it right) if they want to continue. (turing solution)

(d) Keep track of the user's attempts and the total number of questions, and then report their score (i.e., "you took X tries to answer Y questions correctly")

(e) Set a maximum number of tries for each question.  When the user makes their last guess, tell them the answer an move on.

6. (a) Write a program to simulate a person running the 100m dash. Unfortunately, your runner isn't very consistent with their speed, and for each second of the race, they may cover from 5 to 15 metres. Report their current position each second, and the race ends when they cross the finish line.
(b) Add three additional runners (total four runners), each with their own speed which changes each second. End the race until one runner wins.
(c) Modify the program to keep running the race until ALL runners are finished. Once a runner crosses the finish line, they should stop running. If two runners cross the line in the same second, the further runner wins.
-

7. (a) Ask the user for a (small) number (N) and a word, and then display the word N-times on the same line.  When the user enters the word "done", end the program. (turing solution)

(b) Let the user decide which word should terminate the program. (turing solution)

(c) Add a random element where the program will refuse to display the current word.  Make sure you include a message to the user.  Try to make the reason funny/cryptic/technical.

8. (a) Create a program that checks user name and password.  Think carefully how you want to handle each of these (i.e., you don't want to give an invalid user information they could use to hack into the system!).  Start with a single user. (turing solution)

(c) Add a security feature where there are a maximum number of failed attempts before the system locks out the login process.

(d) More security - whenever there is a valid user name with an incorrect password, track the number of failed attempts.  Upon a successful login, report the number of failed attempt on their account to the user.  You might want to only try this feature for a single user.

(e) Once "logged in" give the user some kind of command line prompt (e.g., "> ") and let them type anything they want.  If they type "quit" (or whatever you want), exit the program.  If they type "logout", return them to the log in prompt.

9. Collatz Conjecture:  Start with any natural number, N (1, 2, 3, 4, ...).  If the number is even, divide it by 2.  If the number is odd, multiply by 3 then add 1.  If you repeat this process enough times, the number should always reach 1.  Write a program that tests this conjecture by allowing the user to enter a number, and then apply the Collatz algorithm until the number reaches 1.  Show the result of each step. Hint: You will need to use the MODULO operator to determine even or odd. You may need to do some research if you haven't been taught this yet.