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

clock image
Image source

Project 1: Lotto Logic

In the United States, 48 different lotteries are run in 45 states plus the District of Columbia, Puerto Rico, and the U.S. Virgin Islands. There are no national lotteries as such, but some lotteries operate across multiple states and essentially serve as national lotteries. Americans spent $77.7 billion on various lotteries in 2018 [1].

Lotteries are often based on a series of random numbers presented on lottery tickets. If a lottery ticket holder gets the correct numbers on their ticket in certain combinations, they win an amount of money. In this project we simulate a simple lottery of three numbers single-digit numbers and explore the logic to determine a winner.

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

    Keep the same filename and add to the existing code to complete the project. Leave the existing code unchanged, except as explained with comments in the code 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. Read the comments in the worksheet file and write the code where indicated by the comments. See the Example Runs to verify the correctness of each computation.
  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.
    ***** Lotto Logic *****
    
    Enter the first number (0-9): 1
    Enter the second number (0-9): 1
    Enter the third number (0-9): 1
    
    Orange Ticket result: 10
    Gold Ticket result: 0
    Red Ticket result: 5
    Green Ticket result: 20
    Blue Ticket result: 0
    
    ***** Lotto Logic *****
    
    Enter the first number (0-9): 0
    Enter the second number (0-9): 1
    Enter the third number (0-9): 2
    
    Orange Ticket result: 0
    Gold Ticket result: 10
    Red Ticket result: 1
    Green Ticket result: 0
    Blue Ticket result: 0
    
    ***** Lotto Logic *****
    
    Enter the first number (0-9): 6
    Enter the second number (0-9): 1
    Enter the third number (0-9): 4
    
    Orange Ticket result: 0
    Gold Ticket result: 10
    Red Ticket result: 1
    Green Ticket result: 0
    Blue Ticket result: 10
    
    ***** Lotto Logic *****
    
    Enter the first number (0-9): 7
    Enter the second number (0-9): 1
    Enter the third number (0-9): 2
    
    Orange Ticket result: 0
    Gold Ticket result: 10
    Red Ticket result: 1
    Green Ticket result: 0
    Blue Ticket result: 5
    

    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. The values in (parentheses) are expected values when entering the example input.

  6. After displaying the output, exit the program.
  7. Submit the source code file lottologic.cpp with the rest of the assignment as described in Deliverables.
References and More Information
  1. Lotteries in the United States: Wikipedia article.
  2. Reference two.

Image
Image source

Project 2: Weight on Other Planets

Weight is the force of gravity pulling on you towards the center of a planet or other object with mass (mass × gravity). Anything with mass has gravity and gravity gets weaker with distance from the mass. Thus how much you weigh depends on your mass, the mass of the planet, and the distance you are from the center of the planet.

Since the planets in our Solar System have different masses and are different sizes, you will weigh more or less depending on the planet. For example, if you weight 100 pounds on Earth you would weigh only 38 pounds on Mercury.

Project Specifications
  1. Write a program that asks the user for a weight on Earth and then prints the weight on another planet of the users choice using the following conversion factors:
    Planet Conversion Factor
    Mercury 0.38
    Venus 0.90
    Mars 0.38
    Jupiter 2.54
    Saturn 1.08
    Uranus 0.91
    Neptune 1.19

    Source: NASA [1]

  2. Name the source code file planet.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. The weight in pounds (double)
    2. The name of the planet (string)
  4. Based on the planet name input, use a series of if-statements to calculate and display the users weight on that planet. For example, if the user entered 100 pounds as their weight and Mercury as the planet, display the planet name and weight on one line like this:
    On Mercury you weigh 38.0 lbs.
    

    If the user input does not match one of the planet names, print the message, "Invalid planet name." For example:

    Invalid planet name.
    
  5. Avoid magic numbers by declaring a constant for each multiplier in the conversion list of specification 1. For example:
    const double MERCURY_MULT = 0.38;
    
  6. Numbers must display display with exactly one number after the decimal point. Therefore you must use decimal formatting commands as discussed in lesson 4.1.3.
  7. Include a while statement that allows the user to repeat the program by entering a "y" (without the quotes).
  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 (?), and the text has no other colons or question marks. For the input shown you must get the same output. However, the output must change correctly if the user inputs are different.
    Welcome to the Solar 8-Planet Weight Converter!
    
    Enter your weight in pounds: 100
    Now, select from one of the following planets:
        Mercury
        Venus
        Mars
        Jupiter
        Saturn
        Uranus
        Neptune
    Please enter the planet: Mercury
    On Mercury you weigh 38.0 lbs.
    
    Do you want to run again? (y/n) y
    Enter your weight in pounds: 123
    Now, select from one of the following planets:
        Mercury
        Venus
        Mars
        Jupiter
        Saturn
        Uranus
        Neptune
    Please enter the planet: marz
    Invalid planet name.
    
    Do you want to run again? (y/n) y
    Enter your weight in pounds: 111
    Now, select from one of the following planets:
        Mercury
        Venus
        Mars
        Jupiter
        Saturn
        Uranus
        Neptune
    Please enter the planet: Mars
    On Mars you weigh 42.2 lbs.
    
    Do you want to 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.

  9. Submit the source code file planet.cpp with the rest of the assignment as described in Deliverables.
References and More Information
  1. Astro-Matic 3000
  2. How Do We Weigh Planets?

Image
Image source

Project 3: Playing Card Notations

Encoding and decoding information can save memory space and speed transmission of data. We will apply this principle to standard playing cards using a rank and suit notation. For example we will use QS for the Queen of Spades and 10D for the 10 of Diamonds. Our program will print the full name when the user enters the shorthand notation.

Project Specifications
  1. Develop a program that asks a user for a single input where the first two or three characters are a playing card in a rank-suit order. The first one or two characters of the input are the rank and the last character is the suit. The program displays the full card name for the shorthand notation.
    Notation Meaning
    2 ... 10Card rank values
    AAce
    KKing
    QQueen
    JJack
    DDiamonds
    HHearts
    SSpades
    CClubs

    For example, the code for a Queen of Spades is QS and the 10 of Diamonds is 10D. Notice there are no spaces between the rank and the suit.

  2. Name the source code file cards.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 card code like: QS (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 card code in a single string variable. Then analyze and process the input string to print a full description of the card.
  5. Do not use any string methods other than substr() and length() in this program. Square brackets [] are not allowed either.
  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, "Invalid card notation" 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 (?) as shown below, and the text has no other colons or question marks. For the input shown you must get the same output. However, the output must change correctly if the inputs are different.
    Welcome to the Rapid Card Decoder!
    
    Enter the card notation: QS
    Queen of Spades
    Another card (y/n)? y
    
    Enter the card notation: 10C
    10 of Clubs
    Another card (y/n)? y
    
    Enter the card notation: 2H
    2 of Hearts
    Another card (y/n)? y
    
    Enter the card notation: 1D
    Invalid card notation!
    
    Another card (y/n)? y
    
    Enter the card notation: 11H
    Invalid card notation!
    
    Another card (y/n)? y
    
    Enter the card notation: 5X
    Invalid card notation!
    
    Another card (y/n)? n
    
    Happy decoding!
    

    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 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.
  • Notice that a card 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. Complete the planet.cpp program with 6 or fewer cout statements and without using techniques we have not covered. (1 point)

    The program must work correctly to get this extra credit and include all the output shown in the Example Run including:

    1. Welcome message
    2. Weight input prompt
    3. Planet list
    4. Planet input prompt
    5. Invalid planet name message
    6. Weight on the planet message
    7. Run again prompt

  3. Complete the cards.cpp program with 13 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.

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)
    • Compiles without warnings
    • 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. lottologic.cpp
  4. planet.cpp
  5. cards.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: March 22 2020 @21:59:40