Greetings All!
It's once again
QUIZ TIME!!! That's right, listed below are a set of quiz questions to help you test your knowledge and understanding of the material contained in chapters 7.
In addition to the questions and exercises below, make sure that as you're reading the book you enter the examples into your compiler, build the program, and run the executable. I know this is a time consuming process, but the repeat use of keywords, syntax, and semantics will help ingrain the information into your long-term memory. My advice is to create a simple "driver" project with a function main. As you read, enter the examples into function main, test it, and then erase it for use again in the next example.
PLEASE DO NOT POST THE ANSWERS TO THESE QUESTIONS OR EXERCISES. If you are unable to answer these questions, please ask for assistance, but
DO NOT POST THE ANSWERS. Any question which is not marked with
[Extra Credit] can be answered by reading your textbook. Questions which are marked
[Extra Credit] either have been answered in the thread previously, or can be answered by doing a bit of research.
I will create an answer thread for these questions immediately, so that people will have a chance to get the answers more quickly.
Chapter 7 Quiz1. What does a while-loop cause your program to do?
2. What does the “continue” statement do?
3. What does the “break” statement do?
4. What is it called when you have a loop in which the exit condition can never be met?
5. What loop device do you use if you want to ensure the loop executes at least once, regardless of the success/failure of the test condition?
6. What are the 3 parts to a for-loop header?
7. Can you initialize, test, or perform more than one action within a loop header? If so, what does the syntax look like?
8. Which of the three components of a for-loop header can be left out?
9. According to the new ANSI standard, what is the scope of variables declared in the for-loop header?
10. What types of expressions can be used in a switch statement?
11. What happens if there is no ‘break’ statement at the end of a switch case?
12. Why is it a good idea to always have a default case in a switch statement?
Chapter 7 Exercises1.
Guessing Game: Returning to the “guess the number” exercise from week 3, lets now make it a complete game. In your main function generate a random ‘secret’ number using the method shown in week 5 between 1 and 100. Next, repeatedly ask the user to guess the number UNTIL s/he gets the answer correct. Each time they guess let them know whether the number was higher or lower than their guess. Once the user has guessed correctly, let them know and tell them how many guesses it took.
2.
Color Menu: In this exercise you are going to write a program that shows the user a menu asking them what color they would like to display their menu in. The menu itself, will be a list of matching numbers and colors. Use the menu below to determine menu options. Present the menu to the user over and over again, allowing them to change the color of the menu until they choose the q option. Once they select ‘q’, terminate the program. There is helper code below to allow you to change the color of the console window. Once the user has selected a color option, set the color for the console window, and then re-print the menu onto the screen. Although not strictly necessary for this exercise, I encourage you to make an enum out of the following menu options and color names, and then use a switch statement to check for color values. The program will work just fine without, however….perhaps try it both ways and see how it’s different in this case.
Show the users the following menu:
--------------------------------------------
1. Dark Blue
2. Dark Green
3. Teal
4. Burgundy
5. Violet
6. Gold
7. Silver
8. Gray
9. Blue
10. Green
11. Cyan
12. Red
13. Purple
14. Yellow
15. White
Q. Quit
Please select a color to display your menu:
--------------------------------------------
// To gain access to the functionality required to change the console window colors, add the following include file#include <windows.h>// In function main, call the following line ONCE, to get the handle to the console windowHANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );// To actually SET the color of the console window use the following line of code.SetConsoleTextAttribute( hConsole, COLOR_VALUE ); // where COLOR_VALUE is the color code to set it to
Cheers and Good luck!