# 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).

Image source: own work

#### Project 1: Truth Tables

A truth table is an easy way to see the effect of combinations of input values from logical variables or expressions applied to logical operations. Truth tables have one column for each input variable and one column for showing all of the possible results of a logical operation [1]. The figure to the right shows logical results for three common logical operations with one or two input values. For example, the inputs are column A and B. The result of combining those two inputs with a logical and operation are shown in the column labeled A and B.

In this project we develop and display truth tables in C++. Some of the tables will show the effect of De Morgan's Laws. DeMorgan's laws tell us how to transform logical expressions using the not operation. The laws are: [2]

not (A or B) = not A and not B; and
not (A and B) = not A or not B

Notice that parenthesis group symbols together with logical operations like they do with mathematical operations.

##### 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. Do NOT add any user input (`cin`) to this project. If you have a `cin` statement in your code, you will get a low score.
4. Leave the existing code unchanged except where shown in comments.
5. Complete the truth tables shown in the Example Run below by filling in the logical operations where shown in the comments of the worksheet code. The goal of the assignment is to use correct logical operations as well as the correct output. For example, to fill in the not (a and b) column replace the comment:

`/*(logical operation here) <<*/` with `(not (a and b)) <<`.

Keep the outer parenthesis as shown. Do NOT type in literal values of `true` or `false`, use `if`-statements or loops. Instead type the given variables with one or more logical operators to print the required output inside the indicated parenthesis of the comment. All two or four outputs of the truth table must use the same logical expression (formula) for each output.

6. Example Run: The outputs of the program must look exactly like the following for full credit.
```C++ Truth Tables

a       not a
true  = false
false = true

a     b       a and b
true  true  = true
true  false = false
false true  = false
false false = false

a     b       a or b
true  true  = true
true  false = true
false true  = true
false false = false

a     b       not a and not b
true  true  = false
true  false = false
false true  = false
false false = true

a     b       not(a or b): De Morgan's law for prior oper
true  true  = false
true  false = false
false true  = false
false false = true

a     b       not a or not b
true  true  = false
true  false = true
false true  = true
false false = true

a     b       not(a and b): De Morgan's law for prior oper
true  true  = false
true  false = true
false true  = true
false false = true
```
7. After displaying the output, exit the program.
8. Submit the source code file `truthtables.cpp` with the rest of the assignment as described in Deliverables.

#### Project 2: Credit Card Decoding

Your credit card number may look like a random string of digits but those digits reveal more than you think. The first digits of the card identify the issuing network and are called the Issuer Identification number (IIN) [1]. In addition, card numbers have length requirements as described in the table below.

Credit Card Number Decoding
Credit Card Number Pattern Test Numbers *
American Express  15 digits long and starts with 34 or 37. `343434343434340370000000000002`
Diners Club 14 digits long and starts with 36, 38, or 300-305. `305693090259043670010200000038520000023237`
Discover 16 digits long and starts with 6011. `6011111111111117`
MasterCard 16 digits long and starts with 51-55. `511111111111111854240000000000155555555555554444`
Visa 13 or 16 digits long and starts with a 4. `42222222222224111111111111111`

* Test numbers are not in use but have a valid format. See list of sources below:

To test invalid card numbers for this assignment, change the lengths or starting numbers of valid card numbers.

##### Project Specifications
1. Write a program that identifies the card issuer based on the card number entered by a user.
2. Name the source code file `cardtype.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 Run below:
1. A single card number (string)
2. A 'y' or 'n' (without the quotes) for the repeat loop

Assume the user enters only valid data.

4. After getting the card number input, program a series of if-statements to decide on the issuer network based on the table above and with the output shown in the Example Run below. Output must include:
1. The length of the card number using the `length()` function.
2. The words "card type" (without the quotes) followed by one of the types listed in the table above.
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.
```Welcome to the Credit Card Number Decoder!

Please enter a credit card number: 370000000000002
The card number length is 15.
The card type is American Express.
Decode another card? (y/n) y

Please enter a credit card number: 36700102000000
The card number length is 14.
The card type is Diners Club.
Decode another card? (y/n) y

Please enter a credit card number: 6011111111111117
The card number length is 16.
The card type is Discover.
Decode another card? (y/n) y

Please enter a credit card number: 5111111111111118
The card number length is 16.
The card type is MasterCard.
Decode another card? (y/n) y

Please enter a credit card number: 4222222222222
The card number length is 13.
The card type is Visa.
Decode another card? (y/n) y

Please enter a credit card number: 422222222222
The card number length is 12.
The card type is unrecognized.
Decode another card? (y/n) n
Goodbye.
```

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 characters in aqua italics, nor does the user input appear in aqua italics.

7. Submit the source code file `cardtype.cpp` with the rest of the assignment as described in Deliverables.

#### Project 3: Juice Orders

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

##### Project Specifications
1. Develop a program that asks a user for a single input where the first two or three characters is a flavor of juice in the following shorthand code and the last characters are the number of bottles for the juice type.
Code Juice Type
ABCApple-Beet-Carrot
AKApple-Kale
CLCelery-Lemon
CSLCucumber-Spinach-Lemon
KPLKale-Parsley-Lemon
OCOrange-Carrot
SACSpinach-Apple-Carrot
WGWheat Grass
WLMWatermelon-Lime-Mint

For example, the code for twelve bottles of Kale-Parsley-Lemon is `KPL12` and seven bottles of Orange-Carrot is `OC7`. Notice there are no spaces between the code and quantity. The number of bottles can be any amount greater than zero.

2. Name the source code file `juice.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: WG12 (no spaces allowed!!)
2. A 'y' or 'n' (without the quotes) for the repeat loop

Assume the user enters all the codes with UPPERCASE letters as shown.

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 ice cream order.
5. You may not use any `string` methods other than `substr()` and `length()` in this program. Square brackets `[]` are also not allowed.
6. Add a `while` statement that allows the user to repeat the program by entering a 'y' (without the quotes).
7. If the user enters an invalid code, display the message, "We don't have that juice." as shown in the example run.
8. 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. Prompts for user input may vary as long as the prompts end in a colon (:) or question mark (?). For the input shown you must get the same output. However, the output must change correctly if the inputs are different.
```Welcome to Pulper Prize Juice Company!

Enter the juice order code: CSL12
12 bottles of Cucumber-Spinach-Lemon

Another item (y/n)? y

Enter the juice order code: AK7
7 bottles of Apple-Kale

Another item (y/n)? y

Enter the juice order code: XY123
We don't have that juice.

Another item? (y/n) n

Your juice order will be right up!
```

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 characters in aqua italics, nor does the user input appear in aqua italics.

9. Submit the source code file `juice.cpp` 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.
• Notice that a juice code can be either two or three digits. This means that the third character of the order code will be either a letter or a digit character.
• Remember that we may compare strings using relational operators. See lesson 3.3.3: Comparing Characters and Strings.

## 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. In 2017, Mastercard introduced a new range of credit card numbers that are still 16 digits long but start with numbers in the range of 2221-2720, called the Mastercard 2-Series [1, 3]. Add the identification of the Mastercard 2-Series to the `cardtype.cpp` program. (1 point)

Here are valid card numbers (not in use) for testing:

• 2222405343248877
• 2222990905257051
```Welcome to the Credit Card Number Decoder!

Please enter a credit card number: 2222405343248877
The card number length is 16.
The card type is MasterCard 2-Series.
Decode another card? (y/n) n
Goodbye.
```
3. Complete the `juice.cpp` program with 11 or fewer relational expressions, including the test condition of the `while`-loop, and without using techniques we have not covered. (1 points)

The program must work correctly to get this extra credit.

4. 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. `truthtables.cpp`
4. `cardtype.cpp`
5. `juice.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.

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: October 02 2019 @13:25:08