COMP1701-004

fall 2023

lec-02

The Fooooture

PSA

The Computing Alliance of Mount Royal University Discord Server

#comp-1701

Ignore the flexing, OK?

RECALL

  • 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:

  • software engineering
  • project management
  • systems analysis
  • software testing

I HEREBY PREDICT YOU WILL ACT LIKE A HUMAN

let's talk about these things today:

algorithms (a bit more)

hardware

tracing

the measly 7 things a computer can do

What should my algorithm look like?

You may ask yourself

Here are examples of pseudocode

pseudocode examples

input

get numEggs from user
ask user for destruct_seq
pwd = PROMPT for password
get 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

output "You have [numEggs] eggs. Eggcellent!"
display destruct_countdown
tell 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

assignment

set message = "Are you SURE that's a good idea?"
area = width * height
totalBill = subtotal + taxes + 18
numCartons = 0

no, not that kind

pwd = PROMPT for password
number of chickens = SAY "How many chickens?"

what does * mean here?

add first number to running total

pseudocode examples

if, else if, else/otherwise

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

loops

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

indentation is important

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

vs

pseudocode examples

tracing

When people pursue a career in programming, they usually think:

"I'mma gonna spend my time writing code! Yay!"

This is wrong.

Really, really wrong.

Most of your time will be spent reading code - yours, and (mostly) other people's.

One of the more common ratios tossed around is about 10 to 1, attributed to "Uncle Bob" Martin.

One specialized way to read code is to trace it.

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.

  1. It's a critical skill developers must have, and besides...
  2. ...we're totally gonna test you on it.

Let's make an algorithm and then trace it

Here's the problem:

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.

Let's do another.
This one involves an "if".

Here's the problem:

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.

Let's trace A1 from last year.

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

summarizing what an algorithm is doing

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 accumulator

hardware

hardware

c. curtis

hardware

c. curtis

Inputs

hardware

c. curtis

Outputs

hardware

c. curtis

Secondary Storage

hardware

c. curtis

Main Memory

hardware

c. curtis

CPU

hardware

CPU

What can a CPU actually do?

7 measly things!

  1. read from an input device

  2. write to an output device

  3. read a value from storage

  4. write a value to storage

  5. "do math"

  6. branch

  7. loop (i.e. repeated branching)

WAIT...REALLY???

lec-02

By Jordan Pratt

lec-02

algorithms | tracing

  • 232