Uploaded by sing bling

CP Lab Manual -2-min

advertisement
Practical 02
Problem analysis and program design for conditional and repetitive
control flow
Objectives
 Analyzing and designing programs for conditional logic.
 Analyzing and designing programs for repetitive logic.
Tools
 Edraw Max (Version: 6.8 or 7.2)
 Microsoft Word (Version: 2007, 2010 or 2013)
Keywords: Conditional, repetitive, iterative.
2.1
Duration: 03 hours
Introduction
2.1.1
Conditional control flow
Conditional control flow also referred to as conditional logic or selection logic is one of the order in
which the program instructions are executed. Its execution order differs from the sequential logic. It
executes the instructions on the basis of one or more conditions. If the conditions are satisfied it will
execute the instructions else the instructions will not be executed (skipped). In conditional logic:



2.1.2
Statements are executed on the basis of conditions.
If conditions are satisfied the statements are executed.
If conditions are not satisfied the statements are skipped.
Types of conditional control flow
In programming we normally have four types of conditional/selection control flow:
2.1.2.1 One-Way Selection
In one-way selection there is one condition and only one possible choice available either we choose
it or not. If the condition is satisfied we choose it, and do not choose it, if the condition is not satisfied.
Computer Programming by Ali Asghar Manjotho
17
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Like, the boss checks the experience of an employee and adds the bonus, if the experience is more
than 2 years. Similarly, shopkeeper gives you a discount of 10% if you make the purchase of more
than Rs. 5000. Both of these examples involve one-way selection.
In one-way selection:


The statements are executed if the condition is satisfied (true).
Does nothing when condition is not satisfied (false).
Following is the flow of execution of one-way selection:
Examples
Problem Statement 1: If the number is positive then display it.
Problem Statement 2: If the number is multiple of 5 then add 1 to it and display the resultant
number.
18
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
2.1.2.2 Two-Way Selection
In two-way selection there is one condition and two possible choices available either we choose first
one or the second one. If the condition is satisfied we choose first choice and the second choice if the
condition is not satisfied.
Like, the teacher checks the roll number of a student, if it is even he/she is from section 2 otherwise
he/she is from section 1. It is an example involving two-way selection.
In two-way selection:


If the condition is satisfied (true), the 1st set of statements is executed.
If the condition is not satisfied (false), the 2nd set of statements is executed.
Following is the flow of execution of two-way selection:
Examples
Problem Statement 1: If the number is even, display “Number is even” else display “Number is odd”.
Problem Statement 2: If the number is even, make it odd else double it and finally display the
number.
Computer Programming by Ali Asghar Manjotho
19
Practical 02: Problem analysis and program design for conditional and repetitive control flow
2.1.2.3 Multi-Way Selection
Multi-way selection is series of two-way selections. In multi-way selection there are multiple
conditions and multiple possible choices available, we choose any one of them. Either all the
conditions will not be satisfied or at maximum any one of the condition can be satisfied. But at the
end we can only choose one of the choice.
Like, a person checks the age of the three persons to determine who is elder amongst them? If the
age of first person, is greater than second and third, then person 1 is elder; otherwise if the age of
second person, is greater than first and third, then person 2 is elder; otherwise person 3 is elder. It is
an example involving multi-way selection.
In multi-way selection:







If the 1st condition is satisfied (true), the 1st set of statements is executed.
If the 1st condition is not satisfied (false), then we check for 2nd condition.
If the 2nd condition is satisfied (true), the 2nd set of statements is executed.
If the 2nd condition is not satisfied (false), then we check for 3rd condition.
And the process continues up to nth condition.
If the nth condition is satisfied (true), the nth set of statements is executed.
If the nth condition is not satisfied (false), then (n+1)th set of statements is executed.
Following is the flow of execution of multi-way (4-way) selection:
Example
Problem Statement: Compare three numbers and display which number is largest among them.
20
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
2.1.2.4 Choice-Way Selection
Choice-way selection is the simplest form of multi-way selection. In choice-way selection there is no
any condition to be checked. You are provided with multiple options and given a choice to select only
one of the option among them. Every option is associated with different statement set. When you
select a choice it is matched with all of the options, one of the option will be matched and
corresponding statement set will be executed.
Like, a teacher gives remarks to a student according to the marks he/she gets out of 5. He sets the
remarks criteria as:
Marks
0
1
2
3
4
5
Remarks
Very Bad
Not Satisfactory
Slightly Satisfactory
Satisfactory
Good
Excellent
It is an example involving choice-way selection.
In choice-way selection:






We make a choice.
If choice matches with 1st option, the 1st statement set is executed.
If choice matches with 2nd option, the 2nd statement set is executed.
If choice matches with 3rd option, the 3rd statement set is executed.
And the process continues up to nth option.
If choice matches with nth option, the nth statement set is executed.
Following is the flow of execution of choice-way (4-way) selection:
Computer Programming by Ali Asghar Manjotho
21
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Example
Problem Statement: Give remarks to the student according to the marks he/she gets out of 5 in the
sessional test.
2.1.3
Repetitive control flow
Repetitive control flow also referred to as iterative logic or loop, is one of the order in which the
program instructions are executed. It executes the instructions repetitively multiple number of times.
Repetitive logic allows us to execute a statement or set of statements multiple number of times just
by writing down them only one time.
2.1.4
Types of repetitive control flow
In programming we normally have two types of repetitive/iterative control flow:
22
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
2.1.4.1 Counter-controlled repetition
The case, when we know exactly how many times we have to repeat the statements execution, we
will use counter-controlled repetition logic. Suppose the set of statements need to be executed is N
number of times. We will first set the counter to 1, and every time we check the counter (counter <=
N) and increment it (counter = counter + 1).
Following is the flow of execution of counter-controlled repetition:
Example
Problem Statement: Display the integer numbers from 1 to 10.
Computer Programming by Ali Asghar Manjotho
23
Practical 02: Problem analysis and program design for conditional and repetitive control flow
2.1.4.2 Sentinel-controlled repetition
The case, when we do not know exactly how many times we have to repeat the statements execution,
we will use sentinel-controlled repetition logic. Suppose we have to create a program that
continuously reads lines from a text file and displays them until it reaches the end of the file. In this
case we do not know how many lines will be there in different text files. It is an example of sentinelcontrolled repetition.
We do not always know how times we need to repeat, but we may know that the last entry is a special
value, called a sentinel. The sentinel-controlled repetition is also known as condition-controlled
repetition.
Following is the flow of execution of sentinel-controlled repetition:
Example
Problem Statement: The user continuously enters integer numbers, the program stops when the
user enters any number other than the numbers between 100 and 200. Finally it displays the sum of
all the numbers entered by the user.
24
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
2.2
Procedure
2.2.1
Problem example
Consider the following number generation problem example. We are going to draw its IPO chart,
create algorithm and flow chart.
Problem statement: Write a program that receives the integer number N from the user and displays
all the multiples of 7 from 1 to N.
2.2.2
Creating IPO chart and algorithm
Step 01: Create a new blank document in MS Word.
Step 02: Insert a new table of 2 rows 3 columns by going in to Insert Tab>Table
Step 03: Type in basic text in the table.
Input
Processing
Processing Items:
Output
Algorithm:
Step 04: Identify all the outputs, to the program, from the problem statement. There are N output
problem:

All multiples of 7 in between 1 to N
Step 05: Identify all the inputs, to the program, from the problem statement. There is only one input
to the program:
Computer Programming by Ali Asghar Manjotho
25
Practical 02: Problem analysis and program design for conditional and repetitive control flow

Number N.
Step 06: Identify all the processing items to be used in solving the problem. The processing items
involve any formula, equation, expression or sequence to be used. There is one condition to check for
multiple of 7:

n MOD 7 = 0
Step 07: Write down step by step procedure to solve the problem.








Step 01: Start
Step 02: Input number N from the user
Step 03: Set i = 1
Step 04: Repeat Step 05 to Step 07 while i <= N
Step 05: If (i MOD 7 = 0) then GOTO Step 06 else GOTO Step 07
Step 06: Print i
Step 07: i = i + 1
Step 08: End
Step 08: Fill in all the information in IPO chart.
Input
•
Number N
Processing
Output
Processing Items:
n MOD 7 = 0
•
Multiples of 7 in between
1 to N
Algorithm:
Step 01: Start
Step 02: Input number N from the user
Step 03: Set i = 1
Step 04: Repeat Step 05 to Step 07 while i <= N
Step 05: If (i MOD 7 = 0) then GOTO Step 06 else
GOTO Step 07
Step 06: Print i
Step 07: i = i + 1
Step 08: End
2.2.3
Creating flow chart
Step 01: Open Edraw Max and create new basic flow chart by going in to Template
Categories>Flowchart>Basic Flowchart. Click the Create button at the right side.
26
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Step 02: Click and drag Start or Terminal symbol from toolbox in to drawing area.
Step 03: Double click the shape to add the text “Start”.
Step 04: Click and drag Data symbol from toolbox in to drawing area.
Step 05: Double click the shape to add the text “Input N”.
Computer Programming by Ali Asghar Manjotho
27
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Step 06: Click and drag Process symbol from toolbox in to drawing area.
Step 07: Double click the shape to add the text “Set i = 1”.
Step 08: Click and drag Decision symbol from toolbox in to drawing area.
Step 09: Double click the shape to add the text “i <= N”.
Step 10: Click and drag Decision symbol from toolbox in to drawing area.
Step 11: Double click the shape to add the text “i MOD 7 = 0”.
28
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Step 12: Click and drag Data symbol from toolbox in to drawing area.
Step 13: Double click the shape to add the text “Print i”.
Step 14: Click and drag Process symbol from toolbox in to drawing area.
Step 15: Double click the shape to add the text “i = i + 1”.
Step 16: Click and drag Start or Terminal symbol from toolbox in to drawing area.
Computer Programming by Ali Asghar Manjotho
29
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Step 17: Double click the shape to add the text “End”.
Step 18: Select Right-Angle Connector from the top ribbon bar.
Step 19: Point your cursor over the center of first start symbol. When red lines appear, click over it
(do not leave the click hold your mouse button down), drag your cursor to the center of second
symbol i.e. input box. When the red lines appear over it release the mouse button. Hence you
connected two symbols.
30
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Step 20: Connect rest of the symbols with arrows.
2.3
2.3.1
Problem examples
Even or odd number
Problem statement: Write a program that receives an integer number from the user and displays
whether it is an even number or an odd number.
IPO chart and algorithm
Computer Programming by Ali Asghar Manjotho
31
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Input
•
Integer number
Processing
Output
Processing Items:
If (number MOD 2) = 0 then it is even
If (number MOD 2) ≠ 0 then it is odd
•
Number is odd
or even
Algorithm:
Step 01: Start
Step 02: Input number from the user
Step 03: If (number MOD 2) = 0 then GOTO Step 04
else GOTO Step 05
Step 04: Print “Number is even” GOTO Step 06
Step 05: Print “Number is odd”
Step 06: End
Flow chart
2.3.2
Positive numbers
Problem statement: Write a program that receives an integer number from the user. If the number
is positive then display that number.
IPO chart and algorithm
32
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Input
•
Integer number
Processing
Output
Processing Items:
If (number > 0) then it is positive
•
Number if it is
positive
Algorithm:
Step 01: Start
Step 02: Input number from the user
Step 03: If (number > 0) then GOTO Step 04 else
GOTO Step 05
Step 04: Print number
Step 05: End
Flow chart
2.3.3
Largest number
Problem statement: Write a program that receives three unique integer numbers from the user and
displays the largest number.
IPO chart and algorithm
Computer Programming by Ali Asghar Manjotho
33
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Input
•
First
number
•
Second
number
•
Third
number
Processing
Output
Processing Items:
If (num1> num2 and num1 > num3) then num1 is largest
If (num2> num1 and num2 > num3) then num2 is largest
If (num3> num1 and num3 > num2) then num3 is largest
•
Largest
number
Algorithm:
Step 01: Start
Step 02: Input num1, num2 and num3 from the user
Step 03: If (num1> num2 and num1 > num3) then GOTO Step 05
Step 04: If (num2> num1 and num2 > num3) then GOTO Step 06 else
GOTO Step 07
Step 05: Print num1 GOTO Step 08
Step 06: Print num2 GOTO Step 08
Step 07: Print num3
Step 08: End
Flow chart
34
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
2.3.4
First ten integers
Problem statement: Write a program that generates and displays first 10 integer numbers.
IPO chart and algorithm
Input
Processing
Output
Processing Items:
1, 2, 3, . . . , 10
•
First 10 integer numbers
Algorithm:
Step 01: Start
Step 02: Set i = 1
Step 03: Repeat Step 04 to Step 05 while i <=10
Step 04: Print i
Step 05: Set i = i + 1
Step 06: End
Flow chart
2.3.5
Sum of first N integer numbers
Problem statement: Write a program that displays sum of first N integer numbers. Whereas the
number N is provided by the user.
IPO chart and algorithm
Computer Programming by Ali Asghar Manjotho
35
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Input
•
Number N
Processing
Output
Processing Items:
Sum = 1 + 2 + 3 + , . . . , + N
•
Sum of first N integers
Algorithm:
Step 01: Start
Step 02: Input N from the user
Step 03: Set i = 1, sum = 0
Step 04: Repeat Step 05 to Step 06 while i <=N
Step 05: Set sum = sum + i
Step 06: Set i = i + 1
Step 07: Print sum
Step 08: End
Flow chart
2.3.6
Factorial of a number
Problem statement: Write a program that receives a positive integer number from the user and
displays its factorial.
IPO chart and algorithm
36
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Input
•
Number N
Processing
Output
Processing Items:
N! = N * (N-1) * (N-2) * , . . . , * 1
•
Factorial of N
Algorithm:
Step 01: Start
Step 02: Input N from the user
Step 03: Set i = N , factorial = 1
Step 04: Repeat Step 05 to Step 06 while i >1
Step 05: Set factorial = factorial * i
Step 06: Set i = i - 1
Step 07: Print factorial
Step 08: End
Flow chart
2.3.7
Prime or composite number
Problem statement: Write a program that receives a positive integer number from the user and
displays whether it is a prime or composite number.
IPO chart and algorithm
Computer Programming by Ali Asghar Manjotho
37
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Input
•
Number N
Processing
Output
Processing Items:
Prime number = (N MOD n) ≠ 0, where n= 2 to (N-1)
Composite number = Otherwise
•
N is prime or
composite
Algorithm:
Step 01: Start
Step 02: Input N from the user
Step 03: Set i = 2 , isPrime = true
Step 04: Repeat Step 05 to Step 06 while i < N
Step 05: if (N MOD i) = 0 then isPrime = false GOTO Step 07
Step 06: Set i = i + 1
Step 07: if isPrime = true GOTO Step 08 else GOTO Step 09
Step 08: Print “Number is prime” GOTO Step 10
Step 09: Print “Number is composite”
Step 10: End
Flow chart
38
Computer Programming by Ali Asghar Manjotho
Practical 02: Problem analysis and program design for conditional and repetitive control flow
EXERCISE
For each of the following problem statement, create the IPO chart with algorithm and flow
chart.
Problem Statement 1
There are two circular grounds Ground-A and Ground-B. Ground-A is having diameter of 15 meters
and Ground-B is having diameter of 20 meters. Mohsin is running in Ground-A and Neetesh is running
in Ground-B. Write a computer program that asks the user to input the time taken, in seconds, to
complete one compete round of the ground by both the friends and displays who is running faster.
Problem Statement 2
Write a computer program that asks the user to enter three angles of a triangle. The program displays
whether the triangle is right-angle, acute-angle or obtuse-angle.
Problem Statement 3
Write a computer program that asks the user to enter date of birth and month of birth. The program
should display the zodiac star.
Problem Statement 4
Write a computer program that displays the sum of first 10 odd multiples of 3.
Problem Statement 5
Write a computer program that generates and displays the first N three digit odd numbers. Whereas
the number N is provided by the user.
Problem Statement 6
Write a computer program that displays the sum of last 5 four digit multiples of 5.
Problem Statement 7
Write a computer program that asks the user to input the starting number and ending number of the
range. The program should display the number of multiples of 5 in between that range.
Computer Programming by Ali Asghar Manjotho
39
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Problem Statement 8
Write a computer program that generates and displays the following series of numbers:
15, 30, 45, 60, 75, 90, 105, 120, 135, 150
Problem Statement 9
Write a computer program that generates and displays the following series of numbers:
1, 4, 9, 16, 25, 36, 49, 64, 81, 100
Problem Statement 10
Write a computer program that generates and displays the following series of numbers:
90, 72, 56, 42, 30, 20, 12, 6, 2, 0
40
Computer Programming by Ali Asghar Manjotho
Download