unit 2 – algorithms & pseudocode. algorithms computer problems solved by executing series of...

23
Unit 2 – Algorithms & Pseudocode

Upload: gerald-rodgers

Post on 05-Jan-2016

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Unit 2 – Algorithms & Pseudocode

Page 2: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Alg

orith

ms

• Computer problems solved by executing series of action in order

• Procedure– The Actions to execute– The Order in which they execute

• This is called an ALGORITHM

Page 3: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Pse

ud

oco

de

• Pseudocode is similar to programming language, it’s not an actual computer programming language

• Good for planning out a problem• Carefully prepared pseudocode can

have statements replaced with C++ statements

• Describes only executable statements (i.e. int i; is not executable, although you may choose to list variables)

Page 4: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Pse

ud

oco

de E

xam

ple

1 Prompt the user to enter the first integer

2 Input the first integer 3 4 Prompt the user to enter the

second integer 5 Input the second integer 6 7 Add first integer and second

integer, store result 8 Display result

Page 5: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Con

trol S

tructu

res

• Sequence– Executes statements in order– Use Activity Diagram to create

Page 6: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

if Sin

gle

-Sele

ction

S

tate

men

t

If student’s grade is greater than or equal to 60

Print “Passed” if ( grade >= 60 )

cout << "Passed";

Page 7: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

if…else

Dou

ble

-S

ele

ction

Sta

tem

en

t

If student’s grade is greater than or equal to 60 Print “Passed”

Else Print “Failed”

if ( grade >= 60 ) cout << "Passed";

else cout << "Failed";

Page 8: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

if…else

Dou

ble

-S

ele

ction

Sta

tem

en

t

Page 9: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Con

ditio

nal O

pera

tor

( ?:)

• cout << ( grade >= 60 ? "Passed" : "Failed" );

• grade >= 60 ? cout << "Passed" : cout << "Failed";

Page 10: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Neste

d if…

else

S

tate

men

ts

If student’s grade is greater than or equal to 90 Print “A”

Else If student’s grade is greater than or equal to 80

Print “B” Else

If student’s grade is greater than or equal to 70

Print “C” Else

If student’s grade is greater than or equal to 60

Print “D” Else

Print “F”

Page 11: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Neste

d if…

else

S

tate

men

ts

if ( studentGrade >= 90 ) // 90 and above gets "A" cout << "A";

else if ( studentGrade >= 80 ) // 80-89 gets "B" cout << "B";

else if ( studentGrade >= 70 ) // 70-79 gets "C" cout << "C";

else if ( studentGrade >= 60 ) // 60-69 gets "D" cout << "D";

else // less than 60 gets "F" cout << "F";

Page 12: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

• Beware the Dangling-else Problem

Page 13: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Cre

ate

you

r ow

n

Pse

ud

oco

de

• Top-down• Stepwise refinement

Page 14: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Cre

ate

you

r ow

n

Pse

ud

oco

de

• Start at the top – a single statement that conveys the overall function of the program– Determine the class average for the

quiz for an arbitrary number of students

• This is a COMPLETE representation

Page 15: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Cre

ate

you

r ow

n

Pse

ud

oco

de

• Refinement• Divide into smaller tasks

– Initialize variable– Input, sum and count the quiz grades– Calculate and print the total of all

student grades and the class average

• Only Sequence structure used

Page 16: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Seco

nd

Refi

nem

en

t

• Commit to specific variables– Initialze variablesRefined to– Initialize total to zero– Initialize counter to zero

Page 17: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Seco

nd

Refi

nem

en

t

– Input, sum and count the quiz grades

• Refine using a repetition statement (loop)

• Since number of grades is unknown – use sentinel-controlled repetition (user enters grades then enters -1 to indicate they’re done

Page 18: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Seco

nd

Refi

nem

en

t

• Prompt the user to enter the first grade

• Input the first grade• While the user has not yet entered

the sentinel– Add this grade into the running total– Add one to the grade counter– Prompt the user to enter the next

grade– Input the next grade

• Finish the pseudocode!

Page 19: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Case

Stu

dy

• Today we identify the Class Attributes

• Classes have Attributes (Data) and operations (Behaviour)

• Class Attributes – data members• Operations – member functions

Page 20: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Iden

tifying

A

ttribu

tes

• Attributes of a person– Height, weight, right or left-handed

• Radio Attributes – Station setting, volume setting, AM or

FM

• Look for descriptive words or phrases

Page 21: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

AT

M A

ttribu

tes

Class Descriptive words and phrases

ATM user is authenticated

BalanceInquiry account number

Withdrawal account number

  amount

Deposit account number

  amount

BankDatabase [no descriptive words or phrases]

Account account number

  PIN

  balance

Screen [no descriptive words or phrases]

Keypad [no descriptive words or phrases]

CashDispenser begins each day loaded with 500 $20 bills

DepositSlot [no descriptive words or phrases]

Page 22: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

How

Attrib

ute

s were

d

ete

rmin

ed

• Class CashDispenser has one attribute. The requirements document states that the cash dispenser “begins each day loaded with 500 $20 bills.” The cash dispenser must keep track of the number of bills it contains to determine whether enough cash is on hand to satisfy withdrawal requests. We assign to class CashDispenser an integer attribute count, which is initially set to 500.

Page 23: Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order

Mod

elin

g A

ttribu

tes