Download presentation
Presentation is loading. Please wait.
Published byGerald Rodgers Modified over 9 years ago
1
Unit 2 – Algorithms & Pseudocode
2
Algorithms 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
3
Pseudocode 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)
4
Pseudocode Example 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
5
Control Structures Sequence –Executes statements in order –Use Activity Diagram to create
6
if Single-Selection Statement If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed";
7
if…else Double- Selection Statement If student’s grade is greater than or equal to 60 Print “Passed” Else Print “Failed” if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";
8
if…else Double- Selection Statement
9
Conditional Operator ( ?: ) cout = 60 ? "Passed" : "Failed" ); grade >= 60 ? cout << "Passed" : cout << "Failed";
10
Nested if…else Statements 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”
11
Nested if…else Statements 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";
12
Beware the Dangling-else Problem
13
Create your own Pseudocode Top-down Stepwise refinement
14
Create your own Pseudocode 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
15
Create your own Pseudocode 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
16
Second Refinement Commit to specific variables –Initialze variables Refined to –Initialize total to zero –Initialize counter to zero
17
Second Refinement –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
18
Second Refinement 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!
19
Case Study Today we identify the Class Attributes Classes have Attributes (Data) and operations (Behaviour) Class Attributes – data members Operations – member functions
20
Identifying Attributes 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
21
ATM Attributes ClassDescriptive 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]
22
How Attributes were determined 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.
23
Modeling Attributes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.