At the end of this lesson, you will be able to:
A2.3 write algorithms with nested structures (e.g., to count elements in an array, calculate a total, find highest or lowest value, or perform a linear search).
understand and use a while loop
With a pencil and paper, answer the following questions:
ICS3U only: Create the pseudocode for the following question:
A museum determines the prices for their clientele using the following logic. Display which discount a person gets for the following.
If the child is under 5, it is free.
If the person is 65 or older, they get a senior's discount.
If the person is between the ages of 5 and 25 years old AND they are a student, they get a student discount.
If it is a Tuesday, everyone gets the weekly discount.
Otherwise, they pay regular price.
Note: All discounts are the same amount.
ICS3C only: Create the pseudocode for the following question:
Ask the user if they did their homework.
Ask the user if they cleaned their room.
If the user did their homework AND cleaned their room, tell them they can go to the movies.
Otherwise if the user did their homework OR cleaned their room, tell them they still have some work to do.
Match each of the symbols below with their operator (AND, OR, NOT).
a)
b)
c)
nested if statements
decision statements
from the online book Computer Based Problem Solving by Patrick Coxall, read:
watch "Python While Loop" until 5:45
While loops:
have a boolean expression at the BEGINNING of the loop
have a counter that will be part of your condition to stop the loop.
The counter is initialized OUTSIDE the loop.
The counter is incremented/decremented INSIDE the loop.
execute a minimum of 0 times
An infinite loop occurs when the boolean expression never evaluates to false and therefore the loop goes on forever and your program crashes.
Write a program that asks the user to enter a positive integer
It then uses a while loop to add all the numbers from 0 to that integer and displays the sum
i.e. Enter a positive number: 5 // user enter 5
// Program calculates the following using a loop: 0 + 1 + 2 + 3 + 4 + 5
The sum of the numbers from 0 to 5 is: 15
Make sure your program does not crash if a user enters invalid input!
in groups of 2, do the following on the board for today's daily assignment:
Top-Down Design
Flow Chart
Pseudocode
Test Cases
complete the Daily Assignment section in Hãpara Workspace for this day
if Hãpara is not working, make a copy of this document
move it to your IMH-ICS folder for this course
recreate the same program in C++
NOTE: in C++, you need to further check that a number is an integer even after you cast it
float userFloat = stof(userNumAsString);
// if userFloat is the same as its value as an int, AND
// larger than 0, then it is a whole number
if (userFloat != static_cast<int>(userFloat) || userFloat <= 0 ) {
cout << "Please enter a whole number" << endl;
continue;
}