# A4-Making Selections

## Objectives

• Make use of exponential notation
• Continue using if-statements to make selections
• Start using logical operators to create more complex test conditions
• Start using simple loops

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

• You are encouraged to work with one other student of this class following the rules of Pair Programming for Homework Assignments. If you choose to pair program, there is a bonus applied.
• You may not give a copy of your code to your designated pair-programming partner if you did not develop the code together.
• You may not show your completed code to another person or look at another person's code until you complete and submit this assignment and the due date has passed, except for code you develop together with your pair-programming partner.
• You may get help from people other than your pair-programming partner if you get stuck, but only if they do not show or tell you the code to type.
• Remember that the instructor performs similarity tests on programming project submissions, and plagiarized code is usually very easy to detect.

## Preparation

1. Make sure you have completed the exercises from lesson 4.
2. Complete the Review Exercises in CodeLab 4. These exercises will help prepare you for the problem-solving programs and should be completed first.

## Project Specifications

Your solutions to these projects must only use techniques we have covered so far.

#### Programming Style

For all programs, remember to follow all the style rules we have covered including the recent items:

1. No magic numbers
2. Indentation in `while` statements and placement of curly braces
3. No tab characters in your code.

You can remove tab characters by either setting up your text editor correctly (see here) or by running a program named astyle (see here).

4. Meaningful variable names and consistent naming style (caps vs. underbars).

#### Project 1: A Boolean Tea

George Boole was a pioneer in the study of algebraic logic. His work, later extended by others, formed the basis of Boolean algebra, in which that values of variables are either `true` or `false`. The variables may be combined using the logical operators AND, OR and NOT among others. See lesson 4.2.6 for more information on logical operators.

#### Setting

Mr. Boole is a college professor and you have been invited to tea. During your visit you can discuss anything you want. However, when he asks what you want for tea, you must answer `true` or `false` to his questions. In this project you will write a program to simulate asking questions about how you want your tea and then summarize the beverage choice.

##### Project Specifications

Keep the same filename and add to the existing code to complete the project. Leave the existing code unchanged, except as explained with comments and these instructions. Include all your code in this single file.

2. Add your name and the date to the file comment block at the top of the file.
3. User input is already coded into the worksheet.

Do not add any other input commands or change the input order.

4. Complete each of the following Boolean logic problems and code your solutions into the worksheet after the labeling comments. See the Example Runs to verify the correctness of each computation.
1. Ask for a second teaspoon of sugar only if the first teaspoon of sugar was requested.
2. If black tea is requested print "Black tea", otherwise print "Green tea".
3. If sugar and milk are requested, print ", with milk". If milk is not requested print ", no milk". Otherwise do not print anything.
4. If milk and sugar are requested print ", and". If sugar, but not milk is requested print " with". Otherwise do not print anything.
5. If no sugar is requested print " unsweetened". Otherwise print either ("one" or "two") teaspoons of sugar depending on the amount of sugar requested.
5. Example Runs: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and exact wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
```We have two types of tea, Black and Green.
Would you like Black tea? true
Would you like milk in your tea? true
Would you like sugar? true
Would you like more sugar? false
Black tea, with milk, and one teaspoon of sugar.
```
```We have two types of tea, Black and Green.
Would you like Black tea? true
Would you like milk in your tea? false
Would you like sugar? true
Would you like more sugar? true
Black tea, no milk, with two teaspoons of sugar.
```
```We have two types of tea, Black and Green.
Would you like Black tea? false
Would you like milk in your tea? false
Would you like sugar? false
Green tea, no milk, unsweetened.
```

In the above example runs, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics.

6. After displaying the output, exit the program.
7. Submit the source code file `boolwork.cpp` with the rest of the assignment as described in Deliverables.

#### Project 2: RGB Grayscale Names

Most video displays use a color system known as RGB (Red, Green, Blue). The RGB system makes colors by combining red, green and blue values. In computers, the component values are often stored as integer numbers in the range 0 to 255, the range that a single 8-bit byte can offer [1]. When the RGB values are the same, we have a grayscale system from black to white as shown in the following table

Grayscale Colors
Color Name Red Green Blue
Black 0 0 0
Dim gray / dim grey 105 105 105
Gray / grey 128 128 128
Dark gray / dark grey 169 169 169
Silver 192 192 192
Light gray / light grey 211 211 211
Gainsboro 220 220 220
White smoke 245 245 245
White 255 255 255
##### Project Specifications
1. Write a program that converts RGB values that are equal to each other into the grayscale color names shown in the table above.
2. You must name the source code file `rgbgray.cpp` and include all your code in this single file.

Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

3. Ask the user for the following inputs (and no other input) in this order, as shown in the Example Run below:
1. an integer for red
2. an integer for green
3. an integer for blue
4. A 'y' or 'n' (without the quotes) for the repeat loop

Note that we can enter three values using three `cin` statements or by using code like:

```cout << "Enter 3 integers for red, green, blue: ";
int red, green, blue;
cin >> red >> green >> blue;
```
4. After getting the three integer inputs, program a series of `if`-statements to decide on the grayscale color name shown in the Grayscale Colors table above.
1. If the input values are not equal, the program prints an error message as shown in the Example Run below.
2. If the input values are not one of those in the table, the program prints and error message as shown in the Example Run below.
.
5. Add a `while` statement that allows the user to repeat the program by entering a 'y' (without the quotes).
6. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and exact wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
```Basic Grayscale Color Names

This program converts red, green, blue ints to a grayscale.
Enter integer values from 0 to 255 to get RGB grayscale names.
Leave a space between each red, green and blue value entered.

Enter 3 integers for red, green, blue: 0 0 0
0, 0, 0 is Black.
Convert another color? (y/n) y

Enter 3 integers for red, green, blue: 128 128 128
128, 128, 128 is Gray.
Convert another color? (y/n) y

Enter 3 integers for red, green, blue: 192 192 192
192, 192, 192 is Silver.
Convert another color? (y/n) y

Enter 3 integers for red, green, blue: 1 1 1
1, 1, 1 is not a recognized shade.
Convert another color? (y/n) y

Enter 3 integers for red, green, blue: 1 2 3
1, 2, 3 is an error. RGB values must all be the same.
Convert another color? (y/n) n
```

In the above example run, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the words in italics, nor does the user input appear in italics.

7. Submit this project with the rest of the assignment as described in Deliverables.
1. RGB color model -- Numeric representations: Wikipedia

Number 1
Fan Quiz

#### Project 3: I am a Fan Quiz

In this project we write four quiz questions on a topic of your choosing (keep the topic appropriate for a family-oriented program). For quiz ideas, check out this Fan quiz website.

Remember to cite all sources you use in your code, including quiz questions and other ideas. This assignment adpated from Gina Sprint at Washington State University.

##### Project Specifications
1. Write a program with four quiz questions, one each of the following types in the order shown below:
1. Multiple choice with four possible answers, of which one is correct. Each answer is selected by a single character `a-d`.
2. Fill in the blank that takes an integer as input.
3. True or false that accepts a single boolean value as input. To enable `true` or `false` as input, add the following statement to your code before the input statement:
```cin >> boolalpha; // require true or false for bool input.
```
4. Multiple answer with four possible answers selected by a single character `a-d`, of which two are required for a correct answer and the other two possible answers are wrong.

The user will need to enter a `string` with both the correct letters for answers and your code will need to use `substr()` to examine each letter of the string. See the Example Runs for examples.

2. Name the source code file `quiz.cpp` and include all your code in this single file.

Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

3. Ask the user for the following inputs (and no other input) in this order and each on their own line, as shown in the Example Runs below:
1. single character (`char`)
2. integer number (`int`)
3. boolean value (`true` or `false`)
4. Two letters (`string`)

Assume the user enters only valid data.

4. Number each question.
5. Check the user's answer for correctness using an if-else structure after each question, scoring 1 point for each correct answer. If the user answers incorrectly, inform the user and provide the correct answer.
6. At the end of the quiz, display the number correct, followed by the word "correct", and a message commenting on the user's skill. See the Example Runs for examples.
7. Add a `while` statement that allows the user to repeat the program by entering a 'y' (without the quotes).
8. Example Runs: The input prompts of the program must look like the following for full credit, including the same order of input. Credit for much of the content goes to this quiz.
```Welcome to the "Guardians of the Galaxy Vol. 2" Quiz!

1. Which actor provided the voice for Baby Groot?
a. David Hasselhoff
b. Hulk Hogan
c. Kurt Russell
d. Vin Diesel
Enter a single character a-d: a
Sorry, that is wrong. The answer is d. Vin Diesel.

2. How many times did Baby Groot nearly press the wrong button on the bomb?
Enter an integer: 42
Sorry, that is wrong. The answer is 4.

3. True or false? Rocket is a rabbit.
Input true or false: true
Sorry, that is wrong. Rocket is a Racoon!

4. Select both members of the Guardians of the Galaxy team.
a. Ego
b. Peter Quill
c. Baby Groot
d. Thanos
Enter both letters with no spaces between them: ab
Sorry, that is wrong. The answer is "bc".

You scored 0 correct.
Did you watch the movie?

Do you want to retake the quiz? (y/n) y
```
```Welcome to the "Guardians of the Galaxy Vol. 2" Quiz!

1. Which actor provided the voice for Baby Groot?
a. David Hasselhoff
b. Hulk Hogan
c. Kurt Russell
d. Vin Diesel
Enter a single character a-d: d

2. How many times did Baby Groot nearly press the wrong button on the bomb?
Enter an integer: 4

3. True or false? Rocket is a rabbit.
Input true or false: false

4. Select both members of the Guardians of the Galaxy team.
a. Ego
b. Peter Quill
c. Baby Groot
d. Thanos
Enter both letters with no spaces between them: cb

You scored 4 correct.
Amazing! How many times did you watch the movie?

Do you want to retake the quiz? (y/n) n
```

In the above example runs, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics.

9. Create a file named `answers.txt` that contains nothing but the correct answers to your quiz, with each answer on a new line. After the answers, put the character `n` (to end the while loop) on a new line. For example:
```d
4
false
bc
n
```

Do NOT include your name or any other information in the `answers.txt` file. Think of `answers.txt` file as a crib sheet for someone to know the answers to the quiz questions in advance, like when the instructor is grading the program.

10. Submit both the source code file `quiz.cpp` and `answers.txt` with the rest of the assignment as described in Deliverables.
Hints:
• Multiple answer input may be in any order. Make sure you test all the possibilities.
• A program that reads input from the keyboard can also read input from a text file. This is called input redirection, and is a feature of the terminal window of most operating systems including Linux, OSX and Windows.

You may use this feature to verify your quiz and `answers.txt` file. Compile `quiz.cpp` and add the `answers.txt` file to the folder containing the compiled program. Then type the following into the terminal window for the folder:

```./quiz < answers.txt
```

Your program should run and display text for correct answers. Fix any errors you see such as wrong quiz responses.

## Extra Credit

The following are worth extra credit points:

1. Complete the assignment using pair programming with the same person for all three projects. (2 points)
2. Complete the `rgbgray.cpp` program with 12 or fewer relational expressions, including the test condition of the `while`-loop, and without using techniques we have not covered. (1 point)

The program must work correctly to get this extra credit.

3. Attend an SI session for this assignment, sign the roll sheet and type in your README.txt, "attended SI" followed by the date(s) attended. (2 points)

Make certain that your `README.txt` file describes any extra credit attempted.

## Tutorial Lab

In preparation for next weeks lessons, complete the following:

1. Type the program `invtable.cpp` from the textbook on page 145 into a text editor, and then compile and run the program. Submit your working source code file to Canvas for grading using the file name `invtable.cpp`.
2. Complete the Tutorial Exercises in CodeLab 4 before the specified due date. Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. You can look at solutions if you miss your first attempt by clicking the "Solution" tab.

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

#### Lesson Exercises

• 2: All lesson exercises attempted and turned in
• 1: Some lesson exercises completed and turned in
• 0: No lesson exercises completed or turned in

#### Programming Projects (x3)

• 5: Demonstrates mastery of the program
• Applies concepts from the lessons appropriately
• Meets all specifications (see above)
• Runs to completion with no abnormal error conditions
• Generates correct output given correct input
• Correct file name
• 4: Has most of the functionality expected of the program
• Demonstrates some techniques from the lesson
• Attempts to meet all but one of the specifications (see above)
• Implementation seems more complicated than necessary.
• May have one minor error
• 3: Has some of the functionality expected of the program
• Demonstrates some techniques from the lesson
• Attempts to meet at least 1/2 of the specifications (see above)
• Implementation seems excessively complicated.
• May have 2-3 minor errors
• 2: Serious functional problems but shows some effort and understanding
• Attempts to meet less than 1/2 of the of the specifications (see above)
• Has a major error or many minor errors
• Implementation seems very convoluted
• Demonstrates few techniques from the lesson
• 1: Does not compile or wrong file turned in
• 0: Not turned in or uses techniques not covered

#### Programming Projects Style

• 3: Code is well-documented including:
• 2: Code has a minor documentation error
• 1: Code has some documentation errors
• 0: No apparent attempt to follow documentation standards or write documentation comments

#### CodeLab and Other Tutorial Exercises

• Number CodeLab completed correctly / number exercises * 6 and rounded up to the nearest integer.
• -1 if the tutorial lab file does not compile
• -2 if the tutorial exercise file is not turned in

#### `README.txt` File

• 2: `README.txt` file submitted following the instructions
• 1: `README.txt` file submitted but some information was missing
• 0: No `README.txt` file submitted

Total possible: 30, plus extra credit

## Deliverables

Students submit some homework as they work on it like CodeLab. However, students must submit other homework in Canvas following the link to A4-Making Selections. Include the following items when submitting to Canvas:

1. `README.txt` file
2. All the exercise files from Lesson 4
3. `boolwork.cpp`
4. `rgbgray.cpp`
5. `quiz.cpp` and `answers.txt`
6. `invtable.cpp` from the Tutorial Lab

Note: Make certain your programs compile before you turn them in. When a program does not compile then it does not function either. For all programming problems, you should expect little or no credit if your program does not compile and run. For more information see the Grading Criteria.

Submit all the files needed to complete your assignment at one time. Your assignment must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline. If you resubmit, you must include all your assignment files in the last submission as Canvas hides prior submissions.

Last Updated: March 04 2019 @21:47:29