lec-02
The Computing Alliance of Mount Royal University Discord Server
#comp-1701
What is programming?
What are Polya's 4 steps to solving a problem?
What is an algorithm? What qualities should it have?
What is pseudocode? Who is it meant for?
BTW: JP's take on definitions
Other courses you may take that drill down further into these steps:
algorithms (a bit more)
hardware
tracing
the measly 7 things a computer can do
You may ask yourself
pseudocode examples
get numEggs from userask user for destruct_seqpwd = PROMPT for passwordget ne from user number of chickens = SAY "How many chickens?"
Input is about getting info from the outside world "into" your algorithm. We will almost always store this info into variables (shown in yellow in these examples).
output "You have [numEggs] eggs. Eggcellent!"display destruct_countdowntell user their password is incorrect
print "Bad data. Bad."
SAY "I owe you {number rocks} pointy rocks."Output is about getting info from inside the algorithm "out" to the outside world.
pseudocode examples
set message = "Are you SURE that's a good idea?"area = width * height
totalBill = subtotal + taxes + 18
numCartons = 0no, not that kind
pwd = PROMPT for passwordnumber of chickens = SAY "How many chickens?"
what does * mean here?
add first number to running total
pseudocode examples
if (truckWeight > 500) then output "The bridge collapses." money = money - 1000 else money = money + 1000
if result is odd
print "hmmm...that's odd...."
if (happy is true and happinessKnown is true) clap hands
pseudocode examples
if cost is 0 print "Free is good." else if cost > 0 and cost < 10 print "That's a reasonable price." otherwise print "{cost} is too rich for me."
while (num1 greater than num2) x = x + 1 num1 = num1 - 1
do 16 times: print "Na" print "Batman!"
until (cup is empty) do take sip if (sip was tasty) then say "mmmmmm" output "all done"
pseudocode examples
if (age pizza < 2 weeks) AND (milk smells OK) then eat pizza drink milk
if (age pizza < 2 weeks) AND (milk smells OK) then eat pizza drink milk
pseudocode examples
One of the more common ratios tossed around is about 10 to 1, attributed to "Uncle Bob" Martin.
When you trace code, you carefully walk through the code line-by-line, pretending YOU are the computer.
You typically track the values of any variables used in the code and also note what kind of outputs happen.
In some online games (say like MTG Arena), you buy some kind of in-game currency (say, gems) with "real-life" currency.
Write an algorithm that gets a number of dollars from a user, calculates how many gems they will get for that many dollars, and reports the number of gems to the user.
Assume 1 dollar can buy 3 gems.
Many kinds of board games have a scoring track, with each player having a marker used to track their score.
Let's say we have a track that starts at 0 and goes on...to infinity. We'll also assume that there are only 2 players for this particular game. Also, for this problem, assume that only one marker can be on a given space - if a player's marker would land on another, they can "skip" that space.
Write an algorithm that takes in the marker locations of the 2 players, a change in score for player 1, and then calculates the ending location of player 1's marker on the track.
ask user for number
set small = number
set large = number
ask user for another number
while user provides a positive number
if number > large
then
set large = number
otherwise
if number < small
then
set small = number
again ask user for yet another number
share with user (large - small)(a) Trace, assuming the user provides these 5 numbers: 4, 6, 11, 3 and -2 when asked to by the algorithm. Expressing the state of all variables at each step and that which the algorithm shares until its completion.
(b) In grammatically complete sentences, express the purpose of the entire algorithm. Not what each step produces, but rather, describe the original problem the programmer intended to solve.
reading algorithms
Ask user for a positive integer
Ask user for another positive integer
Set accumulator to zero
while first number is greater or equal to one
if first number is odd
then
add second number to accumulator
divide first number in half, ignore the decimal,
and set the first number to the resulting integer
double the second number
share accumulatorhardware
c. curtis
hardware
c. curtis
hardware
c. curtis
hardware
c. curtis
hardware
c. curtis
hardware
c. curtis
hardware
7 measly things!
read from an input device
write to an output device
read a value from storage
write a value to storage
"do math"
branch
loop (i.e. repeated branching)