24
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design

CPSC 171 Introduction to Computer Science

  • Upload
    danae

  • View
    40

  • Download
    0

Embed Size (px)

DESCRIPTION

CPSC 171 Introduction to Computer Science. Algorithm Discovery and Design. Lab 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. Reading Assignment. - PowerPoint PPT Presentation

Citation preview

Page 1: CPSC 171 Introduction to Computer Science

CPSC 171 Introduction to Computer Science

Algorithm Discovery and Design

Page 2: CPSC 171 Introduction to Computer Science

Lab Hours, Colton 106

Sunday: Matt YoungMonday: Will MooneyTuesday: Cody DepewWednesday: Christian EbingerThursday: Stefan Maurer

Lab will be open 8-10pm starting Monday night, Sept. 7

Page 3: CPSC 171 Introduction to Computer Science

Reading Assignment

Chapter 1 in Textbook

Homework Assignmentpage 34 of TextbookProblems 5,7,8,9,13Due Sept 4 at beginning of class

Page 4: CPSC 171 Introduction to Computer Science

Problem 3 Page 34

Read problem in bookFollow along on board

Page 5: CPSC 171 Introduction to Computer Science

An Algorithm is

A well-ordered collection ofUnambiguous andEffectively computable operations

that, when executedProduces a result andHalts in a finite amount of time

Textbook definition

Page 6: CPSC 171 Introduction to Computer Science

Representing Algorithms

How do we represent algorithms?EnglishProgramming LanguageWe will start with pseudocode and later will look at various programming languages.

Page 7: CPSC 171 Introduction to Computer Science

Pseudocode

Set of English Constructs designed to resemble statements in programming languagesSteps presented in structured mannerSimple, Highly readable

Page 8: CPSC 171 Introduction to Computer Science

Constructs We Will Use

Sequential operations to carry out

computation, input, and output.Conditional operations.Iterative operations.

Page 9: CPSC 171 Introduction to Computer Science

Sequential Operationscomputation

Set the value of X to 3. Assign X a value of A + B. X=2 - 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".

Page 10: CPSC 171 Introduction to Computer Science

Example Algorithm (Sequential)

Figure 2.3Algorithm for Computing Average Miles per Gallon

Page 11: CPSC 171 Introduction to Computer Science

Practice

Write an algorithm in pseudocode to:

Get the radius of a circle as input and output the circumference and area of the circle

Page 12: CPSC 171 Introduction to Computer Science

Conditional Operation

If “a true/false condition” is true then

first set of algorithmic operations

Elsesecond set of algorithm

operationsNotice the indentation

Page 13: CPSC 171 Introduction to Computer Science

Example Algorithm (conditionals)

Figure 2.5Second Version of the Average Miles per Gallon Algorithm

Page 14: CPSC 171 Introduction to Computer Science

Practice

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.

Page 15: CPSC 171 Introduction to Computer Science

Conditional Operation Picture

true-falsestatement

true false

operation

operation

operation

operation

false branch

true

branch

Note: either branch can be missing

Page 16: CPSC 171 Introduction to Computer Science

true-falsestatement

true false

operation

operation

operation

operation

false branch

true

branch

Note: either branch can be missing

Conditional Operation Picture

Page 17: CPSC 171 Introduction to Computer Science

true-falsestatement

true false

operation

operation

operation

operation

false branch

true

branch

Note: either branch can be missing

Conditional Operation Picture

Page 18: CPSC 171 Introduction to Computer Science

Iterative OperationsSet count to 5While count < 4

Add 1 to countPrint count

stop

Set count to 5Do

Add 1 to countPrint count

While count < 4stop

Notice when the continuation condition is checked

Pretest loop Posttest loop

Page 19: CPSC 171 Introduction to Computer Science

Infinite Loops

Set count to 0While count >= 0

Add 1 to countPrint count

stop

Page 20: CPSC 171 Introduction to Computer Science

Iteration Operation Picture

true-falsestatement

true-falsestatement

false

true

true

operation

operation

false

operation

operation

while loop Do/while loop

Page 21: CPSC 171 Introduction to Computer Science

Figure 2.7

Third Version of the Average Miles per Gallon Algorithm

Example Algorithm (iteration)

Page 22: CPSC 171 Introduction to Computer Science

Practice

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.

Page 23: CPSC 171 Introduction to Computer Science

Summary of Operation Typesfor Algorithms

Sequential operations to carry out

computation, input, and output.Conditional operations.Iterative operations.

Page 24: CPSC 171 Introduction to Computer Science

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.