View
231
Download
0
Category
Preview:
Citation preview
Unit 2 – Algorithms & Pseudocode
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
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)
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
Con
trol S
tructu
res
• Sequence– Executes statements in order– Use Activity Diagram to create
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";
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";
if…else
Dou
ble
-S
ele
ction
Sta
tem
en
t
Con
ditio
nal O
pera
tor
( ?:)
• cout << ( grade >= 60 ? "Passed" : "Failed" );
• grade >= 60 ? cout << "Passed" : cout << "Failed";
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”
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";
• Beware the Dangling-else Problem
Cre
ate
you
r ow
n
Pse
ud
oco
de
• Top-down• Stepwise refinement
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
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
Seco
nd
Refi
nem
en
t
• Commit to specific variables– Initialze variablesRefined to– Initialize total to zero– Initialize counter to zero
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
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!
Case
Stu
dy
• Today we identify the Class Attributes
• Classes have Attributes (Data) and operations (Behaviour)
• Class Attributes – data members• Operations – member functions
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
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]
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.
Mod
elin
g A
ttribu
tes
Recommended