Problem Solving Skills
Problem Solving Skills
Lecture 9
Outline
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
3
1st Basic Operation : Input
4
2nd Basic Operation : Output
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.
6
4th Basic Operation : Storage
• Example:
• You are downloading a song in your laptop
• Z = X + Y or Z X + Y
7
Problem to Solve
8
Problem to Solve
• Problem: Algorithm
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
• Refine the algorithm successively to get step by step detailed algorithm that
is very close to a computer language.
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
• Unambiguous
• The operations described are understood by a computing agent without further simplification.
• Effectively Computable
• The computing agent can actually carry out the operation
15
Rules of Algorithm
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
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
22
Rules for Selection statement
24
Repetition
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
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
28
Do While Loop
• Example
29
For 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
35
Summary
36
Flow Chart
37
Quick Recap
• Pseudo-code
• High level description of algorithm…
• intended for human reading
• but structured like a programming language
• 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?
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: AL x W
ALxW
• 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
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
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
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
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)
# 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
# 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 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
74
Summary
75