Title: CPSC 171 Introduction to Computer Science
1CPSC 171 Introduction to Computer Science
- Algorithm Discovery and Design
2Lab Hours, Colton 106
- Sunday Matt Young
- Monday Will Mooney
- Tuesday Cody Depew
- Wednesday Christian Ebinger
- Thursday Stefan Maurer
- Lab will be open 8-10pm starting Monday night,
Sept. 7
3Reading Assignment
Homework Assignment
page 34 of Textbook Problems 5,7,8,9,13 Due Sept
4 at beginning of class
4Problem 3 Page 34
- Read problem in book
- Follow along on board
5An Algorithm is
- A well-ordered collection of
- Unambiguous and
- Effectively computable operations that, when
executed - Produces a result and
- Halts in a finite amount of time
Textbook definition
6Representing Algorithms
- How do we represent algorithms?
- English
- Programming Language
- We will start with pseudocode and later will look
at various programming languages.
7Pseudocode
- Set of English Constructs designed to resemble
statements in programming languages - Steps presented in structured manner
- Simple, Highly readable
8Constructs We Will Use
- Sequential operations to carry out
- computation,
- input, and
- output.
- Conditional operations.
- Iterative operations.
9Sequential Operations
- computation
- Set the value of X to 3.
- Assign X a value of A B.
- X2 - C.
- Set the value of Name to the first person's name.
- input
- Get a value for X, Y, and Z.
- Input values for A1, A2, ..., Am.
- Read X, Y, and Carry.
- output
- Output the value of X.
- Print values for X, Y, and Carry.
- Print the message, "Error".
10Example Algorithm (Sequential)
- Figure 2.3
- Algorithm for Computing Average Miles per Gallon
11Practice
- Write an algorithm in pseudocode to
- Get the radius of a circle as input and output
the circumference and area of the circle
12Conditional Operation
- If a true/false condition is true then
- first set of algorithmic operations
- Else
- second set of algorithm operations
Notice the indentation
13Example Algorithm (conditionals)
- Figure 2.5
- Second Version of the Average Miles per Gallon
Algorithm
14Practice
- Modify your algorithm to print large circle if
the circumference is greater than 100 and small
circle if it is equal to or less than 100.
15Conditional Operation Picture
true-false statement
true
false
true branch
false branch
operation
operation
operation
Note either branch can be missing
operation
16Conditional Operation Picture
true-false statement
true
false
true branch
false branch
operation
operation
operation
Note either branch can be missing
operation
17Conditional Operation Picture
true-false statement
true
false
true branch
false branch
operation
operation
operation
Note either branch can be missing
operation
18Iterative Operations
- Set count to 5
- While count lt 4
- Add 1 to count
- Print count
- stop
Set count to 5 Do Add 1 to count Print
count While count lt 4 stop
Pretest loop
Posttest loop
Notice when the continuation condition is checked
19Infinite Loops
- Set count to 0
- While count gt 0
- Add 1 to count
- Print count
- stop
20Iteration Operation Picture
true-false statement
false
operation
true
operation
operation
operation
true-false statement
true
false
while loop
Do/while loop
21Example Algorithm (iteration)
- Figure 2.7
- Third Version of the Average Miles per Gallon
Algorithm
22Practice
- Write an algorithm that gets as input a single
data value, x, and outputs the three values x2,
sin x, 1/x. This process should repeat until the
input value for x is equal to 99.
23Summary of Operation Typesfor Algorithms
- Sequential operations to carry out
- computation,
- input, and
- output.
- Conditional operations.
- Iterative operations.
24 There is a theorem in theoretical computer
science that proves that these operations are
sufficient to represent ANY algorithm! Algorithms
are used to do everything you see on a computer!
- Do word processing.
- Fly NASA probes to the planets.
- Run the international telephone switching system.
- Create CAT scan images.
- Process your pay checks.
- Run computer games.
- Etc.