Chapter 12 (New Book)
Chapter 12 (New Book)
ALGORITHM DESIGN
AND
PROBLEM SOLVING
Topic Chapter 12
Computational Thinking Algorithm Design and Problem Solving
• WHAT IS AN ALGORITHM?
- A SEQUENCE OF DEFINED STEPS THAT CAN BE
PERFORMED TO CARRIED OUT TO PERFORM A TASK.
Topic Chapter 12
Algorithm Design and Problem Solving
Algorithm design
BRANCHES OF ALGORITHM:
i. FLOWCHART
ii. PSEUDOCODE
Topic Chapter 12
Algorithm Design and Problem Solving
Expressing algorithms
PROGRAM
EXECUTED.
TIMES
• INPUT A
• INPUT B
• SUM A + B
• PRINT SUM
SELECTION
TYPES OF SELECTION
Selection
IF… CASE
ELSE..END OF..OTHERWISE..EN
IF D CASE
SELECTION
IF (<CONDITION>)
THEN <STATEMENTS>
ELSE <STATEMENTS>
ENDIF
SELECTION
• SO LET’S SAY WE WANT TO EXPRESS THE FOLLOWING
ALGORITHM TO PRINT OUT THE BIGGEST OF ANY TWO
NUMBERS:
INPUT OR READ A
All the syntax of
INPUT OR READ B keywords should be all
capital which are shown
IF (A > B) THEN as blue colour in the
diagram
PRINT OR OUTPUT A
ELSE
PRINT OR OUTPUT B
ENDIF
SELECTION
INPUT OR READ A
INPUT OR READ B
INPUT OR READ C
IF (A > B)THEN // A IS BIGGER THAN B
IF (A > C) THEN // A IS BIGGER THAN C
PRINT A // A IS BIGGEST
ELSE
PRINT C // C IS BIGGEST
ENDIF
ELSE // B IS BIGGER THAN A
IF (B > C)THEN // B IS BIGGER THAN C
PRINT B // B IS BIGGEST
ELSE
PRINT C // C IS BIGGEST
ENDIF
ENDIF
PSEUDOCODE FORMAT: SELECTION
CASE ….OF…OTHERWISE…
ENDCASE CASE….ENDCASE
• EXAMPLE:
INPUT MARKS
CASE OF MARKS
CASE 0 TO 39
PRINT “FAIL”
CASE 40 TO 59 Marks Output Message
PRINT “PASS” 0 - 39 “FAIL”
CASE 60 TO 79
40 - 59 “PASS”
PRINT “CREDIT”
CASE 80 TO 100 60 - 79 “CREDIT”
PRINT “DISTINCTION” 80 - 100 “DISTINCTION”
OTHERWISE
Marks more than “INVALID
PRINT “INVALID MARKS“ 100 and less than MARKS“
ENDCASE 0
Topic Chapter 12
Variables Algorithm Design and Problem Solving
For example, the variable used to store a count of how many guesses have
been made in a number guessing game might be given the identifier
NUMBEROFGUESSES and the player’s name might be stored in a variable
called THISPLAYER, shown in figure 12.04.
you can imagine these variables like boxes with name labels on them. when a
value is input, it is stored in the box with the specified name (identifier) on it.
Topic Chapter 12
Identifier Naming Conventions Algorithm Design and Problem Solving
12.05 Assignments
Assigning a value:
The following pseudocode stores the value to an identifier
Number
(See Figure 12.05(a)).
The following pseudocode stores the value 1 in the variable
with the identifier NumberOfGuesses (see Figure
12.05(b)).
NumberOfGuesses 1
Topic Chapter 12
Updating a value Algorithm Design and Problem Solving
• UPDATING A VALUE
count count +
count 0
1
0 1
count count
(a (b
) )
Topic Chapter 12
Algorithm Design and Problem Solving
Copying a value
• COPYING A VALUE
Value2 Value1
15
(a Value1 Value2
)
15 15
(b Value1 Value2
)
Topic Chapter 12
Swapping two values Algorithm Design and Problem Solving
Pseudocode of
Swapping:
Temp Value1
Value1 Value2
Value2 Temp
ITERATION
ITERATION
Iteration
Count Condition
Controlled loop Controlled loop
Example: For… Pre- Post-
Next condition condition
Ex: While..End Ex: Repeat
While …..Until
ITERATION
WHILE (<CONDITION>)
DO <STATEMENTS>
ENDWHILE
ITERATION
TOTAL 0
FOR Num 1 TO 100
TOTAL TOTAL + Num
NEXT Num
PRINT TOTAL
PSEUDOCODE
Add up the numbers from 1 to 100 and print
out the result (using Condition-controlled
Loop)
PSEUDOCODE
Add up the numbers 1 to 100 and print out
the result (using Condition-controlled Loop)
TOTAL 0
Num 1
WHILE (Num<=100)/(Num<101)/(Num<>101)
TOTAL TOTAL + Num
Num Num + 1
ENDWHILE
PRINT TOTAL
PSEUDOCODE
Add up the numbers 1 to 100 and print out
the result (using Condition-controlled Loop)
TOTAL 0
A 1
REPEAT
TOTAL TOTAL + A
A A + 1
UNTIL (A > 100) / UNTIL (A = 101)
PRINT TOTAL
PSEUDOCODE
Working 1: Write a pseudocode to calculate and
print the sum of the following series using three
different loop structures.
1+2+3+4+…….+100 =?
WHILE..ENDWHILE
FOR…ENDFOR REPEAT….UNTIL Loop
Loop Loop
PSEUDOCODE
Working 1: Write a pseudocode to calculate and
print the sum of the following series using three
different loop structures.
1+2+3+4+…….+100 = ?
FOR…NEXT WHILE..END WHILE REPEAT….UNTIL
Loop Loop Loop
0+2+4+6+…….+100 = ?
Instructions:
Homework is given on LMS as assignment
Do it in your school copy
Take picture and convert it into pdf file
Attach the file as an attachment on LMS
WORKING 5:(O-LEVEL JUNE 2004)
1. USING PSEUDOCODE, OR OTHERWISE, WRITE AN ALGORITHM THAT WILL
INPUT THE HOURLY TEMPERATURES FOR ONE DAY IN CENTIGRADE AND PRINT
OUT IN FAHRENHEIT.
• THE MAXIMUM TEMPERATURE
• THE MINIMUM TEMPERATURE
• THE AVERAGE TEMPERATURE FOR THAT DAY.
FORMULA: F (C * 1.8) + 32
SOLUTION(USING THREE DIFFERENT
LOOPS):
FOR……NEXT WHILE…… END REPEAT ……UNTIL
WHILE
SOLUTION(USING THREE DIFFERENT
LOOPS):
FOR……NEXT WHILE…… END WHILE REPEAT ……UNTIL
total 0 total 0 total 0
min 100 min 100 min 100
max -100 max -100 max -100
FOR count 1 TO 24 count 1 count 1
INPUT temp WHILE count <= 24 REPEAT
F (temp * 1.8) + 32 INPUT temp INPUT temp
total total + F F (temp * 1.8) + 32 F (temp * 1.8) + 32
IF F < min THEN min total total + F total total + F
F IF F < min THEN min IF F < min THEN min
IF F > max THEN max F F
F IF F > max THEN max IF F > max THEN max
ENDFOR F F
average total /24 count count + 1 count count + 1
PRINT average, min, ENDWHILE UNTIL count > 24
max average total/24 average total/24
PRINT average, min, PRINT average, min, max
max
Topic Chapter 12
Worked example 12.01 Algorithm Design and Problem Solving
Topic Chapter 12
Worked example 12.01 Algorithm Design and Problem Solving
Topic Chapter 12
Task 12.03 Algorithm Design and Problem Solving
Flow chart :
Topic Chapter 12
Task 12.06 Algorithm Design and Problem Solving
• The selection construct in Table 12.01 uses a condition to follow either the
first group of steps or the second group of steps (see Figure 12.10).
• condition consists of at least one logic proposition. Logic propositions use
the relational (comparison) operators shown in Table 12.03.
Conditions are either TRUE or FALSE. In pseudocode, we distinguish between the relational
operator =
(which tests for equality) and the assignment symbol ←.
Topic Chapter 12
Algorithm Design and Problem
Nested If Solving
NESTED IF :
The first time round the loop, counter is set to 2, the next time
round the loop. Counter has automatically increases to 3, and so
on. The last time round the loop, counter has value 10.
Chapter 12
Topic Algorithm Design and Problem
Iteration/Loops/Repetition Solving
Pseudocode :
INPUT NumberOfRows
INPUT NumberOfColumns
INPUT Symbol
FOR RowCounter 1 TO NumberOfRows
FOR ColumnCounter 1 TO NumberOfColumns
OUTPUT Symbol // Without moving to next line
NEXT ColumnCounter
OUTPUT Newline // move to the next line
NEXT RowCounter
Topic Chapter 12
Algorithm Design and Problem Solving
Worked example 12.09:
Top-down design of a
solution
• As problems solved by computer
programs have become bigger and
more complex, it has become difficult
for one person to devise a solution or
to implement it on their own.
• A Modular, top-down design can
handle such problems.
TOPIC:
CHAPTER -12
STEPWISE REFINEMENT ALGORITHM
DESIGN AND PROBLEM SOLVING
Calculate Wages
INPUT Hours
CALCULATE OUTPUT
INPUT Rate of
Take home pay Pay details
pay
CALCULAT
CALCULATE OUTPUT Pay ,
E CALCULATE
Tax and Hours, Total
Hours * Pay-Tax-
other Pay , Tax , Other
Rate of Deduction
deduction Deduction , Take
pay
home Pay
TOPIC: CHAPTER -12
STEPWISE REFINEMENT ALGORITHM DESIGN AND PROBLEM SOLVING
Our first attempt at solving this problem using structured English is:
01 Set up initial values
02 REPEAT
03 Output number of spaces
04 Output number of symbols
TOPIC: CHAPTER -12
STEPWISE REFINEMENT ALGORITHM DESIGN AND PROBLEM SOLVING
• 02 REPEAT
03 // Output number of spaces
03.1 FOR i ← 1 TO NumberOfSpaces
03.2 OUTPUT Space // without moving to next line
03.3 NEXT i
04 // Output number of symbols
04.1 FOR i ← 1 TO NumberOfSymbols
04.2 OUTPUT Symbol // without moving to next line
04.3 NEXT i
04.4 OUTPUT Newline // move to the next line
05 // Adjust Values For Next Row
05.1 NumberOfSpaces ← NumberOfSpaces – 1
05.2 NumberOfSymbols ← NumberOfSymbols + 2
06 UNTIL NumberOfSymbols > MaxNumberOfSymbols
TOPIC: CHAPTER -12
STEPWISE REFINEMENT ALGORITHM DESIGN AND PROBLEM
SOLVING
PROCEDURE SetValues
INPUT Symbol
CALL InputMaxNumberOfSymbols // need to
ensure it is an odd number
NumberOfSpaces ← (MaxNumberOfSymbols - 1) /
2
NumberOfSymbols ← 1
ENDPROCEDURE
PROCEDURE InputMaxNumberOfSymbols
REPEAT
INPUT MaxNumberOfSymbols
UNTIL MaxNumberOfSymbols MOD 2 = 1
ENDPROCEDURE
TOPIC:
CHAPTER -12
STEPWISE REFINEMENT ALGORITHM DESIGN
AND PROBLEM SOLVING
PROCEDURE OutputSpaces
FOR Count1 ← 1 TO NumberOfSpaces
OUTPUT Space // without moving to next line
NEXT Count1
ENDPROCEDURE
PROCEDURE OutputSymbols
FOR Count2 ← 1 TO NumberOfSymbols
OUTPUT Symbol // without moving to next line
NEXT Count2
OUTPUT Newline // move to the next line
ENDPROCEDURE
PROCEDURE AdjustValuesForNextRow
NumberOfSpaces ← NumberOfSpaces – 1
NumberOfSymbols ← NumberOfSymbols + 2
ENDPROCEDURE
Topic:
Chapter -12
Advantage of Top-Down Design Algorithm
Design and Problem Solving