cs 1114 - lecture-4

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

Upload: zeeshan-sabir

Post on 01-Nov-2014

300 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Cs 1114 - lecture-4

Instructor : Muhammad Haris

All Rights Reserved to Department of Computer Science – GCU Lahore

Programming Fundamentals

Page 2: Cs 1114 - lecture-4

Recap of Previous Lectures To solve a problem using computers, we

need to do the following:1. Define the problem (in terms of inputs,

outputs and processing)

2. Design a solution for it (using a graphical approach)

○ Symbols used Oval – Start and Stop Parallelogram – Input and Output Rectangle - Processing Diamond - Decision box

Programming Fundamentals | Lecture-4 2

Page 3: Cs 1114 - lecture-4

Recap of Previous Lectures Problems we looked at

Sum, Product, Difference of two numbersSquare or cube of a numberSolving an equation or a formula involving 3,

4 variables○ Example

Some formulae from elementary physics or an algebraic equation

Programming Fundamentals | Lecture-4 3

Page 4: Cs 1114 - lecture-4

Recap of Previous Lectures Decision making is required in many

real-world problemsComputer programs can be very useful in

this regard

To incorporate decision making in our solutions we learnt the usage of “Decision box”

Programming Fundamentals | Lecture-4 4

Decision Rule

YesNo

Always in the form whose answer is

“Yes” or “No”

Page 5: Cs 1114 - lecture-4

Recap of Previous Lectures We always make comparisons in a

decision box or Decision Rules are always in the form of comparisonsExamples

○ difference < 0○ sum > 0○ number2 != 0○ marks >= 50

Programming Fundamentals | Lecture-4 5

Page 6: Cs 1114 - lecture-4

Recap of Previous Lectures To determine the usage of decision rule,

follow these steps1. Define inputs and outputs

2. Is decision-making required in this solution?

3. What are the basis of decision?○ the thing which is required to make a

decision will appear in the decision box

4. What kind of comparison is required in the decision box?

Programming Fundamentals | Lecture-4 6

Page 7: Cs 1114 - lecture-4

Recap of Previous Lectures Problems we looked at

Checking negativity of a numberChecking equality of two numbersMultiplying or Dividing two numbers

conditionallyChecking status of a student

Programming Fundamentals | Lecture-4 7

Page 8: Cs 1114 - lecture-4

Tasks (from previous lecture)

Find whether the sum of two numbers is greater than 50

Find whether the sum of two numbers is greater than the third number?

Divide a number by another if only if the second number is not equal to “0”

Determine whether a student is “passed” or “failed” from his marksA student securing marks less than 50 is

considered “failed”

Programming Fundamentals | Lecture-4 8

Page 9: Cs 1114 - lecture-4

Decision Rules

So far1 decision rule in a decision boxExamples

○ difference < 0○ number2 != 0○ marks < 50

Programming Fundamentals | Lecture-4 9

difference < 0 number2 != 0 marks< 50

Page 10: Cs 1114 - lecture-4

Consider This Example

Divide a number by another if the second number is greater than “0” and less than “10”

Divide a number by another if the second number is between “0” and “10” (also known as range check)

Programming Fundamentals | Lecture-4 10

OR

Page 11: Cs 1114 - lecture-4

Programming Fundamentals | Lecture-4 11

START

READ num1, num2

DISPLAY answer

STOP

answer = num1 / num2

num2 > 0YesNo

Page 12: Cs 1114 - lecture-4

More than 1 Decision Rule

Programming Fundamentals | Lecture-4 12

START

READ num1, num2

DISPLAY answer

STOP

answer = num1 / num2

num2 > 0 ANDnum2 < 10

YesNo

Page 13: Cs 1114 - lecture-4

Another Example

Add two numbers if either of them is “0”

Programming Fundamentals | Lecture-4 13

Page 14: Cs 1114 - lecture-4

Another Example

Determine status of a Students from marks of two of his subjectsIf marks for both the subjects are greater

than 40 he’s considered passed

Programming Fundamentals | Lecture-4 14

Page 15: Cs 1114 - lecture-4

Another Example

Determine status of a Students from marks of two of his subjectsIf marks for any of the subjects is greater

than 40 he’s considered passed

Programming Fundamentals | Lecture-4 15

Page 16: Cs 1114 - lecture-4

Programming Fundamentals | Lecture-4 16

BE PREPAREDFOR

ON-THE-SPOT TESTIN

NEXT LECTURE