Download - Cs 1114 - lecture-7
Instructor : Muhammad Haris
All Rights Reserved to Department of Computer Science – GCU Lahore
Programming Fundamentals
Another Example
Find grade from given marks of a Student
Programming Fundamentals | Lecture-6 2
Marks Range Grade
90 or above A+
85 to 89 A
75 to 84 B
65 to 74 C
50 to 64 D
Below 50 F
Consider this Example
Find smallest of three numbers
Programming Fundamentals | Lecture-6 3
Programming Fundamentals | Lecture-6 4
START
READ n1, n2, n3
n1 < n2YesNo
n2 < n3 n1 < n3YesYesNo No
DISPLAY n1DISPLAY n3DISPLAY n3 DISPLAY n2
Programming Fundamentals | Lecture-6 5
START
READ n1, n2, n3
DISPLAY n1
STOP
n1 < n2 AND n1 < n3
YesNo
n2 < n1 ANDn2 < n3
Yes
DISPLAY n2
No
DISPLAY n3
Important Observations (1) If two rules are combined with AND in a
decision box, they can be separated like this, and vice versa
Programming Fundamentals | Lecture-6 6
n1 < n2Yes
n1 < n3
n1 < n2 AND n1 < n3
Important Observations (2) If two rules are combined with OR in a
decision box, they can be separated like this
Programming Fundamentals | Lecture-6 7
no==1 OR no==2
no == 1No
no == 2
Same Question Once Again When do we need to combine the rules
and when do we need to separate them?
When we have to perform the same action upon satisfaction or dissatisfaction of those rulesWe combine them
OtherwiseWe separate them
Programming Fundamentals | Lecture-6 8
Another Example For given obtained marks and
attendance percentage do the following:If attendance percentage of a student is
greater than equal to 85 and obtained marks are greater than 40○ Display he is “Passed”
If attendance percentage is greater than 85 but marks are less than 40○ Display he is “Failed”
If attendance percentage is less than 85○ Display he is “Not Eligible”
Programming Fundamentals | Lecture-6 9
Programming Fundamentals | Lecture-6 10"
START
READ marks, tm, ap
DISPLAY “Not Eligible”
STOP
ap >= 85YesNo
marks > 40Yes
DISPLAY “Failed”
No
DISPLAY “Passed”
Modes of Decision Making
If we carefully observe the nature of problems we have considered so far, we see there are two different modes / styles of decision makingLinearNon-Linear
Programming Fundamentals | Lecture-6 11
Linear Style
When a field is being tested for different values and a different action is to be taken for each value
ExamplesPerforming different operations on 2
numbers based on the value of second number○ Field: number2 Values: < 0, >0, 0
Displaying name against roll number○ Field: rollNumber Values: 1, 2, 3
Programming Fundamentals | Lecture-6 12
Linear Style
ExamplesDisplaying city name against city code
○ Field: city code Values: 042,049 etc.Calculating base pay depending upon the
pay type○ Field: pay type Values: 1, 2, 3
Finding season against month number○ Field: month number Values: 1, 2, ….. 12
Finding grade of a student○ Field: marks Values: >=90, 85 – 90, 75 – 84 ...
Programming Fundamentals | Lecture-6 13
Non-Linear Style
When a number of conditions need to be satisfied before an action can occur
ExamplesDeclaring a student as passed if his
attendance percentage and marks fulfill the requirements○ 1st condition: attendance percentage○ 2nd condition: marks○ Action: displaying as “passed”
Programming Fundamentals | Lecture-6 14
Try this Yourself
Find smallest among four numbers Find largest among four numbers
Programming Fundamentals | Lecture-6 15
Another Kind of Problem
Find sum of two numbersIf second number is equal to 0 add 5 to it
Programming Fundamentals | Lecture-6 16
Programming Fundamentals | Lecture-6 17
START
READ number1, number2
sum = number1 + number2
DISPLAY sum
STOP
number2==0YesNo
number2 = number2 + 5
Congratulations!!!
We are done with the decision making part
Assignment-1
Don’t forget to read the instructions