# 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: Tire Pressure Logic

Tires must be properly inflated to achieve maximum handling, traction, and durability. It is the air pressure that supports the weight of your vehicle, not the tire itself. Tire pressure should be monitored frequently for safe driving and optimal tire performance.

Uneven pressure side to side can cause a pull to one side or the other. Thus we want to make sure that tire pressure is even from one side to another on both front and back as well as meeting minimum and maximum standards.

Ideally, just inflate all tires to the recommended pressure to prevent problems. Federal law requires car manufacturers to post tire pressure information on the doorwell of the driver's side, like the one shown in the image.

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

Note that the initial code will produce warnings when you try to compile. The warnings do NOT mean the code did not compile. The warnings will go away once you make use of the constants provided.

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 steps and code your solutions into the worksheet where indicated by the comments. See the Example Run to verify the correctness of each computation.
1. For each tire, write code to test and report on the inflation pressure using the words "Perfect", "Low", "High", or "OK", and based on the following criteria.
• Perfect: Tire matches the recommended pressure PSI_FRONT or PSI_REAR exactly.
• Low: Tire pressure is lower than the recommended pressure by the LIMIT.
• High: Tire pressure is higher than the recommended pressure by the LIMIT.
• OK: Tire pressure is acceptable but not too low, too high or perfect.
2. Next compare the side-to-side pressure for both the front and rear tires. The front and rear comparison of side-to-side pressure are independent of each other. If the tires are more than LIMIT apart, report the pressure as "Uneven". If the pressure is not uneven, report the side-to-side pressure as "OK".
3. Report on the overall inflation status such that if all measurements are acceptable, report the overall tire inflation as "OK". Otherwise, report the overall tire inflation as "BAD".
5. 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.
*** Tire Logic ***
Input 32 36 32 35 for values in parentheses.
Enter tire pressures when prompted.
Input right front pressure: 32
Input left front pressure: 33
Input right rear pressure: 34
Input left rear pressure: 35

*** Checking each tire's pressure ***
Right front 32 (Perfect): Perfect
Left front 33 (High): OK
Right rear 34 (Low): OK
Left rear 35 (OK): OK

*** Comparing side to side pressure ***
Front pressure comparison (Uneven): OK
Rear pressure comparison (OK): OK

*** Overall Status ***


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. The values in (parentheses) are expected values when entering the suggested input of: 32 36 32 35.

6. After displaying the output, exit the program.
7. Submit the source code file tirelogic.cpp with the rest of the assignment as described in Deliverables.
Hints:
• To report the overall status, use the boolean variable goodPressure which is initialized to true. When a tire pressure problem is found, assign false to goodPressure.
• To simplify the logic for the side-to-side pressure comparison, use the absolute value math function from lesson 2.3.5: Mathematical Functions.
Drought Severity Levels [3]
Level Intensity % of Mean
D0 Abnormally Dry (40-60%]
D1 Moderate Drought (20-40%]
D2 Severe Drought (10-20%]
D3 Extreme Drought (4-10%]
D4 Exceptional Drought (0-4%]

#### Project 2: Drought Conditions

A drought is a period of below average rainfall resulting in water shortages [1]. The United States Drought Monitor measures drought conditions in the US every week and publishes the severity using classifications like those in the table to the right. Currently, 81.7% of California is in a drought condition [2].

In this project we state the severity of the drought using the categories supplied by the United States Drought Monitor as shown in the table to the right. The "(" means "down to but not including" and the "]" means "up to and including".

For reference, the following table is the currently known rainfall data for the Santa Cruz area in Water Year (WY) 2018. Notice that water year starts in October of the previous year [5].

Santa Cruz Rainfall in Inches
Oct Nov Dec Jan Feb Mar Apr May Jun Jul Aug Sep
Historical Average [4] 1.44 4.08 4.22 6.49 6.15 4.78 1.97 0.70 0.18 0.14 0.11 0.41
Cumulative Average 1.44 5.52 9.74 16.23 22.38 27.16 29.13 29.83 30.01 30.15 30.26 30.67
Water Year 2018 [5] 0.07 2.75 0.18 5.29 0.32
Cumulative WY 2018 0.07 2.82 3.00 8.29 8.61
##### Project Specifications
1. Write a program that asks the user for the historical average rainfall year-to-date (YTD) and the current water year (WY) rainfall YTD for a given area. From this data, calculate if the area is potentially in a drought and report the severity of the drought using the classifications shown in the Drought Severity Levels table above.
2. Name the source code file drought.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 average rainfall year-to-date (YTD) followed by the current rainfall YTD. The input from the user must be in this order as shown in the Example Run. Assume the user inputs only valid data.
4. After the user inputs the data, calculate the current rainfall (c) divided by the average rainfall (a), and display the result as a percentage using the following formula and in the format shown in the Example Run.

5. From the percentage (pct), write a series of if-else statements to display the intensity of the current year rainfall with the wording from the intensity column of the Drought Severity Levels table above. If there is no drought, print the water year intensity as: normal rainfall year.
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. The output statistics line must include:
1. Current rainfall in inches
2. Percentage compared to average
3. Average rainfall in inches
*** Drought Monitor ***

Average rainfall YTD: 16.23
Current rainfall YTD: 17
Rainfall of 17 is 104.744% of the average of 16.23 inches.
Water year intensity: normal rainfall.
Run again? (y/n) y

Average rainfall YTD: 16.23
Current rainfall YTD: 8.29
Rainfall of 8.29 is 51.0783% of the average of 16.23 inches.
Water year intensity: abnormally dry.
Run again? (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 characters in aqua italics, nor does the user input appear in aqua italics.

7. Add a while statement that allows the user to repeat the program by inputting a "y" (without the quotes), and exiting the loop for any other character entered, as shown in the Example Run.
8. Submit the source code file drought.cpp with the rest of the assignment as described in Deliverables.
1. Drought: Wikipedia article.
2. United States Drought Monitor: California Data
3. How are the drought categories assigned?: United States Drought Monitor Tutorial
4. Historic Average: Santa Cruz, California: Intellicast
5. Monthly Precipitation Summary Water Year 2018: California Nevada River Forecast Center: NOAA
6. Monthly Climate Summary Santa Cruz, California: Western Regional Climate Center historical data, 1948 to 2005
 H e l l o ! 0 1 2 3 4 5

#### Project 3: Comparing Characters

Remember from lesson 3.2.6 that the substr() function can extract one or more characters from a string. Also recall from lesson 3.3.3, that we may compare string characters to each other because they are stored as numbers in the computer. The characters of the alphabet are arranged in numerical order using the ASCII table.

In this project, we use the substr() function to extract characters from a string. Once we extract the characters, we use relational operators to test if the first or last character of a string is earlier in the alphabet.

##### Project Specifications
1. Write a program that tests if the first or last letter of a word is earlier in the alphabet. If the characters are the same, we note that as well.
2. Name the source code file firstlast.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 word such as "easy" (without the quote marks) as a string
2. "y" or "n" (without the quote marks) to see if the program should loop again

Assume the user enters only valid data.

4. Use substr() to test if the first or last letter of the word input by the user is earlier in the alphabet and print Last or First followed by the letter itself inside of double quotes. See the Example Run for the exact format.
5. If the letters are the same then print, "First and last characters are the same." (without the quotes) as shown in the Example Run.
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. The output must include double quote marks (") around the single letter.
*** Comparing First and Last Characters ***

Input a word: easy
First letter "e" is earlier in the alphabet.
Run again? (y/n) y

Input a word: zebra
Last letter "a" is earlier in the alphabet.
Run again? (y/n) y

Input a word: roar
First and last characters are the same.
Run again? (y/n) y

Input a word: a
First and last characters are the same.
Run again? (y/n) y

Input a word: Zebra
First letter "Z" is earlier in the alphabet.
Run again? (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 characters in aqua italics, nor does the user input appear in aqua italics.

7. Add a while statement that allows the user to repeat the program by inputting a "y" (without the quotes), and exiting the loop for any other character entered, as shown in the Example Run.
8. Submit the source code file firstlast.cpp with the rest of the assignment as described in Deliverables.

## 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 firstlast.cpp program with 3 or fewer relational expressions, including the while loop, and without using techniques we have not covered. (1 point)

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 * 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. tirelogic.cpp
4. drought.cpp
5. firstlast.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: April 09 2018 @18:35:49