Lecture 12

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

Application of ICT

Lecture # 12

Qurratulann
Department of Computer Science
GC University, Lahore

The slides are adapted from the publisher’s material


Understanding Computers: Today and Tomorrow
Outline
 Information Processing Cycles
 Problem Solving
Problem
Logic Building
Structure theorem
Pseudo codes
Flowcharts
Information Processing Cycle
A computer is a machine that, under a program’s direction and control, performs
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.
Problem Solving
The act of finding a solution to a perplexing, distressing,
vexing, or unsettled question

How do you define problem solving?


Problem Solving
How do you solve problems?
Understand the problem
Devise a plan
Carry out the plan
Look back
Problem Solving Strategies
Ask questions!
 What do I know about the problem?
 What is the information that I have to process in order the find the
solution?
 What does the solution look like?
 What sort of special cases exist?
 How will I recognize that I have found
the solution?
Problem Solving Strategies
Ask questions! Never reinvent the wheel!
 Similar problems come up again and again in different guises
 A good programmer recognizes a task or subtask that has been
solved before and plugs in the solution

Can you think of two similar problems?


Problem Solving Strategies
Divide and Conquer!
Break up a large problem into smaller units and solve each
smaller problem
 Applies the concept of abstraction
 The divide-and-conquer approach can be applied over and over again
until each subtask is manageable
Computer Problem Solving
 Analysis and Specification Phase
• Analyze
• Specification
 Algorithm Development Phase
• Develop algorithm
• Test algorithm
 Implementation Phase
• Code algorithm
• Test algorithm
 Maintenance Phase
• Use
• Maintain
Computer Problem Solving
Algorithms
Algorithm
A set of unambiguous instructions for solving a problem
or subproblem in a finite amount of time using a finite
amount of data
Abstract Step
An algorithmic step containing unspecified details
Concrete Step
An algorithm step in which all details are specified
Developing an Algorithm
Two methodologies used to develop computer
solutions to a problem
 Top-down design focuses on the tasks to be done
 Object-oriented design focuses on the data involved in the solution
Pseudocode
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
Rules of
Algorithm
For Input:
Use keyword “Input” or “Get” followed by a
list of variables separated by a single comma.
Example
Input a
Input a , b
Get a
Get a, b
For Output:
Use keyword “Output”, “Display” or “Print”
followed by a variable name or text. Enclose
“text/message” in inverted commas. Do not
enclose variable name in inverted commas.
Example
Output “Number =” + num
Rules of
Algorithm
Storage/ Assignment
Use the keyword “Set” in combination with “=” or “:=” OR use keyword “=”, “:=” or “<-”
Example
Set X=8 Good Practice:
• Number your steps.
X=8 • Indicate Start and
Set X:=8 End of the
Algorithm.
X:= 8
X<- 8
Multiply Two Numbers
Input: num1 and num2
Output: Product of num1 and num2
Steps:
1. Start
2. Input num1 , num2
3. Product = num1 * num2
4. Print Product
5. End
Problem Solving
Structure Theorem
Tools and techniques for solving a problem
Control
Structures
Control structure
An instruction that determines the order in which other instructions
in a program are executed

Can you name the ones we defined in the functionality of


pseudocode?
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.
Selection
 I have passed my 8th grade, now I have to select between Science and
Arts groups.
Repetition
 If I am fail in a grade I have to repeat it until pass.
Sequence Statements

24
Selection Statements
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.
Rules for Selection statement
One Option Multiple Conditions
IF (condition) then IF (condition) then
steps… steps…
Endif Else if(condition) then
Two Options steps…
IF(condition) then Else if (condition) then
steps… steps…
Else Else
steps… steps…
Endif Endif
Algorithm with Selection
Problem: Write the appropriate dress for a given temperature.
 Write "Enter temperature"
 Read temperature
 Determine Dress
Algorithm with Selection
Determine Dress
IF (temperature > 90)
Write “Texas weather: wear shorts”
ELSE IF (temperature > 70)
Write “Ideal weather: short sleeves are fine”
ELSE IF (temperature > 50)
Write “A little chilly: wear a light jacket”
ELSE IF (temperature > 32)
Write “Philadelphia weather: wear a heavy coat”
ELSE
Write “Stay inside”
Repetition Statements

Flow of control of Repetition statement


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.
While Loop
The while loop is used to repeat a section of code an
unknown number of times until a specific condition is met.
Rules:
while ( condition )
steps…
end while
Example:
Set a to 10, Set b to 1
while (a>=b)
b=b+1
End while
Do While Loop
The DO WHILE statement
performs the action in its body at
least once.
Rules:
Do
steps..
While (condition)
Example
Set a to 10, set b to 11
Do
b=b+1
While (a>=b)
For Loop
For loops are typically used when the number of iterations is known before
entering the loop.
Rules:
For (initialize; condition; change)
steps…
End for
Example
For (number=1; number<10;number++)
Print “The Number is: “ + number
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
Repetition Statements
A count-controlled loop
Set sum to 0
Set count to 1
While (count <= limit) Why is it
Read number called a
Set sum to sum + number count-controlled
loop?
Increment count
Write "Sum is " + sum
Repetition Statements
An event-controlled loop
Set sum to 0
Set allPositive to true
WHILE (allPositive) Why is it
Read number called an
event-controlled
IF (number > 0) loop?
Set sum to sum + number What is the
ELSE event?
Set allPositive to false
Write "Sum is " + sum
Modules in Algorithms
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)
Subprogram Statements
 We can give a section of code a name and use that name
as a statement in another part of the program
 When the name is encountered, the processing in the
other part of the program halts while the named code is
executed
Flow Chart
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
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.
 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
What is flow chart?
A flowchart is a diagrammatic\pictorial representation
of the operations involved in a data processing system.
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.
Flow chart Symbols
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
Example
Algorithm: START

Step 1: Input W,L


Input
Step 2: AL x W W, L

Step 3: Print A
A LxW

Output
W, L

STOP
Example
START

Input
M1,M2,M3,M4

GRADE(M1+M2+M3+M4)/4

N Y
IS
GRADE<50

Print “PASS”
Print “FAIL”

STOP
Trace Table
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
Example 1
– Trace table
Algorithm
1. Start – Let user Input for y is 5
2. Set x := 0 Step Algorithm Lines X Y Output
3. input y
4. x := y * 2 1 Start - - -
5. Output x 2 Set x := 0 0 - -
6. End 3 input y 0 5 -
4 x := y * 2 10 5 -
5 Output x 10 5 10
6 End - - -
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
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 - - - - - - - - -
Example 3
Algorithm : Take five inputs from user, calculate and display
their sum and average (Without using loop)
1. Start 10. Input num

2. Num=0 11. sum := sum + num

3. Set sum := 0 , average := 0 , totalNumbers :=5 12. Input num

4. Input num 13. sum := sum + num

5. sum := sum + num 14. average := sum / totalNumbers

6. Input num 15. Print “Sum is ” sum

7. sum := sum + num 16. Print “Average is ” average

8. Input num 17. End


9. sum := sum + num
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 -


Example 3

# 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 - - - - -
Example 4
Algorithm : Take five inputs from user using only 1 variable for input,
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, 14. Then sumOfEven := sumOfEven +num3
num5=0
15. End if
3. Set totalSum := 0 , sumOfEven := 0,
average := 0 , 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 + 18. End if


num5 19. If (num5 mod 2 = 0 )
6. average := sum / totalNumbers 20. Then sumOfEven := sumOfEven +num5
7. If (num1 mod 2 = 0 ) 21. End if
8. Then sumOfEven := sumOfEven +num1 22. Print “Total Sum is ” sum
9. End if 23. Print “Average is ” average
10. If (num2 mod 2 = 0 ) 24. Print “Sum of evens is ” sumOfEven
11. Then sumOfEven := sumOfEven +num2 25. End
12. End if
Example 4
Assume input: 24, 19, 34, 18, 75
# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve average totalNumb
n ers

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
Example 4
Assume input: 24, 19, 34, 18, 75
# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve average totalNumb
n ers
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
Example 4
Assume input: 24, 19, 34, 18, 75
# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve average totalNumb
n ers

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
Example 5
Take five inputs from user, calculate and display their 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
13. End
Example 5
Assume input: 25, 17, 34, 9, 75
# Algorithm Loop num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Pass / r
Iteration
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 2 25 25 0 5 2 True

10 5 2 25 25 0 5 2 True
Example 5
Assume input: 25, 17, 34, 9, 75
# Algorithm Loop num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Pass / r
Iteration
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 3 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 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
Example 5
Assume input: 25, 17, 34, 9, 75
# Algorithm Loop num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Pass / r
Iteration
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 - - - - - - -
Example 5
Assume input: 25, 17, 34, 9, 75

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
Example 5
Same problem can be done using for loop. Increment statement
will work as last line in the loop.
Example 6
Take as many inputs as user wants using -1 as sentinel value, calculate and
display their sum and average
Algorithm:
1. Start
12. end if
2. input num=0
13. totalNumbers := totalNumbers +1
3. Set sum := 0 , average := 0 , 14. sum := sum + num
totalNumbers := 0, loopCounter := 0
15. End while
4. Repeat while (true)
16. average := sum / totalNumbers
5. Begin 17. Print “Total Sum is ” sum
6. loopCounter = loopCounter + 1 18. Print “Average is ” average
7. input num 13. End
8. display “Enter input ” loop counter “:”
9. if (num == -1)
10. then break
11. end if
Example 6
Create Trace Table Yourself

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
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 not
 Prim number check
 Factorial of given number
 Fibonacci Series

You might also like