A4-Making Selections

Table of Contents


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

Academic Honesty

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

Image

Project 1: Squirrel Logic

Squirrels are familiar to almost everyone. More than 200 squirrel species live all over the world. Some squirrels live in trees and some in the ground. Some squirrels even fly!

Nuts, seeds and berries provide most of the food for squirrels. Tree squirrels are well know for their hoarding of nuts for winter times when other food is scarce.

Squirrels have a logic all their own which we explore in this project.

Project Specifications
  1. Start by downloading the worksheet: squirrel.cpp.

    Keep the same filename and add to the existing code to complete the project. Leave the existing code unchanged, except for changing Your Name and Date Completed in the comments to your name and the date respectively.

  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. Read the comments in the worksheet file and write the code specified in the comments. The code output should look like the following two Example Runs. The places to enter your code are marked:
    // *** Enter code here ***
    
  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 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.
    ***** Squirrel Logic *****
    
    This worksheet prints information about squirrels that depends
    on the answers to the following questions.
    Enter the number of nuts: 63
    Enter the temperature: 75
    Enter true for weekend and false for weekday: false
    
    The squirrel says Chitter!
    63 is basic food, more nuts please.
    When the temperature is 75, squirrels are playing.
    When a squirrel has 63 nuts it says "Bark!"
    With 63 nuts on a weekday, it is a bad party :(
    
    ***** Squirrel Logic *****
    
    This worksheet prints information about squirrels that depends
    on the answers to the following questions.
    Enter the number of nuts: 80
    Enter the temperature: 59
    Enter true for weekend and false for weekday: true
    
    The squirrel says Chatter!
    80 is better food, more nuts please.
    When the temperature is 59, squirrels are resting.
    When a squirrel has 80 nuts it says "Cry!"
    With 80 nuts on a weekend, it is a good party!
    

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

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

Image
Image source (Modified)

Project 2: Numbers in the Electromagnetic Spectrum

Electromagnetic waves can be measured by either wavelength or frequency. The wavelength is the distance between one wave crest (or trough) to the next. The frequency is the rate at which the waves repeat over a period of time such as seconds.

The term electromagnetic spectrum refers to the entire range of the frequencies of electromagnetic radiation and light wavelengths. Electromagnetic radiation extends from radio waves (low frequency) to Gamma rays (high frequency). There are no precisely defined boundaries between the bands of the electromagnetic spectrum as the bands fade into each other like the bands in a rainbow [1]. However, we will use the boundaries shown in the following table for our project.

Electromagnetic Spectrum
Type Wavelength (m) Frequency (Hz)
Radio wave [ 10-1, +∞ ) [ 3 x 109, 0 )
Microwaves [ 10-3, 10-1 ) [ 3 x 109, 3 x 1011 )
Infrared [ 7 x 10-7, 10-3 ) [ 3 x 1011, 4 x 1014 )
Visible light [ 4 x 10-7, 7 x 10-7 ) [ 4 x 1014, 7.5 x 1014 )
Ultraviolet [ 10-8, 4 x 10-7 ) [ 7.5 x 1014, 3 x 1016 )
X-rays [ 10-11, 10-8 ) [ 3 x 1016, 3 x 1019 )
Gamma rays ( -∞, 10-11 ) [ 3 x 1019, +∞ )
Project Specifications
  1. Write a program that asks the user for a wavelength value and prints a description of where the value belongs in the electromagnetic spectrum defined by the table above.
  2. Name the source code file spectrum.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 wavelength in meters
    2. whether to run again (y/n)

    Assume the user enters valid numbers in fixed or exponential notation.

  4. Numbers on the boundary between two wavelengths belong in the longer wavelength (lower frequency) spectrum (higher entry in the table).
  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.
    Enter a wavelength and I will report the band of
    the electromagnetic spectrum.
    
    Enter a wavelength in meters: .001
    Microwaves
    Run again? (y/n) y
    
    Enter a wavelength in meters: 4e-7
    Visible light
    Run again? (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 characters in italics, nor does the user input appear in italics.

  7. Submit the source code file spectrum with the rest of the assignment as described in Deliverables.
Hints:
  • Start by translating the Electromagnetic Spectrum table from scientific to exponential notation. Here are the first few numbers in both exponential and fixed notation.

    Scientific Exponential Fixed
    10-1 1e-1 0.1
    10-3 1e-3 0.001
    7 x 10-7 7e-7 0.0000007

    As an example, Microwaves have a wavelength of 10-3 to 10-1, which is 0.001 to 0.1 meters. Thus, microwave wavelengths are less than 10-1 but greater than or equal to 10-3.

    For more information, see lesson 4.1.2: Exponential Notation.

References and More Information
  1. Electromagnetic spectrum: Wikipedia article
  2. Introduction to electromagnetic waves: Khan Academy

Image
Image source

Project 3: Ice Cream Orders

Encoding and decoding information can save memory space and speed transmission of data. Waitstaff at ice cream shops often encode orders to reduce writing and speed up customer orders. In this project we will look at decoding orders at an ice cream shop.

Project Specifications
  1. Develop a program that asks a user for a single input where the first two characters is a flavor of ice cream in the following shorthand code and the last characters are the number of scoops for the type.
    Code Ice Cream Flavor
    CHChocolate
    CCChocolate Chocolate Chip
    CMChocolate Mint
    HCHorchata
    MCMocha Chip
    RRRocky Road
    SBStrawberry
    VBVanilla Bean

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

  2. Name the source code file icecream.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

    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. 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 Really Cool Ice Creamery!
    
    Enter the ice cream order code: CM12
    12 Chocolate Mint
    Another order? (y/n) y
    
    Enter the ice cream order code: HC2
    2 Horchata
    Another order? (y/n) y
    
    Enter the ice cream order code: XY123
    We don't have that flavor.
    Another order? (y/n) n
    
    Your ice cream order will be right up!
    

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

  7. If the user enters an invalid code, display the message, "We don't have that flavor." as shown in the example run.
  8. Submit the source code file icecream.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.

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

Grading Criteria

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. squirrel.cpp
  4. spectrum.cpp
  5. incecream.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. If you resubmit, you must include all your assignment files in the last submission as Canvas hides prior submissions.

Last Updated: September 25 2017 @15:33:43