| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Exercises - Selection

Page history last edited by Steve Sweeney 4 years, 10 months ago

Part A: Simple Selection Exercises

The following exercises can be done using simple IF statements.

Save your work as SelectionA1, SelectionA2, etc.

 

  1. Ask the user their name. Give a different message depending on whether they enter your (the programmer's) name or another name.  In other words, write a program that compares the user's name to the programmer's name, and gives different messages depending on whether they are the same or different.
  2. Ask the user to enter two numbers, and have your program tell them which number is the larger of the two.  Extend this program to also say if they are equal to each other.
  3. Ask the user to answer 3 math problems and either tell them they are right (IF), or show them the correct answer (ELSE). Make this program more interesting by using random numbers.

 

Part B: Selection Exercises

Use some form of the IF-ELSEIF-ELSE structure to complete the following exercises. You may also use Boolean Logic (AND, OR) to improve your program. The CASE statement may also be used, but only under specific conditions.

Save your work as SelectionB1, SelectionB2, etc.

 

  1. Ask the user where they are from (e.g., province or country).  Depending on their answer, give a bit of trivia on the region.  If they enter a region that you don't have in your program, give them a generic answer, "I don't know anything about <region>", where <region> is the user input.
  2. Write a program to determine the smallest of three numbers.
  3. Write a program to determine the largest of three numbers.
  4. Write a program that asks for the user's age.  If the user is under 13, output, "You are a child".  For 13 to 19, output, "You are a teen".  From 20 to 65, "You are an adult".  Over 65, "You are a senior citizen".
  5. Write a program that plays one round of rock, paper, scissors.  Rock beats scissors.  Paper beats rock.  Scissors beats paper.
    1. Assume two players, and each player will enter their selection either as words ("rock", "paper", "scissors") or using a menu (1 = rock, 2 = paper, etc).
    2. Make the game more interesting by having the computer play against the human.  Use random numbers for the computer's choice (1, 2, or 3) and compare against the user's choice.
  6. Ask the user their language preference using a single letter or menu item (e = English, or 1 = English, etc.), and then output a greeting in that language.  If your program doesn't have a greeting in their preferred language, you should output a statement apologizing for this.  For example, you might use 'e' for English, 'f' for French, 'g' for German, etc.
  7. Write a program to test the user on the concept of the modulo operator.  Ask the user to enter two integer values (called num1 and num2 for now), and then ask them to determine the answer of num1 mod num2.  Congratulate them if they get it correct.  If they get the answer wrong, remind them of how modulo works, and tell them the correct answer in a nicely formatted way.
    (You may need to research modulo: Presentation: Conversions & Modulo in Turing)

 

Part C: Advanced Selection Exercises

Use various concepts from selection, including IF-ELSE, Boolean Logic (AND, OR), and nested statements to complete these exercises. Also incorporates more advanced mathematical concepts such as integer division and modulo.

Save your work as SelectionC1, SelectionC2, etc.

 

  1. Write a program that asks the user to enter three integer values, and then the program will output the three numbers in order (lowest to highest)
  2. Write a program that solves a quadratic equation in standard form using the quadratic formula.  You may look this up to refresh your memory.  Check if the equation has zero, one, or two solutions first, using the discriminant, and then output any solutions (if there are any).
  3. In the (imaginary) country of Riva, the cost of mailing a letter is 40 sinas for letters up to 30g, 55 sinas for letters over 30g and up to 50g, 70 sinas for letters over 50g and up to 100g, and 75 sinas for anything above 100g. Write a program that prompts the user for a mass and then gives the cost of mailing a letter having that mass.
    extend 1: Instead of a flat rate over 100g, charge an additional 25 sinas for each additional 50g or part thereof.  Hint: Try to use the mathematical rounding functions
    extend 2: Try to solve this problem using the INTEGER DIVISION and MODULO operators (you may need to do some research on these topics).
    (You may need to research modulo: Presentation: Conversions & Modulo in Turing)
  4. Write a program that makes change for amounts less than one dollar.  Input to the program should be a positive integer less than 100, representing an amount of money, in cents.  Output should be the original amount of money together with a set of coins (quarters, dimes, nickels, pennies) that could make up that amount.
    (You may need to research modulo: Presentation: Conversions & Modulo in Turing)
    Improvements:
    1. The program should produce change containing the minimum number of coins required.
    2. The output should be expressed in a natural form.  For example, don't refer to zero coins of a particular type, or the singular or plural forms for coins should be used correctly (i.e., "1 dime", not "1 dimes").

Comments (0)

You don't have permission to comment on this page.