0% found this document useful (0 votes)
2 views4 pages

cmpt141-ch01 Solutions

The document discusses algorithms and their applications, including examples of pseudocode for everyday tasks like making a phone call and mailing a letter. It also covers the process of refining algorithms and translating pseudocode into Python code for calculating averages. The document emphasizes the differences in levels of abstraction between human thought, pseudocode, and formal programming languages.

Uploaded by

ahmedscorp2025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

cmpt141-ch01 Solutions

The document discusses algorithms and their applications, including examples of pseudocode for everyday tasks like making a phone call and mailing a letter. It also covers the process of refining algorithms and translating pseudocode into Python code for calculating averages. The document emphasizes the differences in levels of abstraction between human thought, pseudocode, and formal programming languages.

Uploaded by

ahmedscorp2025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Algorithms and Computer Programs

Algorithms
Reading Roundup
"Reading Roundup" slides feature questions whose answers can be found in the pre-class readings.

What is an algorithm?
What is the difference between a problem and an algorithm?
What is abstraction?

What is meant by levels of abstraction?


What is refinement?

Exercise 1
Write algorithms in pseudocode for the following problems:
Answers for developing algorithms may vary wildly. The following are just examples of simple algo-
rithms.

(a) Making a phone call from a cellphone


Answer:
(a) Get cellphone
(b) Turn on screen
(c) Pass security screen
(d) Go to contacts and locate person to call
(e) Dial number for contact as found in contacts screen

(b) Mailing a letter to someone


Answer:
(a) Put letter in envelope
(b) Write address on envelope
(c) Attach stamp to envelope
(d) Seal envelope
(e) Go to mailbox and drop envelope off for delivery

1
Example: Mailing a letter
Here’s one possible answer:
(a) Put letter in envelope
(b) Write address on envelope
(c) Attach stamp to envelope
(d) Seal envelope
(e) Go to mailbox and drop off envelope

Abstraction and Refinement


Exercise 2
Choose ONE action from this algorithm and refine it (i.e. make it more detailed)
(a) Put letter in envelope
Answer:
(a) Fold letter such that it will fit in envelope
(b) Lift flap of envelope
(c) Slide folded letter into envelope

(b) Write address on envelope


Answer:
(a) Pick up writing implement
(b) Write recipient’s name in center of envelope
(c) Write recipient’s address below their name
(d) Write your name and address in top left corner

(c) Attach stamp to envelope


Answer:
(a) Lick back of stamp
(b) Put stamp on top right corner of envelope
(c) Press down on stamp with thumb

(d) Seal envelope


Answer:
(a) Lick back of envelope flap
(b) Close envelope flap
(c) Press envelope flap tightly against back of envelope

(e) Go to mailbox and drop off envelope

2
Answer:
(a) Walk to mailbox
(b) Grab handle to open mailbox
(c) Insert envelope into open slot

Computing an Average

You have a bunch of numbers How do you


calculate the average of the numbers?
Calculating an average
(a) Sum all the numbers
(b) Divide the sum by how many numbers there are

Demo 1
Translate the following pseudocode into Python:
Algorithm Average :
Input : a set of numbers

let total = 0;
for each number x in the set of numbers :
let total = total + x
let average = total / size of the set of numbers

Solution

# CMPT 141 - Algorithms & Computer Programs


# Topic : Algorithms
# DEMO

def Average ( S ):
# you should be able to see that this code here
# looks a lot like the pseudocode algorithm
total = 0
for x in S :
total = total + x
average = total / len ( S )
return average

# Print out some example uses of Average


print ( " Average of 0 to 5 is " , Average ( range (5)))
print ( " Average of 0 to 10 is " , Average ( range (10)))
print ( " Average of 0 to 100 is " , Average ( range (100)))
This code can be found in cmpt141-ch01-py/cmpt141-ch01-demo-01.py.

The point here is not to understand why the Python program looks the way it does, but to be able to see
which lines in the pseudocode algorithm become which lines in the Python code, which is something

3
you should be able to appreciate at this point. Parts of this program look a lot like the pseudocode
algorithm, but, unlike the pseudocode, everything written in the Python program has a strictly defined,
unambiguous meaning.
The green lines are called "comments" and are not actually part of the computer program. All text after
a # character on a line is ignored by Python and is only used to describe what the program is doing.

3 Levels of Thought

For humans: Simple. Direct. Concise. Pseudocode: More verbose. Actions in

terms of alien capabilities. Computer Program: Formal language. Precise

meanings of terms.

You might also like