Cs 1114 - lecture-7

19
Instructor : Muhammad Haris All Rights Reserved to Department of Computer Science – GCU Lahore Programming Fundamentals

description

 

Transcript of Cs 1114 - lecture-7

Page 1: Cs 1114 - lecture-7

Instructor : Muhammad Haris

All Rights Reserved to Department of Computer Science – GCU Lahore

Programming Fundamentals

Page 2: Cs 1114 - lecture-7

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

Page 3: Cs 1114 - lecture-7

Consider this Example

Find smallest of three numbers

Programming Fundamentals | Lecture-6 3

Page 4: Cs 1114 - lecture-7

Programming Fundamentals | Lecture-6 4

START

READ n1, n2, n3

n1 < n2YesNo

n2 < n3 n1 < n3YesYesNo No

DISPLAY n1DISPLAY n3DISPLAY n3 DISPLAY n2

Page 5: Cs 1114 - lecture-7

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

Page 6: Cs 1114 - lecture-7

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

Page 7: Cs 1114 - lecture-7

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

Page 8: Cs 1114 - lecture-7

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

Page 9: Cs 1114 - lecture-7

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

Page 10: Cs 1114 - lecture-7

Programming Fundamentals | Lecture-6 10"

START

READ marks, tm, ap

DISPLAY “Not Eligible”

STOP

ap >= 85YesNo

marks > 40Yes

DISPLAY “Failed”

No

DISPLAY “Passed”

Page 11: Cs 1114 - lecture-7

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

Page 12: Cs 1114 - lecture-7

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

Page 13: Cs 1114 - lecture-7

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

Page 14: Cs 1114 - lecture-7

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

Page 15: Cs 1114 - lecture-7

Try this Yourself

Find smallest among four numbers Find largest among four numbers

Programming Fundamentals | Lecture-6 15

Page 16: Cs 1114 - lecture-7

Another Kind of Problem

Find sum of two numbersIf second number is equal to 0 add 5 to it

Programming Fundamentals | Lecture-6 16

Page 17: Cs 1114 - lecture-7

Programming Fundamentals | Lecture-6 17

START

READ number1, number2

sum = number1 + number2

DISPLAY sum

STOP

number2==0YesNo

number2 = number2 + 5

Page 18: Cs 1114 - lecture-7

Congratulations!!!

We are done with the decision making part

Page 19: Cs 1114 - lecture-7

Assignment-1

Don’t forget to read the instructions