- WordPress.com
Download
Report
Transcript - WordPress.com
Programming Fundamentals
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
4/8/2016
Oval – Start and Stop
Parallelogram – Input and Output
Rectangle - Processing
Diamond - Decision box
2
Recap of Previous Lectures
Problems we looked at
Sum, Product, Difference of two numbers
Square or cube of a number
Solving an equation or a formula involving 3,
4 variables
○ Example
Some formulae from elementary physics or an
algebraic equation
4/8/2016
3
Recap of Previous Lectures
Decision making is required in many
real-world problems
Computer programs can be very useful in
this regard
To incorporate decision making in our
solutions we learnt the usage of
“Decision box”
Always in the form
whose answer is
“Yes” or “No”
No
4/8/2016
Decision
Rule
Yes
4
Recap of Previous Lectures
We always make comparisons in a
decision box or Decision Rules are
always in the form of comparisons
Examples
○ difference < 0
○ sum > 0
○ number2 != 0
○ marks >= 50
4/8/2016
5
Recap of Previous Lectures
To determine the usage of decision rule,
follow these steps
1. 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?
4/8/2016
6
Recap of Previous Lectures
Problems we looked at
Checking negativity of a number
Checking equality of two numbers
Multiplying or Dividing two numbers
conditionally
Checking status of a student
4/8/2016
7
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 marks
A student securing marks less than 50 is
considered “failed”
4/8/2016
8
How to Code Selection?
Represents
decision box
if (decision_rule) {
No
Decision
Rule
Yes
/* steps that lie under
the “Yes” part */
} else {
/* steps that lie under
the “No” part */
Represents
“Yes” Part
}
“No” Part is not always coded
4/8/2016
Represents
“No” Part
9
Scenario Type 1
When nothing is performed in the “No” part
of the decision box
There is no need to “code” the “No” part. Only
“Yes” part will be coded or programmed.
Few Examples
Absolute value of difference of two numbers
Multiply two numbers if their difference is
greater than 0
Divide a number by another if and only if the
second number is not equal to 0
4/8/2016
10
Example: Scenario-1
START
READ number1, number2
difference = number1 - number2
No
difference < 0
No
operation
Yes
difference = difference * -1
DISPLAY difference
STOP
4/8/2016
11
/* declare variables to save values */
int number1, number2;
int difference;
difference = number1 – number2;
if (difference < 0) {
/* multiply difference by -1 */
difference = difference * (-1);
}
// print value of difference
cout<<difference;
4/8/2016
12
Try this Yourself
Divide a number by another if and only if
the second number is not equal to 0
4/8/2016
13
Scenario Type 2
When something is performed in the “No”
part of the decision box
Both “No” and “Yes” part will be coded or
programmed.
Few Examples
Find smaller of two numbers
Find whether a number is negative or not
Divide two numbers if their difference is greater
than 10, otherwise multiply them
4/8/2016
14
Example: Scenario-2
START
READ number1, number2
No
number1 < number2
Yes
smaller = number2
smaller = number1
DISPLAY smaller
STOP
4/8/2016
15
/* declare variables to save values */
int number1, number2
int smaller;
/* use selection control structure to
check which number is smaller */
if (number1 < number2) {
smaller = number1;
} else {
smaller = number2;
}
// print smaller
cout<<smaller;
4/8/2016
16
Decision Rules
So far
1 decision rule in a decision box
Examples
○ difference < 0
○ number2 != 0
○ marks < 50
difference < 0
4/8/2016
number2 != 0
marks< 50
17
Consider This Example
Divide a number by another if the
second number is greater than “0” and
less than “10”
OR
Divide a number by another if the
second number is between “0” and “10”
(also known as range check)
4/8/2016
18
More than 1 Decision Rule
START
READ num1, num2
No
num2 > 0 AND
num2 < 10
Yes
answer = num1 / num2
DISPLAY answer
STOP
4/8/2016
19
Another Example
Add two numbers if either of them is “0”
4/8/2016
20
Another Example
Determine status of a Students from
marks of two of his subjects
If marks for both the subjects are greater
than 40 he’s considered passed
4/8/2016
21
Another Example
Determine status of a Students from
marks of two of his subjects
If marks for any of the subjects is greater
than 40 he’s considered passed
4/8/2016
22
Consider this Example
Determine status of a Student from
marks of two of his subjects and his
GPA
If either the marks for both of the subjects
are greater than 40 or GPA is greater than 2,
he’s considered passed
4/8/2016
23
START
READ m1, m2, GPA
No
(m1 > 40 AND m2 > 40)
OR GPA > 2
DISPLAY “Failed”
Yes
DISPLAY “Passed”
STOP
4/8/2016
24
Another Example
Determine whether a student has got
Grade A from his total marks and his
GPA
If marks are greater than 80 and less than
90 OR GPA is greater than 3.7 and less 3.8,
he will be graded as “A”
4/8/2016
25
START
READ tm, GPA
No
(tm > 80 AND tm < 90)
OR
(GPA > 3.7 AND GPA < 3.8)
DISPLAY “Other than A”
Yes
DISPLAY “A”
STOP
4/8/2016
26
Try this Yourself
Determine whether a car is of mid-size
type or not from its price (in millions) and
engine capacity (in cc)
If price is between 1 million and 2 million or if
engine capacity is between 1000 and 1500,
a car is considered to be a mid-size car
4/8/2016
27
Operator
Opposite
>
<=
<
>=
==
!=
!=
==
<=
>
>=
<
Decision Box
Yes
No
>
>
<=
<
<
>=
==
==
!=
!=
!=
==
<=
<=
>
>=
>=
<
4/8/2016
28
Decision leading to Decisions
In real-world problems, it frequently
happens that result of a decision leads
to further decision making
This way computer programs prove to
be much more useful
4/8/2016
29
Consider this Example
For two numbers, do the following
If second number is greater than 0
○ Divide them
If second number is less than 0
○ Multiply them
If second number is equal to 0
○ Add them
4/8/2016
30
START
READ num1, num2
<=
No
==
No
ans = num1 +num2
num2 < 0
>
Yes
num2 > 0
<
Yes
ans = num1 / num2
ans = num1 * num2
DISPLAY ans
STOP
4/8/2016
31
Another Example
For given roll number of a student,
display his name
Assume that there are only three students
(first three roll numbers of your section)
4/8/2016
32
START
READ rollNo
!=
No
rollNo == 2
==
Yes
DISPLAY “Ali”
No
No
DISPLAY “No
Such Student”
rollNo == <
4
Yes
DISPLAY
“Rehman”
Yes
rollNo == 8
DISPLAY
“Hassan”
STOP
4/8/2016
33
Try this Yourself
For given day number of the week,
display the corresponding day
4/8/2016
Day Number
Week Day
1
Monday
2
Tuesday
3
Wednesday
4
Thursday
34
Another Example
For given obtained marks, total marks,
attendance percentage, do the following:
If attendance percentage of a student is
greater than 85 and obtained marks are
greater than 40
○ Calculate the percentage of his marks
If marks are less than 40
○ Display that he is “Failed”
If attendance percentage is less than 85
○ Display that he is “Not Eligible”
4/8/2016
35
Try it yourself
Display the name of a city against given
city code
Design your solution for 4-5 cities only
Calculate base pay from given annual
salary and pay type
Base pay = salary / Dividing Factor
4/8/2016
Pay type
Description
Dividing Factor
1
Weekly
52
2
Bi-monthly
24
3
Monthly
12
36
Yes
No
AND
All are True
Any of them or all of
them are not True
OR
Any of them or all of
them are True
All are not True
Programming Fundamentals | Lecture-6
37
Consider this Example
For given marks of two subjects of a
student, do the following:
If marks for either of the subjects are greater
than or equal to 40
○ Display that he is “Passed”
If marks for subject 1 are >= 40
○ Display that he is “Passed due to subject 1”
If marks for subject 2 are >= 40
○ Display that he is “Passed due to subject 2”
If marks for both the subjects are >= 40
○ Display that he is “Passed due to both subjects”
Programming Fundamentals | Lecture-6
38
START
READ m1, m2
No
m1 >= 40 OR
m2 >= 40
Yes
DISPLAY
“Failed”
No
No
Yes
m1>= 40 AND
m2 >= 40
Yes
DISPLAY “Passed due
to both subjects”
m1>= 40
DISPLAY “Passed due
to subject 2”
DISPLAY “Passed due
to subject 1”
Programming Fundamentals | Lecture-6
39
Consider this Example
For given marks of two subjects of a
student, do the following:
If marks for both the subjects are greater than
or equal to 40
○ Display that he is “Passed”
If marks for subject 1 are less than 40
○ Display that he is “Failed due to subject 1”
If marks for subject 2 are less than 40
○ Display that he is “Failed due to subject 2”
If marks for both the subjects are less than 40
○ Display that he is “Failed due to both subjects”
Programming Fundamentals | Lecture-6
40
START
READ m1, m2
No
No
Yes
Yes
DISPLAY
“Passed”
DISPLAY “Failed due
to both subjects”
No
m1< 40
DISPLAY “Failed
due to subject 2”
m1< 40 AND
m2 < 40
m1 >= 40 AND
m2 >= 40
Yes
DISPLAY “Failed due
to subject 1”
Programming Fundamentals | Lecture-6
41
An Important Point
Compare the two Examples
Problem-1: Find “Winter” season from given
month number
○ Only one scenario
Problem-2: Find “Day” from given day no
○ More than 1 scenarios
Programming Fundamentals | Lecture-6
42
Try it Yourself
Find the season from given month
number
Month Number
Season
1
Winter
2
Winter
3
Spring
4
Spring
5
Spring
6
Summer
7
Summer
8
Summer
9
Fall
10
Fall
11
Fall
12
Winter
Programming Fundamentals | Lecture-6
43
Another Example
Find grade from given marks of a
Student
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
Programming Fundamentals | Lecture-6
44
Consider this Example
Find smallest of three numbers
Programming Fundamentals | Lecture-6
45
START
READ n1, n2, n3
No
Yes
n1 < n2
Yes
No
No
n2 < n3
DISPLAY n3
Yes
n1 < n3
DISPLAY n2
DISPLAY n3
Programming Fundamentals | Lecture-6
DISPLAY n1
46
START
READ n1, n2, n3
No
No
n2 < n1 AND
n2 < n3
DISPLAY n3
n1 < n2 AND
n1 < n3
Yes
DISPLAY n1
Yes
DISPLAY n2
STOP
Programming Fundamentals | Lecture-6
47
Important Observations (1)
If two rules are combined with AND in a
decision box, they can be separated like
this, and vice versa
n1 < n2 AND
n1 < n3
Yes
n1 < n2
n1 < n3
Programming Fundamentals | Lecture-6
48
Important Observations (2)
If two rules are combined with OR in a
decision box, they can be separated like
this
No
no==1 OR
no==2
no == 1
no == 2
Programming Fundamentals | Lecture-6
49
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 rules
We combine them
Otherwise
We separate them
Programming Fundamentals | Lecture-6
50
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
51
START
READ marks, tm, ap
No
Yes
ap >= 85
No
Yes
marks > 40
DISPLAY “Not Eligible”
DISPLAY “Failed”
DISPLAY “Passed”
STOP
Programming Fundamentals | Lecture-6
52"
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 making
Linear
Non-Linear
Programming Fundamentals | Lecture-6
53
Linear Style
When a field is being tested for different
values and a different action is to be
taken for each value
Examples
Performing 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
54
Linear Style
Examples
Displaying 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
55
Non-Linear Style
When a number of conditions need to be
satisfied before an action can occur
Examples
Declaring 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
56
Try this Yourself
Find smallest among four numbers
Find largest among four numbers
Programming Fundamentals | Lecture-6
57
Another Kind of Problem
Find sum of two numbers
If second number is equal to 0 add 5 to it
Programming Fundamentals | Lecture-6
58
START
READ number1, number2
No
Yes
number2==0
number2 = number2 + 5
sum = number1 + number2
DISPLAY sum
STOP
Programming Fundamentals | Lecture-6
59
Congratulations!!!
We are done with the decision making
part