Problem Solving Skills

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 75

Problem Solving Skills

Lecture 9
Outline

• Information Processing Cycles


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

2
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.

3
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

4
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

5
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

6
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

7
Problem to Solve

What steps would you propose to solve the following problem?

Your hair are dirty/greasy and want to clean up

8
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

9
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

10
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.
11
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.

12
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”
13
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
14
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

15
Rules of Algorithm

• For Input: Get a,b

Use keyword “Input” or “Get” followed by • For Output:


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

16
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
X=8 of the Algorithm.
Set X:=8
X:= 8
X<- 8

17
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

18
Problem Solving
Structure Theorem
Tools and techniques for solving a problem

19
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.

20
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.

21
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.

22
Rules for Selection statement

• One Option • Multiple Conditions


IF (condition) then IF (condition) then
<<steps>> <<steps>>
Endif Else if(condition) then
<<steps>>
Else if (condition) then
• Two Options
<<steps>>
IF(condition) then
Else
<<steps>>
<<steps>>
Else
Endif
<<steps>>
Endif
23
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

24
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

25
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.

26
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     

27
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

28
Do While Loop

• The DO WHILE statement performs number =1


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

• Example
29
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.

30
Example

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

31
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

32
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.

33
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.

34
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)

35
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

36
Flow Chart

37
Quick Recap

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

• Modules (subroutines/ functions/ methods)


• Break down bigger algorithms into chunks
• Improves Clarity and Reuse

• Variables
• Are named things with a value (like in algebra)
• Can make algorithms more flexible
• Can also improve Reuse

38
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

39
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.

40
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.

41
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

42
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 43
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 44
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
45
Trace Table

46
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

47
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 - - -

48
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

49
– 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 - - - - - - - - -

50
Example 3
– Algorithm : Take five inputs from user, calculate 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
5. sum := sum + num 16. Print “Average is ” average

6. Input num 17. End

7. sum := sum + num


8. Input num
9. sum := sum + num
51
– 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 -
52
– 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 - - - - -

53
– 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
54
– 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

55
– 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
21 End if 24 19 34 18 75 170 76 34 5
56
– 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

57
Take five inputs from user, calculate and display their sum
Example 5 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
13. End 58
Take five inputs from user, calculate and display their sum and average

Example 5 (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
59
# 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
60
# 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 - - - - - - -

61
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

62
Take five inputs from user, calculate and display their sum
Example 5.1 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
13.End 63
Take five inputs from user, calculate and display their sum and average

Example 5 (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
64
# 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
65
# 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 - - - - - - -

66
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

67
Same problem can be done using for loop. Increment
statement will work as last line in the loop.

68
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 , totalNumbers := 0, 11. totalNumbers := totalNumbers +1
loopCounter := 0
12. sum := sum + num
4. Repeat while (true)
13. End while
5. Begin
6. loopCounter = loopCounter + 1 14. average := sum / totalNumbers
7. input num 15. Print “Total Sum is ” sum
8. display “Enter input ” loop counter “:” 16. Print “Average is ” average
9. if (num == -1)
13. End
10. then break
11. end if
69
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

70
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 , totalNumbers := 0, 11. if (num mod 2 = 0)
loopCounter := 0
12. then sumOfEvens := sumOfEvens +num
4. Repeat while (true)
13. else sumOfOdds := sumOfOdds +num
5. Begin
6. loopCounter = loopCounter + 1 14. end if
7. input num 15. End while
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
71
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

72
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

73
Practice Questions

• Factorial of given number


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

74
Summary

• In this lecture, we have covered:


• Algorithm creation
• Trace table and dry run
• Use of loops and decisions

75

You might also like