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

PF - 3

The document discusses flowcharts and selection structures in programming. It provides examples of pseudocode for simple programs and draws corresponding flowcharts to illustrate the logic and flow. Key concepts covered include: - Flowcharts use standard symbols to represent program elements and flow. - Selection structures like if/else are used to make decisions and choose between alternative paths. - Nested if/else and elif statements allow testing multiple conditions. - Well-designed flowcharts and pseudocode clearly show the program logic and flow of control.

Uploaded by

Saad Nadeem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

PF - 3

The document discusses flowcharts and selection structures in programming. It provides examples of pseudocode for simple programs and draws corresponding flowcharts to illustrate the logic and flow. Key concepts covered include: - Flowcharts use standard symbols to represent program elements and flow. - Selection structures like if/else are used to make decisions and choose between alternative paths. - Nested if/else and elif statements allow testing multiple conditions. - Well-designed flowcharts and pseudocode clearly show the program logic and flow of control.

Uploaded by

Saad Nadeem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

CS1002

Programming Fundamentals
3
Algorithms and Problem-Solving
2 Techniques
3 Flow Charts
4 The Flowchart
 Dictionary Definition: A schematic representation of a sequence of
operation, as in a manufacturing process or computer program
 Technical Definition: A graphical representation of the sequence of
operations in an information system or program
 Information system flowcharts: show how data flows from source documents
through the computer to final distribution to the users
 Program flowcharts: show the sequence of instructions in a single program or
subroutine
 Different symbols are used to draw each type of flowchart
5 The Flowchart

A flowchart
Shows logic of an algorithm
Emphasizes individual steps and their
interconnections

E.g. control flow from one action to another


Flowchart Common Symbols
6

Name Symbol Use in flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input

Denotes a process to be carried out


Rectangle (e.g. addition, subtraction etc.)

Denotes a decision (or branch) to be made.


The program should continue along one of two
Diamond routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes and output operation

Denotes the direction of logic flow in the


Flow Line program
7
8
9
Example 1 (Pseudo code)

Start
Processing
1. Start
declare num1,
2. declare num1, num2, result num2, result
Input
3. input num1
Input n1
4. input num2 Input
5. results  num1 + num2 Input n2
Processing
6. Print results result  n1 + n2

7. End
Print result Output

End
10 Class Task

 Draw flow chart for the following task


 Take 3 numbers from the user and print the average and sum of these three
numbers
11 if Selection Structure

Selection structure
 Choose among alternative courses of action
Pseudocode example:
If student’s grade is greater than or equal to 50
Print “Passed”
 If the condition is true
 Print statement is executed and program continues to next statement
 If the condition is false
 Print statement is ignored and program continues
 Indenting makes programs easier to read
12 If selection structure

 Generic Format

true
condition Statement (s)

false
13 if Selection Structure
Translation into Algorithm
If student’s grade is greater than or equal to 50
Print “Passed”

if ( grade >= 50 )
print "Passed";

 Diamond symbol (decision symbol)


 Indicates decision is to be made
 Contains an expression that can be true or false
 Test condition, follow path

 if structure
 Single-entry/single-exit
14 if Selection Structure

 Flowchart of pseudocode statement

A decision can be made


on any expression.
zero - false
true Print nonzero - true
Marks >= 50 “Passed”
Example:
3 - 4 is true
false
15 Example 2: Flow chart
Start

declare M1, M2,


M3, M4, average
Pseudocode:
Input M1,
1.0 Start M2, M3, M4

2.0 Declare M1, M2, M3, M4, average average  (M1 +


M2+M3+M4)/4
3.0 Input M1, M2, M3, M4
4.0 average = (M1+ M2+ M3+ M4) / 4 true average
>= 60
5.0 if (average > 60) then
false
5.1 Print “PASS” Print
“PASS”
6.0 endif
7.0 End End
16 if/else Selection Structure

 General Structure

true false
if condition is met condition
then
statement_1 (s) Statement_1 Statement_2
(s) (s)
else
statement_2 (s)
endif
17
if/else Selection Structure
 if
 Performs action if condition true
 if/else
 Different actions if conditions true or false
Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”

if ( grade >= 60 )
Print "Passed";
else
Print "Failed";
18 if/else Selection Structure

false true
grade >= 60

print print
“Failed” “Passed”
Example 3: Flow chart
19

Start
Pseudocode:
1.0 Start declare M1, M2,
2.0 Declare M1, M2, M3, M4, average M3, M4, average

3.0 Input M1, M2, M3, M4 Input M1,


4.0 average = (M1+ M2+ M3+ M4) / 4 M2, M3, M4

5.0 if (average > 50) then average  (M1 +


5.1 Print “PASS” M2+M3+M4)/4

6.0 else
true average false
6.1 Print “FAIL” >= 60
7.0 endif
8.0 End Print
Print “PASS” “FAIL”

End
if/else Selection Structure
20
 Nested if/else structures
 One inside another, test for multiple cases
 Once condition met, other statements skipped

if student’s grade is greater than or equal to 90


Print “A”
else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
21

if Logical_Expression :
Indented Code Block
22
23

if Logical_Expression :
Indented Code Block 1
else :
Indented Code Block 2
24
25
if Logical_Expression_1 :
Indented Code Block 1
elif Logical_Expression_2 :
Indented Code Block 2
elif Logical_Expression_3 :
Indented Code Block 3
...
else :
Indented Code Block N
26
x = 10
27 y = 20
z = 30

print("Start")
if x == 10:
print(" Nested If")
if y == 20:
print(" End of Nested If Block ")
else:
print(" End of Nested If-Else Block ")
elif y == 20:
print(" Elif block ")
else:
print(" Nested If")
if z == 30:
print(" End of Nested If Block ")
else:
print(" End of Nested If-Else Block ")
print("Stop")
x = 10
28 y = 20
z = 30

print("Start")
if x == 10:
print(" Nested If")
if y == 20:
print(" End of Nested If Block ")
else:
print(" End of Nested If-Else Block ")
elif y == 20:
print(" Elif block ")
else:
print(" Nested If")
if z == 30:
print(" End of Nested If Block ")
else:
print(" End of Nested If-Else Block ")
print("Stop")
a = 10
b = 20
29 c = 30

avg = (a + b + c) / 3
print("avg =", avg)

if avg > a and avg > b and avg > c:


print("%d is higher than %d, %d, %d" %(avg, a, b, c))
elif avg > a and avg > b:
print("%d is higher than %d, %d" %(avg, a, b))
elif avg > a and avg > c:
print("%d is higher than %d, %d" %(avg, a, c))
elif avg > b and avg > c:
print("%d is higher than %d, %d" %(avg, b, c))
elif avg > a:
print("%d is just higher than %d" %(avg, a))
elif avg > b:
print("%d is just higher than %d" %(avg, b))
elif avg > c:
print("%d is just higher than %d" %(avg, c))
30
31 if/else Selection Structure
 Example
if ( grade >= 90 ) // 90 and above
Print "A";
else if ( grade >= 80 ) // 80-89
Print "B";
else if ( grade >= 70 ) // 70-79
Print << "C";
else if ( grade >= 60 ) // 60-69
Print "D";
else // less than 60
Print "F";
32 if/else Selection Structure
Compound statement
 Set of statements within a pair of braces

if grade >= 60
Print "Passed"
else
Print "Failed";
Print "You must take this course again"
endif
33
34
35
36
Sequence Flowchart
37
Start
Input

Read one
number n1

Read second
number n2
Processing

Sum  n1 + n2
Output

Display
Sum

End
38 Example 1
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”
end if
39 Example 2

 Write an pseudocode and draw a flowchart to


convert the length in feet to centimeter
 Algorithm:
 Input the length in feet (Lft)
 Calculate the length in cm (Lcm) by multiplying Lft
with 30
 Print length in cm (Lcm)
40 Example 3

Write a pseudocode and draw a flowchart to


calculate area of a rectangle
The program should ask the user to input Length
and Width and then display the Area.
Area = Length * Width
41 Example 4
 Write pseudocode and draw a flowchart that will calculate
the roots of a quadratic equation
 ax2 + bx + c = 0

 Hint d = b2 – 4ac and roots are


 X1 = (-b + sqrt(d )) / 2a
 X2 = (-b – sqrt(d)) / 2a
 Real roots are only possible if d > 0
 Cater this as well
42 Example 5
Write a pseudocode that reads two values, determines the largest value
and prints the largest value with an identifying message and draw
flowchart

Pseudocode
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
end if
Step 3: Print “The largest value is”, MAX
43
Example 6
 Write a pseudocode and draw a flowchart
 to read an employee name (NAME), overtime hours worked (OVERTIME), hours
absent (ABSENT)
 determine the bonus payment (PAYMENT)

Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours $50


>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10
44 Example 7
Average of 10 numbers
 Write pseudocode and draw a flowchart to determine the average of 10
numbers taken as input from the user. The program must ask the user to
enter number by displaying a message.
 After taking all 10 numbers average will be calculate and then printed to
the screen.
 Avg = (n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10)/10
45 Example 6
Draw a flowchart that reads two values, determines the largest value and prints the largest
value with an identifying message

Pseudocode
1.0 Declare VALUE1, VALUE2, MAX
2.0 Input VALUE1, VALUE2
3.0 if (VALUE1 > VALUE2) then
3.1 MAX  VALUE1
4.0 else
4.1 MAX  VALUE2
5.0 end if
6.0 Print “The largest value is”, MAX
46 The Repetition Structure
 In flowcharting one of the more confusing things is to separate
selection from looping
 This is because both structures use the diamond as their control
symbol
 In pseudocode we avoid this by using specific keywords to
designate looping
 WHILE/ENDWHILE
 REPEAT/UNTIL
The main difference is that while loops are designed to run while a
condition is satisfied and then terminate once that condition returns
false. On the other hand, until loops are designed to run while the
condition returns false and only terminate when the condition returns
true.
47
while Repetition Structure
Repetition structure
 Action repeated while some condition remains true
Psuedocode
while there are more items on my shopping list
Purchase next item and cross it off my list
while loop repeated until condition becomes false
Example
Declare product = 2;
while ( product <= 1000 )
product = 2 * product;
48 Looping flowchart
49 while Repetition Structure
 Flowchart for while loop

true
product <= 1000 product = 2 * product

false
WHILE / ENDWHILE
50 count = 0
Start
WHILE count < 10
ADD 1 to count
count = 0 WRITE count
ENDWHILE
WRITE “The End”

count <10 Mainline


count = 0
Write WHILE count < 10
add 1 to “The End”
DO Process
count
ENDWHILE
Stop WRITE “The End”
write count
Process
ADD 1 to count
WRITE count
REPEAT/UNTIL
51
Start count = 0
REPEAT
count = 0 ADD 1 to count
WRITE count
UNTIL count <= 10
WRITE “The End”
add 1 to
count
Mainline
count = 0
write count
REPEAT
DO Process
count UNTIL count >= 10
<=10
WRITE “The End”

Write Process
“The End” ADD 1 to count
WRITE count
52 Exercise Program 1
 Write pseudocode and draw a flowchart that will
 Print first 10 odd numbers starting from the number provided by the user.
 If it is even number then start from the next odd number if it is odd number
then start with the current number
 E.g. the user input 10 then the out put will be
 11 13 15 17 19 21 23 25 27 29

 If the user input 5 then the output will be


 5 7 9 11 13 15 17 19 21 23

 Hint: Use number mod 2 = 0 to determine the even number


53 Pseudocode for printing 10 of numbers
1. START
2. declare count, num
3. input num
4. if num mod 2 = 0 then
4.1 num = num + 1
5. end if
6. count = 0
7. while count < 10
7.1 print num
7.2 num = num + 2
7.3 count = count +1
8. end while
9. END
Flow chart for printing 10 odd numbers
54
START 1

Declare
count, num count = 0

Input num
count False
num < 10
False
mod 2
True
=0
True Print num
num = num +1
num = num + 2
count = count +1

1
END
55 Exercise Program 2
 Write a pseudocode and draw a flowchart to
 Read an employee number (EMPNO), employee name (NAME), overtime
hours worked (OVERTIME), hours absent (ABSENT) and
 Determine the bonus payment (PAYMENT) for 10 employees one by one

Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours $50


>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10
56 Exercise Program 3
 Write a pseudocode and draw a flowchart to
 Read an employee number (EMPNO), employee name (NAME), overtime
hours worked (OVERTIME), hours absent (ABSENT) and
 Determine the bonus payment (PAYMENT)
 The program will keep on taking input and calculating until the user enters
a –ve EMPNO

Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours $50


>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10
57 Exercise Program 4: Average
 Write a pseudocode and draw a flowchart
 that will get 10 numbers from the user and print their average.
 Use only one number as input i.e. n1

 Hint: Use loop to get input in n1 and add it to Sum.


58 Exercise Program 5: Square/cube

 Write a pseudocode and draw a flowchart for a program that will


 Get one number from user
 Print its square if it is an even number
 Print its cube if it is and odd number
 The loop will end if the provided number is a –ve number (Do not print
square or cube of this –ve number)
59

Thank you

You might also like