At the end of this lesson, you will be able to:
A3.1 demonstrate the ability to use existing subprograms (e.g., random number generator, substring, absolute value) within computer programs;
generate random numbers
create if..then..else statements
With a pencil and paper, write the answers to the following questions.
Declare a variable called my_name in Python and assign it to your name. Then do the same for C++.
Declare a constant in Python for gravity (9.81). Then do the same for C++.
Write the symbol that corresponds to each of the following expressions:
a) "is equal to"
b) "has the value of"
c) "is greater than"
d) "is not equal to"
e) "is less than or equal to"
Translate the following pseudocode into code in Python. Then do the same for C++.
IF (age >= 100) THEN
DISPLAY "You are a centenarian!"
Create the flowchart for a program that does the following:
Ask the user to enter their age.
If they are the same age as you, tell them so.
If they are older than you, tell them so.
If they are younger than you, tell them so.
Create 3 test cases for the above program: 1 for a user age that is older than you, 1 for a user that is the same age and 1 for a user that is younger than you.
boolean expressions
if...then statements
Note: IF..THEN must be on the SAME LINE when writing pseudocode and the next line should be indented
IF (it is raining) THEN
WEAR a rain jacket
ENDIF
go over how to generate a random number in Python:
import random
random_variable = random.randint(1, 100) # a number between 1 and 100
note that in a flowchart, generating a random number is a process
from the online book Computer Based Problem Solving by Patrick Coxall, read:
read "Python if..else statement"
watch the video within from 3:50 to 5:50
recreate the "Number Guessing Game" program from yesterday
use the random number generator to generate a new random number every time the program is started over
step through your program using the Codespaces debugger to see what the random number is when testing your code
Note: since you are generating a random number, there is no need for a constants.py file
if the user guesses the correct number, it says: "You guessed correct!"
otherwise, it says: "You guessed wrong! The correct answer was: <correct number>"
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++
in C++ you will have to explicitly set the seed (hopefully to the clock) or you will always get the same random number
#include <iostream>
#include <cstdlib>
int main() {
// declare integer to hold random number
int someRandomNumber;
// initialize random seed
srand((unsigned)time(NULL));
// generate a random number between 1 and 100
someRandomNumber = rand() % 100 + 1;
std::cout << someRandomNumber << std::endl;
}