0% found this document useful (0 votes)
24 views

L05!06!07 ProblemSolving

This document discusses problem solving skills and provides an overview of information processing cycles, problem solving techniques, and pseudocode and flowcharts. It explains the four basic operations of a computer: input, processing, output, and storage. It then discusses how to solve problems using an algorithmic approach, including defining the problem, developing step-by-step logic using pseudocode, and representing the logic visually with a flowchart. Key aspects of algorithms like definiteness, order, and computability are also covered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

L05!06!07 ProblemSolving

This document discusses problem solving skills and provides an overview of information processing cycles, problem solving techniques, and pseudocode and flowcharts. It explains the four basic operations of a computer: input, processing, output, and storage. It then discusses how to solve problems using an algorithmic approach, including defining the problem, developing step-by-step logic using pseudocode, and representing the logic visually with a flowchart. Key aspects of algorithms like definiteness, order, and computability are also covered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 76

Problem Solving Skills

Fall 2018
Introduction to Information and Communication Technologies
Online Course Room

• piazza.com/cs102_pf_fall18_aumc/spring2019/cs163
• Access code: PF163_AUMC

12/14/2023 2
Outline

• Information Processing Cycles


• Problem Solving
• Problem
• Logic Building
• Structure theorem
• Pseudo codes
• Flowcharts

3
Information Processing Cycle

• A computer is a machine that, under a program’s direction and control, perfroms four basic
operations:
• Input
• Processing
• Output
• Storage

• A program is a sequence of instructions that tells


the computer how to perform these four operations
in order to accomplish a task.

4
1st Basic Operation : Input

• When a computer is required to take an input from a particular source,


whether it is a terminal, a disk or any other device, the verbs Read and Get
are used.
• Example :
• You are writing an essay in MS word
• Computer reads/gets 2 values to add them up

Mehwish Fatima - CIIT Lahore 14/12/2023 5


2nd Basic Operation : Output

• When a computer is required to provide output to a device, the verbs Print,


Write, Output, or Display are used in pseudo code.
• Usually an output Prompt instruction is required before an input Get
instruction.
• Example :
• You are viewing and printing your essay
• Computer displays / writes the added result of 2 values

14/12/2023 6
3rd Basic Operation : Processing

• Arithmetic Operations:
• Mathematical calculation, Formula, and for these, a programmer uses either actual mathematical symbols or
the words for those symbols.

• Comparison or Logical Operations:


• A computer can compare two variables and select one or two alternate actions
• Example:
X = 5, Y = 7 X > Y OR X < Y
Z = X +Y
Z=5+7
Z = 12

14/12/2023 7
4th Basic Operation : Storage

• An information is stored in some location in memory called Variable.


• 3 situations when need to store a value to a variable or memory location:
• To give data an initial value in pseudo code, the verbs Initialize or Set are used
• To assign a value as a result of some processing the symbols '=' or ‘' are written
• To keep a variable for later use, the verbs Save or Store are used

• Example:
• You are downloading a song in your laptop
• Z = X + Y or Z  X + Y

14/12/2023 8
Problem to Solve

What steps would you propose to solve the following problem?

Your hair are dirty/greasy and want to clean up

14/12/2023 9
Problem to Solve
• Problem: Algorithm
Hair are dirty • Turn on water tab
• Wet you hair
• Solution: • Apply shampoo
Wash the hair
• Leather
• How: • Rinse hair
Wash the hair Algorithm • Dry off

14/12/2023 10
Steps in Problem Solving (in computer
programming)

• Computer programming can be divided into two phases:


• Problem solving phase
• Make an ordered sequence of steps that solves a problem
• these sequence of steps is called an algorithm

• Implementation phase
• implement using a programming language

14/12/2023 11
Steps in Problem Solving

• First produce a general algorithm (one can use pseudocode)

• Refine the algorithm successively to get step by step detailed algorithm that
is very close to a computer language.

• Pseudocode is an artificial and informal language that helps programmers


develop algorithms. Pseudocode is very similar to everyday English.
Pseudocode & Algorithm

• Example 1: Write an algorithm to determine a student’s final grade and


indicate whether it is passing or failing. The final grade is calculated as the
average of four marks.
Pseudocode

Pseudocode:
• Input a set of 4 marks
• Calculate their average by summing and dividing by 4
• if average is below 50
Print “FAIL”
else
Print “PASS”
Algorithm

• Detailed Algorithm
• Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Characteristics of Algorithm

• Definite and having Input and Output


• Well-ordered
• The steps are in a clear order

• Unambiguous
• The operations described are understood by a computing agent without further simplification.

• Effectively Computable
• The computing agent can actually carry out the operation

• Algorithms can be executed by a computing agent which is not necessarily a computer

14/12/2023 16
Rules of Algorithm

• For Input: • For Output:


Use keyword “Input” or “Get” followed by a Use keyword “Output”, “Display” or “Print”
list of variables separated by a single followed by a variable name or text.
comma. Good Practice: Enclose “text/message” in inverted
Show a message commas. Do not enclose variable name in
Example before to prompt user
inverted commas.
Input a for input.
Input a , b Example
Get a Output “Enter a number”

Get a,b Display “Your number is ” num

14/12/2023 17
Rules of Algorithm

• Storage/ Assignment
Use the keyword “Set” in combination with “=” or “:=” OR use keyword “=”, “:=” or “<-”
Good Practice:
Example • Number your steps.
Set X=8 • Indicate Start and End
of the Algorithm.
X=8
Set X:=8
X:= 8
X<- 8

14/12/2023 18
Addition of Two Numbers

• Input: number1 and number2


• Output: Sum of number1 and number2
• Steps:
1. Start
2. Input number1 , number2
3. Sum = number1 + number2
4. Display Sum
5. End

14/12/2023 19
Flow Chart

20 14/12/2023
Quick Recap

• Pseudo-code
• High level description of algorithm…
• intended for human reading
• but structured like a programming language

14/12/2023 21
Pseudo code & Flowchart

• There are two commonly used tools to help to build logic (algorithm).
• Pseudo code is an artificial and informal language that helps programmers develop algorithms.
• Pseudo code may be an informal English, combinations of computer languages and spoken
language. Whatever works for you.
• A Flowchart is another algorithm but graphical that shows logic solution.
• Emphasizes individual steps and their interconnections.
• A flowchart must have a start and stop.
• A step in a flowchart must connect i.e. You can’t leave a step “hanging” with no connection. e.g.
control flows from one action to the next

Mehwish Fatima - CIIT Lahore 14/12/2023 22


What is flow chart?

• The flowchart is one of the most basic methods of representing algorithms.


It is useful as a precise method of explanation in some circumstances.
• A flowchart is a diagrammatic\pictorial representation of the operations
involved in a data processing system.

14/12/2023 23
Flow chart Symbols

• Start/End
• Used at the beginning and end of each flowchart.

• Input/Output
• Shows when information/data comes into a program or is printed out.

• Process
• Used to show calculations, storing of data in variables, and other “processes” that take
place within a program.

14/12/2023 24
Flow chart Symbols N
X>7? Y

• Decision
• Used to show that the program must decide whether something (usually a comparison
between numbers) is true or false. YES and NO (or T/F) branches are usually shown.

• Connector
• Used to show that flowchart continues on another page.

• Flow Direction
• Show you how you have to move

14/12/2023 25
Example
START

Algorithm Input
W, L
• Step 1: Input W,L
• Step 2: AL x W
ALxW

• Step 3: Print A
Output
W, L

STOP
Example
START
Input M1,M2,M3,M4
GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0

Print Print
“PASS” “FAIL”

STOP
Example Flowchart

START

Algorithm Input
Lft

• Step 1: Input Lft


• Step 2: Lcm  Lft x 30 Lcm  Lft x 30

• Step 3: Print Lcm Output


Lcm

STOP
Trace Table

29 14/12/2023
Trace Tables & Dry Run

• Algorithm
• A sequence of steps designed to perform a particular task

• Dry run
• Working through a section of a program manually

• Trace table
• A table constructed with a column to identify the instruction executed and columns for the contents of each variable

• Variable
• The identifier associated with a particular memory location used to store data

• Constant
• A data item with a fixed value

14/12/2023 30
Example 1
– Trace table A - indicates Nil

• Algorithm – Let user Input for y s 5


1. Start
Ste Algorithm Lines X Y Output
2. Set x := 0 p
3. input y 1 Start - - -
2 Set x := 0 0 - -
4. x := y * 2
3 input y 0 5 -
5. Output x
4 x := y * 2 10 5 -
6. End 5 Output x 10 5 10
6 End - - -

14/12/2023 31
Example 2
– Algorithm : Take five inputs from user, calculate and display their sum and average (Without
using loop)
1. Start
2. input : num1, num2, num3, num4 and num5
3. Set sum := 0 , average := 0 , totalNumbers :=5
4. input in num1, num2, num3, num4 and num5
5. sum := num1 + num2 + num3 + num4 + num5
6. average := sum / totalNumbers
7. Print “Sum is ” sum
8. Print “Average is ” average
9. End

14/12/2023 32
– Trace table: Take five inputs from user, calculate and display their sum and average
(Without using loop)
Example 2 – Assume input: 25, 17, 34, 9, 75

# Algorithm Lines num1 num2 num3 num4 num5 sum average totalNu Output
mbers
1 Start - - - - - - - - -
2 input : num1, num2, num3, num4 and - - - - - - - - -
num5
3 Set sum := 0 , average := 0 , - - - - - 0 0 5 -
totalNumbers :=5
4 input num1, num2, num3, num4 and 25 17 34 9 75 0 0 5 -
num5
5 sum := num1 + num2 + num3 + num4 + 25 17 34 9 75 0 0 5 -
num5
6 average := sum / totalNumbers 25 17 34 9 75 160 32 5 -
7 Print “Sum is ” sum 25 17 34 9 75 160 32 5 Sum is 160
8 Print “Average is ” average 25 17 34 9 75 160 32 5 Sum is 160
Average is 32
9 End - - - - - - - - -

14/12/2023 33
Example
– Algorithm : Take five inputs from user, calculate
3
10. Input num
and display their sum and average (Without 11. sum := sum + num
using loop)
12. Input num
1. Start
13. sum := sum + num
2. Num=0
14. average := sum / totalNumbers
3. Set sum := 0 , average := 0 , totalNumbers :=5
15. Print “Sum is ” sum
4. Input num
16. Print “Average is ” average
5. sum := sum + num
6. Input num 17. End

7. sum := sum + num


8. Input num
9. sum := sum + num
14/12/2023 34
– Take five inputs from user using only 1 variable for input, calculate and display their
sum and average (Without using loop)
Example 3 – Assume input: 25, 17, 34, 9, 75

# Algorithm Lines num sum average totalNumbers Output

1 Start - - - - -
2 num=0 0 - - - -
3 Set sum := 0 , average := 0 , totalNumbers :=5 - 0 0 5 -
4 Input num 25 0 0 5 -
5 sum := sum + num 25 25 0 5 -
6 input num 17 25 0 5 -
7 sum := sum + num 17 42 0 5 -
8 Input num 34 42 0 5 -
9 sum := sum + num 34 76 0 5 -
10 input num 9 76 0 5 -
11 sum := sum + num 9 85 0 5 -
14/12/2023 35
– Take five inputs from user using only 1 variable for input, calculate and display their
sum and average (Without using loop)
Example 3 – Assume input: 25, 17, 34, 9, 75

# Algorithm Lines num sum average totalNumbers Output

12 Input num 75 85 0 5 -
13 sum := sum + num 75 160 0 5 -
14 average := sum / totalNumbers 75 160 32 5 -
15 Print “Sum is ” sum 75 160 32 5 Sum is 160
16 Print “Average is ” average 75 160 32 5 Sum is 160
Average is 32
17 End - - - - -

14/12/2023 36
– Algorithm : Take five inputs from user using only 1 variable for input,

Example 4 calculate and display their totalSum, sumOfEven and average


(Without using loop)

1. Start 13. If (num3 mod 2 = 0 )


2. Set num1=0, num2=0, num3=0, num4=0, num5=0 14. Then sumOfEven := sumOfEven +num3
3. Set totalSum := 0 , sumOfEven := 0, average := 0 , 15. End if
totalNumbers := 5
16. If (num4 mod 2 = 0 )
4. input num1, num2, num3, num4 and num5
17. Then sumOfEven := sumOfEven +num4
5. sum := num1 + num2 + num3 + num4 + num5
18. End if
6. average := sum / totalNumbers
19. If (num5 mod 2 = 0 )
7. If (num1 mod 2 = 0 )
20. Then sumOfEven := sumOfEven +num5
8. Then sumOfEven := sumOfEven +num1
21. End if
9. End if
22. Print “Total Sum is ” sum
10. If (num2 mod 2 = 0 )
23. Print “Average is ” average
11. Then sumOfEven := sumOfEven +num2
24. Print “Sum of evens is ” sumOfEven
12. End if
25. End

14/12/2023 37
– Algorithm : Take five inputs from user using only 1 variable for input, calculate and
display their totalSum, sumOfEven and average (Without using loop)
Example 4 – Assume input: 24, 19, 34, 18, 75

# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve average totalNum
n bers
1 Start - - - - - - - - -
2 Set num1=0, num2=0, num3=0, num4=0, 0 0 0 0 0 - - - -
num5=0
3 Set sum := 0 , sumOfEven := 0, - - - - - 0 0 0 5
average := 0 , totalNumbers := 5
4 input in num1, num2, num3, num4 and 24 19 34 18 75 0 0 0 5
num5
5 sum := num1 + num2 + num3 + num4 + 24 19 34 18 75 0 0 0 5
num5
6 average := sum / totalNumbers 24 19 34 18 75 170 0 34 5
7 If (num1 mod 2 = 0 ) 24 19 34 18 75 170 0 34 5
8 Then sumOfEven := sumOfEven +num1 24 19 34 18 75 170 24 34 5
9 End if 24 19 34 18 75 170 24 34 5

14/12/2023 38
– Algorithm : Take five inputs from user using only 1 variable for input, calculate and
display their totalSum, sumOfEven and average (Without using loop)
Example 4 – Assume input: 24, 19, 34, 18, 75

# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve average totalNum
n bers
10 If (num2 mod 2 = 0 ) 24 19 34 18 75 170 24 34 5
11 Then sumOfEven := sumOfEven 24 19 34 18 75 170 24 34 5
+num2
12 End if 24 19 34 18 75 170 24 34 5
13 If (num3 mod 2 = 0 ) 24 19 34 18 75 170 24 34 5
14 Then sumOfEven := sumOfEven 24 19 34 18 75 170 58 34 5
+num3
15 End if 24 19 34 18 75 170 58 34 5
16 If (num4 mod 2 = 0 ) 24 19 34 18 75 170 58 34 5
17 Then sumOfEven := sumOfEven 24 19 34 18 75 170 76 34 5
+num4
18 End if 24 19 34 18 75 170 76 34 5
19 If (num5 mod 2 = 0 ) 24 19 34 18 75 170 76 34 5
20 Then sumOfEven := sumOfEven 24 19 34 18 75 170 76 34 5
+num5
14/12/2023 39
21 End if 24 19 34 18 75 170 76 34 5
– Algorithm : Take five inputs from user using only 1 variable for input, calculate and
display their totalSum, sumOfEven and average (Without using loop)
Example 4 – Assume input: 24, 19, 34, 18, 75

# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve average totalNum
n bers
22 Print “ Total Sum is ” sum 24 19 34 18 75 170 76 34 5
23 Print “Average is ” average 24 19 34 18 75 170 76 34 5
24 Print “ Sum of evens is ” sum 24 19 34 18 75 170 76 34 5
25 End - - - - - - - - -

Output
22 Total Sum is 170
23 Total Sum is 170
Average is 34
24 Total Sum is 170
Average is 34
Sum of evens is 76

14/12/2023 40
Take five inputs from user, calculate and display their
Example 5 sum and average (With using loop)

– Algorithm:
1. Start
2. Num=0
3. Set sum := 0 , average := 0 , totalNumbers := 5, loopCounter := 1
4. Repeat while (loopCounter <= totalNumbers)
5. Begin
6. input num
7. display “Enter input ” loop counter “:”
8. sum := sum + num
9. loopCounter = loopCounter + 1
10. End while
11. average := sum / totalNumbers
12. Print “Total Sum is ” sum
13. Print “Average is ” average
14/12/2023 41
13. End
Take five inputs from user, calculate and display their sum and
Example 5 average (With using loop)
– Assume input: 25, 17, 34, 9, 75

# Algorithm Loop Pass / num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Iteration r
1 1 - - - - - - -
2 2 - 0 - - - - -
3 3 - - 0 0 5 1 -
4 4 - - 0 0 5 1 True
5 5 1 - 0 0 5 1 True
6 6 1 25 0 0 5 1 True
7 7 1 25 25 0 5 1 True
8 8 1 25 25 0 5 2 True
9 Repeat ----- go to step 4
9 4 - 25 25 0 5 2 True
10 5 2 25 25 0 5 2 True
14/12/2023 42
# Algorithm Loop Pass / num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Iteration r
11 6 2 17 25 0 5 2 True
12 7 2 17 42 0 5 2 True
13 8 2 17 42 0 5 3 True
9 Repeat ----- go to step 4
14 4 - 17 42 0 5 3 True
15 5 3 17 42 0 5 3 True
16 6 3 34 42 0 5 3 True
17 7 3 34 76 0 5 3 True
18 8 2 34 76 0 5 4 True
9 Repeat ----- go to step 4
19 4 - 34 76 0 5 4 True
20 5 4 34 76 0 5 4 True
21 6 4 9 76 0 5 4 True
22 7 4 9 85 0 5 4 True
23 8 4 9 85 0 5 5 True
14/12/2023 43
# Algorithm Loop Pass / num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Iteration r
9 Repeat ----- go to step 4
24 4 - 9 85 0 5 5 True
25 5 5 9 85 0 5 5 True
26 6 5 75 85 0 5 5 True
27 7 5 75 160 0 5 5 True
28 8 5 75 160 0 5 6 True
9 Repeat ----- go to step 4
29 4 - 75 160 0 5 6 False
Loop ended --- go to step 10
30 10 - 75 160 32 5 6 -
31 11 - 75 160 32 5 6 -
32 12 - 75 160 32 5 6 -
33 13 - - - - - - -

14/12/2023 44
Step Output for total algorithm
6 Enter input 1: 25
6 Enter input 2: 17
6 Enter input 3: 34
6 Enter input 4: 9
6 Enter input 5: 75
11 Total Sum is 160
12 Total Sum is 160
Average is 32

14/12/2023 45
Take five inputs from user, calculate and display their
Example 5.1 sum and average (With using loop)
– Algorithm: (slight modification in condition and loopCounter value)
1. Start
2. num=0
3. Set sum := 0 , average := 0 , totalNumbers := 5, loopCounter := 0
4. Repeat while (loopCounter < totalNumbers)
5. Begin
6. loopCounter = loopCounter + 1
7. input num
8. Display “Enter input ” loop counter “:”
9. sum := sum + num
10. End while
11. average := sum / totalNumbers
12. Print “Total Sum is ” sum
13. Print “Average is ” average 46
14/12/2023
13. End
Take five inputs from user, calculate and display their sum and
Example 5 average (With using loop)
– Assume input: 25, 17, 34, 9, 75

# Algorithm Loop Pass / num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Iteration r
1 1 - - - - - - -
2 2 - 0 - - - - -
3 3 - - 0 0 5 0 -
4 4 - - 0 0 5 0 True
5 5 1 - 0 0 5 0 True
6 6 1 - 0 0 5 1 True
7 7 1 25 0 0 5 1 True
8 8 1 25 25 0 5 1 True
9 Repeat ----- go to step 4
9 4 - 25 25 0 5 1 True
10 5 2 25 25 0 5 1 True
14/12/2023 47
# Algorithm Loop Pass / num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Iteration r
11 6 2 25 25 0 5 2 True
12 7 2 17 25 0 5 2 True
13 8 2 17 42 0 5 2 True
9 Repeat ----- go to step 4
14 4 - 17 42 0 5 2 True
15 5 3 17 42 0 5 2 True
16 6 3 17 42 0 5 3 True
17 7 3 34 42 0 5 3 True
18 8 2 34 76 0 5 3 True
9 Repeat ----- go to step 4
19 4 - 34 76 0 5 3 True
20 5 4 34 76 0 5 3 True
21 6 4 34 76 0 5 4 True
22 7 4 9 76 0 5 4 True
23 8 4 9 85 0 5 4 True
14/12/2023 48
# Algorithm Loop Pass / num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Iteration r
9 Repeat ----- go to step 4
24 4 - 9 85 0 5 4 True
25 5 5 9 85 0 5 4 True
26 6 5 9 85 0 5 5 True
27 7 5 75 85 0 5 5 True
28 8 5 75 160 0 5 5 True
9 Repeat ----- go to step 4
29 4 - 75 160 0 5 5 False
Loop ended --- go to step 10
30 10 - 75 160 32 5 5 -
31 11 - 75 160 32 5 5 -
32 12 - 75 160 32 5 -
33 13 - - - - - - -

14/12/2023 49
Step Output for total algorithm
6 Enter input 1: 25
6 Enter input 2: 17
6 Enter input 3: 34
6 Enter input 4: 9
6 Enter input 5: 75
11 Total Sum is 160
12 Total Sum is 160
Average is 32

14/12/2023 50
Summary

• In this lecture, we have covered:


• Algorithm creation
• Trace table and dry run

14/12/2023 51
Problem Solving
Structure Theorem
Tools and techniques for solving a problem

52 14/12/2023
What is the Structure theorem

• It states that it is possible to write any algorithm by using only three basic control
structures.
• Sequence
• I have to study classes from grade 1 to grade 8.
• I cannot skip any class in order to reach in grade 8.
• Repetition
• If I am fail in a grade I have to repeat it until pass.
• Selection
• I have passed my 8th grade, now I have to select between Science and Arts groups.

14/12/2023 53
Decision/ Selection

• Sometimes we need to put certain condition before performing some action, then action
will depend upon the condition if its fulfilled or not Selection statements help us to get this
thing done
• This construct represents the decision making abilities of the computer to compare two
pieces of information and select one of two alternative actions.
• In pseudocode, selection is represented by the keywords IF, THEN, ELSE and ENDIF
• An IF statement always has a condition to check, often a comparison between a variable and a number.
• The IF statement also must specify what to do if the condition/comparison is true.
• These instructions (for “true”) may come after the word THEN, or they may simply be listed.

14/12/2023 54
Types of Selection

• IF – THEN – ENDIF(Single IF)


• Single IF selection statement either performs (selects) an action if a condition is true or skips the action if the condition is
false.

• IF – ELSE – ENDIF (Double IF)


• The IF-ELSE selection statement performs an action if a condition is true and performs a different action if the condition is
false

• IF – ELSE IF – ELSE – ENDIF (Multiple IF)


• The IF – ELSE IF – ELSE selection statement performs one of many different actions, depending on the value of an
expression.

• Switch (Alternate to Multiple IF)


• The SWITCH selection statement performs one of many different actions, depending on the value of an expression.

14/12/2023 55
Rules for Selection statement

• One Option Endif


IF (condition) then • Multiple Conditions
<<steps>> IF (condition) then
Endif <<steps>>
Else if(condition) then

• Two Options <<steps>>


IF(condition) then Else if (condition) then
<<steps>> <<steps>>
Else Else
<<steps>> <<steps>>
Endif
Decision/ Selection
Problem: Input the marks of the students and display pass if the marks are 50 else display fail.
Start
Step 1: Input Marks
Step 2: IF Marks > 50 THEN

Step 3: Display “Pass”


Step 4: ELSE
Step 5: Display “Fail”
Step 6: ENDIF
Step 7: End

14/12/2023 57
Repetition

• A loop is a repetition of all or part of the commands in a program.


• A loop often has a counter (a variable) and continues to repeat a specified number of times.
• A loop may also continue till than a condition is true or until a certain condition is met (e.g.,
until the end of a file or until a number reaches a set limit)
• Example
• WHILE condition p is true
• Statement(s) to execute
• ENDWHILE

14/12/2023 58
Types of Repetition

• WHILE
• while loop is a control flow statement that allows code to be executed repeatedly based on a
given boolean condition. The while loop can be thought of as a repeating if statement.

• DO WHILE
• The DO WHILE statement performs the action (or group of actions) in its body at least once.

• FOR
• The FOR loop allows code to be repeatedly executed.
• For loops are also typically used when the number of iterations is known before entering the loop.

14/12/2023 59
While Loop

• The while loop is used to repeat a section of code an unknown number of times until a specific
condition is met.
• While loops execute blocks of code over and over again.
• The advantage to a while loop is that it will go (repeat) as often as necessary to accomplish its goal.
• Algo rules:
while ( condition )
<<steps>>
end while

• The "something" should eventually result in the condition being false

14/12/2023 60
Example: While Loop

while (condition)
action
• How it works:
• if condition is true then execute action
• repeat this process until condition evaluates to false

• action is either a single statement or a group of statements.


• Example
while (number<10)
Display “Hello number ” number “player”
Number=Number +1
End while

14/12/2023 61
Do While Loop

• The DO WHILE statement performs the • Example


action (or group of actions) in its body at number =1
least once. Do
Do Display “Hello number ” number “player”
<<steps>> Number=Number +1
While (condition) while (number<10)

14/12/2023 62
For Loop

• A for loop is classified as an iteration statement.


• Unlike many other kinds of loops, such as the while loop, the for loop is often
distinguished by an explicit loop counter or loop variable.
• This allows the body of the for loop to know about the sequencing of each iteration.
• For loops are also typically used when the number of iterations is known before
entering the loop.
• For loops are the shorthand way to make loops when the number of iterations is
known, as a for loop can be written as a while loop.

14/12/2023 63
Example

• RULES
For (initialize; condition; change) • Example
<<steps>> For (number=1; number<10;number++)
End for Display “Hello number ” number “player”
End for
For Vs. While

• For loop is used when you know the number of iterations you have to make,
mean when you know how many times to execute a loop.
• WHILE is used when you are not sure about the iterations but you know
what the condition is and then you can loop that till the condition is met.
• But both can be used in both situations

14/12/2023 65
Repetition

• Pretest loop: evaluation occurs before the statements within the loop are processed

• Posttest loop: evaluation occurs after the statements within the loop are processed
• The step at which the loop starts is called its "entrance"
• The last step performed before completion is called its "exit".
• The conditional test that controls the exit from a loop should be placed either immediately following the entrance
to the loop or following all steps in its body, but never in the middle of the body.
• Loops with tests at entrance are said to pretest i.e while-do.
• Loops with tests after entire body are said to posttest i.e do-while.
• It is imperative that at least one statement within the statement block alter the condition and eventually render it
false, otherwise the logic may result in an endless loop.

14/12/2023 66
Repetition

• Example
Set student_total to 0
WHILE student_total < 10
Read student record
Print student name and address
Add 1 to student_total
ENDWHILE

• The variable student total is initialized before the loop condition is executed. The student
total variable is incremented within the body of the loop so it will eventually stop.

14/12/2023 67
Modules

• Modules break an algorithm into logical parts (like your groups)


• Helps with Clarity and Understandability

• Modules can be reused


• Within the same algorithm
• In a different algorithm

• In Programming Modules can be called:


• Sub-routines (in older languages)
• Functions (in procedural languages like C/C++)
• Methods (in object oriented languages like Java)

14/12/2023 68
Same problem can be done using for loop. Increment statement will
work as last line in the loop.

14/12/2023 69
Take as many inputs as user wants using -1 as sentinel
Example 6 value, calculate and display their sum and average

– Algorithm:
1. Start
2. input num=0 10. end if
3. Set sum := 0 , average := 0 , 11. totalNumbers := totalNumbers +1
totalNumbers := 0, loopCounter := 0
12. sum := sum + num
4. Repeat while (true)
13. End while
5. Begin
14. average := sum / totalNumbers
6. loopCounter = loopCounter + 1
15. Print “Total Sum is ” sum
7. input num
8. display “Enter input ” loop counter “:” 16. Print “Average is ” average
9. if (num == -1) 13. End
10. then break
11. end if 14/12/2023 70
Example 6: Trace table
Step Output for total algorithm
Enter input 1: 25
Enter input 2: 17
Enter input 3: 34
Enter input 4: 9
Enter input 5: 75
Enter input 6: -1
Total Sum is 160
Total Sum is 160
Average is 32

14/12/2023 71
Take as many inputs as user wants using -1 as sentinel
Example 7 value, calculate and display sumOfEvens and sumOfOdds

– Algorithm:
1. Start
2. num=0 10. end if
3. Set sum := 0 , average := 0 , 11. if (num mod 2 = 0)
totalNumbers := 0, loopCounter := 0
12. then sumOfEvens := sumOfEvens +num
4. Repeat while (true)
13. else sumOfOdds := sumOfOdds +num
5. Begin
14. end if
6. loopCounter = loopCounter + 1
15. End while
7. input num
8. display “Enter input ” loop counter “:” 16. Print “Sum of Evens : ” sumOfEvens

9. if (num == -1) 17. Print “Sum of Odds : ” sumOfOdds


10. then break 13. End
11. end if 14/12/2023 72
Example 7: Trace table
Ste Output for total algorithm
p
Enter input 1: 25
Enter input 2: 18
Enter input 3: 34
Enter input 4: 9
Enter input 5: 75
Enter input 6: -1
Sum of Evens : 52
Sum of Evens : 52
Sum of Oddss : 109

14/12/2023 73
Practice Questions

• Temperature converter
• Currency converter
• Leap Year calculator
• Grade calculator
• Number of days in a given month (if and switch)
• Alphabet character is vowel or consonant (if and switch)
• Palindrome tester

14/12/2023 74
Practice Questions

• Factorial of given number


• Prime number
• Fibonacci Series
• (Optional) Using Single * printing line, square, triangle, etc (Nested loops)

14/12/2023 75
Summary

• In this lecture, we have covered:


• What is Problem Solving and its Logic Building
theorem
• What is Structure theorem
• What is algorithm and how to represent it in Pseudo codes and flow chart

14/12/2023 76

You might also like