# 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 TextPad 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: RGB Color 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 colors. 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]. Over 16 million (2563) colors are possible in the three-byte color model. The names were standardized for the web starting in HTML 3.2 [2] and the following table shows 9 of the basic RGB color values.

Basic RGB Colors
Color Name Red Green Blue
Black 0 0 0
Gray 128 128 128
White 255 255 255
Red 255 0 0
Lime 0 255 0
Blue 0 0 255
Yellow 255 255 0
Magenta 255 0 255
Cyan (Aqua) 0 255 255
##### Project Specifications
1. Write a program that converts RGB values into the color names shown in the table above.
2. You must name the source code file `colorb.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 you can enter three values using three `cin` statements or by using a single `cin` statement with 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 and print the color name. If the input does not match one of those in the table, the program prints an 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 wording of the output. For the input shown you must get the same output. However, the output must change correctly if the inputs are different.
```Basic Color Names

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

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

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

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: 1 2 3
1, 2, 3 is not a basic color
Convert another color? (y/n) n
```

In the above example run, the user entered the values shown in 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.
Hint:
• Use logical operators to test multiple conditions for each color name.
1. RGB color model -- Numeric representations: Wikipedia
2. Adding a touch of style: W3C guide

#### Project 2: Stellar Distances in Miles

The nearest star to Earth is the Sun at 93 million miles. The next closest stars are in the Alpha Centauri system, which is a star system with three stars. In late 2012, astronomers discovered that Tau Ceti may host five planets including one within the star's habitable zone [2]. In this project we explore the distance of these stars from our Earth.

##### Project Specifications
1. Write a program that lists the following stars that are within a range given by a minimum and maximum distance in miles from the Earth using the following distances:
Star Distance in Miles
Sun 9.30 x 107
Proxima Centauri 2.48 x 1013
Alpha Centauri 2.57 x 1013
Barnard's Star 3.51 x 1013
Wolf 359 4.53 x 1013
Sirius A, B 5.06 x 1013
Epsilon Eridani 6.17 x 1013
Ross 248 6.35 x 1013
Procyon 6.70 x 1013
Tau Ceti 7.00 x 1013

Source: NASA [3]

2. You must name the source code file `stellar.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. Minimum distance in miles
2. Maximum distance in miles
3. A 'y' or 'n' (without the quotes) for the repeat loop (spec 5)
4. After getting the two numerical inputs, program a series of `if`-statements to decide on and print the star name if the star is between the minimum and maximum values as shown in the Example Run below. Include stars equal to the minimum or maximum distances in the listing.
5. Include 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 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.
```Enter a minimum and maximum distance from the Earth and I
will list the stars within those distances.

Enter the minimum distance in miles: 0
Enter the maximum distance in miles: 7e13
The stars between 0 and 7e+13 are:
Sun
Proxima Centauri
Barnard's Star
Wolf
Sirius
Epsilon Eridani
Ross
Procyon
Tau Ceti
Run again? (y/n) y

Enter the minimum distance in miles: 1e13
Enter the maximum distance in miles: 5e13
The stars between 1e+13 and 5e+13 are:
Proxima Centauri
Barnard's Star
Wolf
Run again? (y/n) y

Enter the minimum distance in miles: 3.5e13
Enter the maximum distance in miles: 3.6e13
The stars between 3.5e+13 and 3.6e+13 are:
Barnard's Star
Run again? (y/n) n

Thanks for checking on our stellar neighbors!
```

In the above example run, the user entered the values shown in 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.
Hints:
• Test each star one by one to see if it lies between the minimum and maximum distances.
• Remember from the lecture notes that a value lies between 0 and 100 if the value is at least 0 and at most 100
1. The Nearest Stars: An Atlas of the Universe
2. The Nearest Stars to Earth: Space.com
3. The Solar Neighborhood: NASA

#### Project 3: Cake Orders

Encoding and decoding information can save memory space and speed transmission of data. Waitstaff at bakeries often encode orders to reduce writing and speed up customer orders. In this project we will look at decoding orders at a cake company.

##### Project Specifications
1. Develop a program that asks a user for a single input where the first one or two characters is a type of cake in the following shorthand code and the last characters are the number of cakes for the type.
Code Cake Type
BFBlack Forest
CCCarrot Cake
CMChocolate Mint
DFDevil's Food
GCGerman Chocolate
PCPumpkin Cheesecake
RCRum Cake
TTiramisu

For example, the code for 12 Chocolate Mint cakes is: `CM12`. Notice there are no spaces between the code and quantity. The quantity can be any amount greater than zero.

2. You must name the source code file `cake.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. A single order code like: CM12 (no spaces allowed!!)
2. A 'y' or 'n' (without the quotes) for the repeat loop
4. Store the order code in a single `string` variable. Then use `substr()` to analyze and process the input string to print a full description of the cake order.
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 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.
```Welcome to the Great Cake company!

Enter the cake order code: CM12
12 Chocolate Mint cakes
Order more? (y/n) y

Enter the cake order code: PC999
999 Pumpkin Cheesecakes
Order more? (y/n) y

Enter the cake order code: T1
1 Tiramisu cakes
Order more? (y/n) n
```

In the above example run, the user entered the values shown in 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. Assume the user enters all the codes with UPPERCASE letters as shown. Do not add any extra features not required for the problem.
8. Submit this project with the rest of the assignment as described in Deliverables.
Hints:
• Use the `substr()` function to separate the code and quantity into separate variables.
• Remember that we can always extract the last few characters of a string without knowing its length. See lesson 3.2.6: String Functions.

## 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 `colorb.cpp` program with 24 or fewer relational expressions, including the test condition of the `while`-loop, and without using techniques we have not covered. (2 points)

The program must work correctly to get this extra credit.

3. Complete the `cake.cpp` program with 8 or fewer relational expressions, including the test condition of the `while`-loop, and without using techniques we have not covered. (2 points)

The program must work correctly to get this extra credit.

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 * 8 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. `colorb.cpp`
4. `stellar.cpp`
5. `cake.cpp`
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.

You must submit all the files needed to complete your assignment. 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, but must resubmit all your assignment file.

Last Updated: March 21 2017 @00:13:01