fundamental programming lect 3

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

Upload: namrah-erum

Post on 16-Aug-2015

96 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Fundamental Programming Lect 3

Instructor : Muhammad Haris

All Rights Reserved to Department of Computer Science – GCU Lahore

Programming Fundamentals

Page 2: Fundamental Programming Lect 3

Tasks (from previous lecture)

Calculate area of a rectanglearea = base height

Find Cube of a Number Calculate Marks Percentage

(marks obtained / total marks) 100

Calculate Sales Taxamount (tax percent / 100)

Find “no. of minutes” and “no. of seconds” for given “no of years”

Programming Fundamentals | Lecture-3 2

Page 3: Fundamental Programming Lect 3

Power of Computer Programs Once given the input

They perform the instructions (or no. of steps as instructed by the programmer) themselves and give the desired output○ No matter how complex the problem is and

how long they have to work on it

Programming Fundamentals | Lecture-3 3

Now We Will Further Explore Their Power

Page 4: Fundamental Programming Lect 3

Consider this Problem

Find absolute value after subtracting one number from anotherDifference = number1 – number2

Programming Fundamentals | Lecture-3 4

Page 5: Fundamental Programming Lect 3

Difference of Two numbers

Programming Fundamentals | Lecture-3 5

START

READ number1, number2

difference = number1 - number2

DISPLAY difference

STOP

Will result in a negative value if number1 is

smaller than number2

Page 6: Fundamental Programming Lect 3

Is This Solution Correct?

Programming Fundamentals | Lecture-3 6

START

READ number1, number2

difference = number1 - number2

DISPLAY difference

STOP

difference = difference * -1

Page 7: Fundamental Programming Lect 3

Analyze the Problem

There exists two conditionsCondition-1

○ Difference is positiveCondition-2

○ Difference is negative

Programming Fundamentals | Lecture-3 7

Page 8: Fundamental Programming Lect 3

What is to be Done? There exists two conditions

Condition-1○ Difference is positive

Do not do any conversion, because value is already positive

Condition-2○ Difference is negative

Convert it into a positive value by multiplying it by “-1”

Programming Fundamentals | Lecture-3 8

Page 9: Fundamental Programming Lect 3

Should we design two different solutions?

9Programming Fundamentals | Lecture-1

Page 10: Fundamental Programming Lect 3

What is Required?

Need to design a solution in such a way which can decide for itself whether to multiply the difference by “-1” or not (by checking the value of difference)

The graphical shapes which we have learned so far are insufficient to provide this kind of “decision making”

Programming Fundamentals | Lecture-3 10

Page 11: Fundamental Programming Lect 3

Decision Box

Programming Fundamentals | Lecture-3 11

Decision Rule

YesNo

Always in the form whose answer is

“Yes” or “No”

Page 12: Fundamental Programming Lect 3

Programming Fundamentals | Lecture-3 12

START

READ number1, number2

difference = number1 - number2

DISPLAY difference

STOP

difference = difference * -1

difference < 0YesNo

Page 13: Fundamental Programming Lect 3

Power of Computers

Decision MakingProgrammers can instruct computers to

make decisions while solving a problem○ As the complexity of problems increases,

decision making process become more extensive

This way computers prove very useful by performing laborious tasks of decision making

Programming Fundamentals | Lecture-3 13

Page 14: Fundamental Programming Lect 3

Another Example

Find smaller of two numbers Input

number1number2

Outputsmaller

ProcessingCompare the numbers with each other and

decide which one is smaller

Programming Fundamentals | Lecture-3 14

Page 15: Fundamental Programming Lect 3

Programming Fundamentals | Lecture-3 15

START

READ number1, number2

DISPLAY smaller

STOP

smaller = number1

number1 < number2YesNo

smaller = number2

Page 16: Fundamental Programming Lect 3

Alternative

Programming Fundamentals | Lecture-3 16

START

READ number1, number2

STOP

DISPLAY number1

number1 < number2YesNo

DISPLAY number2

Page 17: Fundamental Programming Lect 3

Comparison Operators

< (less than) > (greater than) <= (less than equal to) >= (greater than equal to) == (equal to) != (not equal to)

Programming Fundamentals | Lecture-3 17

Page 18: Fundamental Programming Lect 3

Output a Message

Programming Fundamentals | Lecture-3 18

DISPLAY “This is a message”

Page 19: Fundamental Programming Lect 3

Try it Yourself

Find whether a number is negative or not?

Find whether two numbers are equal or not?

Multiply two numbers if their difference is greater than 0

Programming Fundamentals | Lecture-3 19

Page 20: Fundamental Programming Lect 3

Some Keywords

Look for these keywords in problem statement in order to determine the usage of decision boxIfIf and only ifWhether

Programming Fundamentals | Lecture-3 20

Page 21: Fundamental Programming Lect 3

Tasks (to be done by next 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-3 21

Page 22: Fundamental Programming Lect 3

Programming Fundamentals | Lecture-2 22

BE PREPAREDFOR QUIZ

IN NEXT LECTURE