CS118 –
Programming
Fundamentals
Lecture # 03
Tuesday, September 21, 2020
FALL 2020
FAST – NUCES, Faisalabad Campus
Saqib Hayat
Algorithms and Problem
2
Solving Techniques
CS118 - FALL 2020 September 21, 2020
3
Flow Charts
CS118 - FALL 2020 September 21, 2020
Flow Chart Symbols
4
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
CS118 - FALL 2020 September 21, 2020
The Flowchart
5
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
CS118 - FALL 2020 September 21, 2020
The Flowchart
6
A flowchart
Shows logic of an algorithm
Emphasizes individual steps and their interconnections
E.g. control flow from one action to another
CS118 - FALL 2020 September 21, 2020
if Selection Structure
7
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
CS118 - FALL 2020 September 21, 2020
if Selection Structure
8
Translation into Algorithm
If student’s grade is greater than or equal to 60
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
CS118 - FALL 2020 September 21, 2020
if Selection Structure
9
Flowchart of pseudocode statement
A decision can be
made on any
expression.
true zero - false
grade >= 50 print “Passed”
nonzero - true
Example:
false 3 - 4 is true
CS118 - FALL 2020 September 21, 2020
if/else Selection Structure
10
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 40
print “Passed”
else
print “Failed”
if ( grade >= 50 )
Print "Passed";
else
Print "Failed";
CS118 - FALL 2020 September 21, 2020
if/else Selection Structure
11
false true
grade >= 50
print print
“Failed” “Passed”
CS118 - FALL 2020 September 21, 2020
if/else Selection Structure
12
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”
CS118 - FALL 2020 September 21, 2020
if/else Selection Structure
13
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";
CS118 - FALL 2020 September 21, 2020
if/else Selection Structure
14
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
CS118 - FALL 2020 September 21, 2020
Sequence Flowchart
15 Start
Input
Read one
number n1
Read second
number n2
Processing
Sum n1 + n2
Output
Display
Sum
End
CS118 - FALL 2020 Sep 03, 2018
Example 1
16
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
CS118 - FALL 2020 Sep 03, 2018
Example 2
17
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)
CS118 - FALL 2020 Sep 03, 2018
Example 3
18
Write a pseudocode 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
CS118 - FALL 2020 Sep 03, 2018
Example 4
19
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
CS118 - FALL 2020 Sep 03, 2018
Example 5
20
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
CS118 - FALL 2020 Sep 03, 2018
Example 6
21
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
CS118 - FALL 2020 Sep 03, 2018
Example 7
22
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
CS118 - FALL 2020 Sep 03, 2018
Questions
23
CS118 - FALL 2020 September 21, 2020