Unit-7 Algorithm Design Problem Solving Part2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 71

ALGORITHM DESIGN

AND
PROBLEM SOLVING
GRADES 9-10 IGCSE
METHODS USED TO DESIGN AND
CONSTRUCT A SOLUTION TO A PROBLEM
Learning Targets:
• To learn methods used to design and
construct solutions to problems through
• Structure Diagrams
• Flowcharts
• Pseudocodes
STRUCTURE DIAGRAMS
ACTIVITY

Create a Structure Diagram after applying the Top Down Design


approach for computing solutions in any of the following areas. You
can choose any other domain / real life scenario other than the
examples listed below:
1. Hospital
2. Hotel
3. Bank
4. Mall
5. Food Ordering App
6. Car Dealer
FLOWCHARTS

A flowchart shows diagrammatically the steps required to


complete a task and the order that they are to be performed.
These steps, together with the order, are called an algorithm.

Flowcharts are an effective way to communicate how the


algorithm that makes up a system or sub-system works.
FLOWCHART SYMBOLS
FLOWCHART SYMBOLS
FLOWCHART SYMBOLS
FLOWCHART SYMBOLS
FLOWCHART SYMBOLS
FLOWCHART ACTIVITY

• Group 1: Draw a flowchart to input any alphabet and check whether it is


vowel or consonant.
• Group 2: Draw a flowchart to input angles of a triangle and check whether
triangle is valid or not.
• Group 3: Draw a flowchart to check if a number is even or odd.
• Group 4: Draw a flowchart to input month number and print number of
days in that month.
• Group 5: Draw a flowchart to check whether a year is leap year or not.
• Group 6: Draw a flowchart to find roots of a quadratic equation.
AGENDA FOR TODAY’S CLASS
1. Program Development Life Cycle
2. Structure Diagrams
3. Flowcharts • Recap of Pseudocode guidelines
4. Pseudocodes • Try examples on writing pseudocodes
PSEUDOCODES
• What is a pseudocode?
PSEUDOCODES

Are pseudocodes useful ?


If yes, how? If no, why?
PSEUDOCODES
How similar or different is a Pseudocode from Code written in a
programming language?
PSEUDOCODES

Similarities Differences
Pseudocode resembles a It uses less strict syntax
program code since it uses
many keywords and operators.

Order of statements is It is independent of any


important in both programming language
PSEUDOCODE GUIDELINES
GUIDELINES FOR WRITING PSEUDOCODES
Pseudocode will handle different types of data.
GUIDELINES FOR WRITING PSEUDOCODES
Pseudocode will use fixed values called as LITERALS.
GUIDELINES FOR WRITING PSEUDOCODES

Pseudocode will use user defined words called as IDENTIFIERS.

Examples:

Variables – Marks, num, amount, name

Functions - Calculate( ), ShowTotal( ), Display( )


GUIDELINES FOR WRITING PSEUDOCODES
IDENTIFIERS must follow identifier naming rules.

Examples: Valid or Invalid


• Temp
• 123Temp
• Temp123
• Temp%
GUIDELINES FOR WRITING PSEUDOCODES
IDENTIFIERS must follow identifier naming rules.

Keywords should never be used as identifier names.

Examples: Valid or Invalid


• Input
• Print
• If
GUIDELINES FOR WRITING PSEUDOCODES

IDENTIFIERS must follow identifier naming rules.

Example:
• Temp
• temp
GUIDELINES FOR WRITING PSEUDOCODES
VARIABLES are program elements whose value can change
during the course of a program.

Example:
• X 🡨100
• Temperature 🡨 34
GUIDELINES FOR WRITING PSEUDOCODES

VARIABLES need to be declared before use.


GUIDELINES FOR WRITING PSEUDOCODES

CONSTANTS are program elements whose value remains the


same during the course of a program.
GUIDELINES FOR WRITING PSEUDOCODES
Use INDENTATION to clarify the structure of a pseudocode,
show hierarchy or logical grouping of statements.
GUIDELINES FOR WRITING PSEUDOCODES
INPUT statement is used to read a value and store it in a variable.
OUTPUT statement is used to display a value stored in a variable.

Multiple outputs
GUIDELINES FOR WRITING PSEUDOCODES

ASSIGNMENT statement is used to assign a value to a variable.


GUIDELINES FOR WRITING PSEUDOCODES

UPPERCASE or lowercase
GUIDELINES FOR WRITING PSEUDOCODES

Arithmetic Operations
GUIDELINES FOR WRITING PSEUDOCODES

Some more … Arithmetic Operations


GUIDELINES FOR WRITING PSEUDOCODES

Logical Operations
GUIDELINES FOR WRITING PSEUDOCODES
Boolean Operations
GUIDELINES FOR WRITING PSEUDOCODES

Conditional statement … If-Else


Examples of If-Else statement
Examples of If-Else statement
GUIDELINES FOR WRITING PSEUDOCODES

Conditional statement … Case statement

• Note that the case clauses are tested


in sequence. When a case that
applies is found, its statement is
executed, and the CASE statement is
complete. Control is passed to the
statement after the ENDCASE. Any
remaining cases are not tested.

CASE statement allows one out


of several branches of code to be • Each case clause is indented by
executed, depending on the value two spaces.
of a variable.
GUIDELINES FOR WRITING PSEUDOCODES

Conditional statement … Case statement


Example of Case statement

• Write a pseudocode that asks the user to input the day number and prints the weekday
corresponding to it.
Example of Case statement

• Write a pseudocode that asks the user to input the day number and prints the
weekday corresponding to it. INPUT daynum
CASE OF daynum
1: OUTPUT “Monday”
2: OUTPUT “Tuesday”
3: OUTPUT “Wednesday”
4: OUTPUT “Thursday”
5: OUTPUT “Friday”
6: OUTPUT “Saturday”
7: OUTPUT “Sunday”
OTHERWISE OUTPUT “Please enter a valid day number”
ENDCASE
ACTIVITY: Level Easy
• Write a pseudo code that reads two numbers and multiplies
them together and print out their product.
• Write a pseudo code that tells a user that the number they
entered is not a 5 or a 6.
• Write pseudo code that performs the following: Ask a user to
enter a number. If the number is between 0 and 10, write the
word blue. If the number is between 10 and 20, write the word
red. if the number is between 20 and 30, write the word green.
If it is any other number, write that it is not a correct color
option.
ACTIVITY: Level Intermediate
Write a pseudocode:
• To input any alphabet and check whether it is a vowel or a
consonant.

• To input angles of a triangle and check whether the triangle is


valid or not.

• To find the maximum between three numbers.


ITERATION / REPETITION / LOOPING STATEMENTS

• https://www.youtube.com/watch?v=eSYeHlwDCNA
• https://www.youtube.com/watch?v=wxds6MAtUQ0&t=29s
1. FOR…TO…NEXT LOOP
• When you know the number of times a loop has to run – Set number of
repetitions
2. WHILE..DO..ENDWHILE LOOP
• This loop repeats itself till a given condition is TRUE.
3. REPEAT … UNTIL LOOP

• This loop repeats itself UNTIL a given condition becomes true.


• The actions in this loop are always completed at least once.
ACTIVITY
Write a pseudocode:

To display the first 10 natural numbers

or

To input the CS marks of all students in our class.


ACTIVITY
Write a pseudocode:
To print all multiples of 5 between 1 and 100 (including both 1 and
100).
ACTIVITY

Write a pseudocode:
To allow a user to input a password. If the correct password
“CS0478” is entered then display “Access allowed to the class”. The
user is allowed 3 chances to enter the right password. If at the end
of 3 chances, correct password hasn’t been entered by the user, then
display a message “Access denied to the class”.
EXPLAINING THE PURPOSE OF AN ALGORITHM
ARRAYS

Example 2:
Example 1:

Example 3:
ARRAYS
ARRAYS
AGENDA FOR TODAY’S CLASS

• Array totalling, counting


• Finding the maximum,
minimum and average in
an array
(Topics 7.4.1, 7.4.2, 7.4.3)
7.4 STANDARD METHODS OF SOLUTION
Array name:
StudentMark

ArraySize: ClassSize
7.4 STANDARD METHODS OF SOLUTION - TOTAL

• Totalling – to add all the values stored in an array and display the
sum.
• Example – to find out the sum total of marks awarded to each student
in a class.
Total ← 0
FOR Counter ← 1 TO ClassSize
Total 🡨 Total + Marks[Counter]
NEXT Counter
OUTPUT Total
7.4 STANDARD METHODS OF SOLUTION - AVERAGE

• Average – find the average among a group of values using a loop


• Example : Calculate the average marks for a class of students.
7.4 STANDARD METHODS OF SOLUTION - COUNT
• Counting – to count the number of times an action is performed.
• Example – to count the number of students that were awarded a pass
mark (>50).

PassCount ← 0
FOR Counter ← 1 TO ClassSize
IF Marks[Counter] > 50
THEN
PassCount ← PassCount + 1
ENDIF
NEXT Counter
OUTPUT PassCount
7.4 STANDARD METHODS OF SOLUTION - MAX
• Maximum – find the maximum among a group of values using a loop
• Example : Find the highest mark awarded in the class.

Max ← Marks[1]
FOR Counter ← 1 TO ClassSize
IF Marks[Counter] > Max
THEN
Max ← Marks[Counter]
ENDIF
NEXT Counter
OUTPUT Max
7.4 STANDARD METHODS OF SOLUTION
• Minimum – find the minimum among a group of values using a loop
• Example : Find the lowest mark awarded in the class.

Min ← Marks[1]
FOR Counter ← 1 TO ClassSize
IF Marks[Counter] < Min
THEN
Min ← Marks[Counter]
ENDIF
NEXT Counter
OUTPUT Min
7.4 STANDARD METHODS
OF SOLUTION
• Linear Search
• Example – Searching
for a name in a class list
of student names
OUTPUT "Please enter name to find "

7.4 STANDARD METHODS


INPUT Name
Found ← FALSE
OF SOLUTION Counter ← 0
WHILE Counter < ClassSize AND
Found=FALSE
• Linear Search
DO
IF Name = StudentName[Counter]
• Example – Searching THEN
Found ← TRUE
for a name in a class list ELSE
of student names Counter ← Counter + 1
ENDIF
ENDWHILE
IF Found= TRUE
THEN
OUTPUT Name, " found at index ",
Counter, " in the list."
ELSE
OUTPUT Name, " not found."
ENDIF
7.4 STANDARD METHODS OF SOLUTION

• Bubble Sorting

Arranging elements in a specific order


(Ascending / Descending)

https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
7.4 STANDARD METHODS OF SOLUTION
• Bubble Sorting

Arranging elements in a specific order


(Ascending / Descending)

https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
ANOTHER EXAMPLE …
7.4 STANDARD METHODS OF SOLUTION • Bubble Sorting

You might also like