fundamental programming lect 3
TRANSCRIPT
Instructor : Muhammad Haris
All Rights Reserved to Department of Computer Science – GCU Lahore
Programming Fundamentals
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
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
Consider this Problem
Find absolute value after subtracting one number from anotherDifference = number1 – number2
Programming Fundamentals | Lecture-3 4
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
Is This Solution Correct?
Programming Fundamentals | Lecture-3 6
START
READ number1, number2
difference = number1 - number2
DISPLAY difference
STOP
difference = difference * -1
Analyze the Problem
There exists two conditionsCondition-1
○ Difference is positiveCondition-2
○ Difference is negative
Programming Fundamentals | Lecture-3 7
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
Should we design two different solutions?
9Programming Fundamentals | Lecture-1
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
Decision Box
Programming Fundamentals | Lecture-3 11
Decision Rule
YesNo
Always in the form whose answer is
“Yes” or “No”
Programming Fundamentals | Lecture-3 12
START
READ number1, number2
difference = number1 - number2
DISPLAY difference
STOP
difference = difference * -1
difference < 0YesNo
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
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
Programming Fundamentals | Lecture-3 15
START
READ number1, number2
DISPLAY smaller
STOP
smaller = number1
number1 < number2YesNo
smaller = number2
Alternative
Programming Fundamentals | Lecture-3 16
START
READ number1, number2
STOP
DISPLAY number1
number1 < number2YesNo
DISPLAY number2
Comparison Operators
< (less than) > (greater than) <= (less than equal to) >= (greater than equal to) == (equal to) != (not equal to)
Programming Fundamentals | Lecture-3 17
Output a Message
Programming Fundamentals | Lecture-3 18
DISPLAY “This is a message”
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
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
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
Programming Fundamentals | Lecture-2 22
BE PREPAREDFOR QUIZ
IN NEXT LECTURE