Share the knowledge

### 1.3.1     Nested IF Selection Examples

Example NSel1

Problem :- To modify the example seq1 as explained above in selection exercise 5.

Solution :-

Algorithm :- (only pseudocode shown; as a supplementary exercise do the flowchart)

If mark for project = 0 Then

output result as “absent”

Else

total marks = project + theory

output the total marks

If total marks < 40 Then

output result as “failed”

Else

output result as “passed”

Endif

Endif

Example NSel2

Problem :- In a medical center a computer system is used to calculate the body weight index (NDX) which gives an indication of whether a person is under weight, over weight, obese or of normal weight.

If  NDX < 20 Then the person is under weight

If  20 < NDX < 25 Then the person is of normal weight

If  25 < NDX < 30 Then the person is over weight

If  NDX > 30 Then the person is obese (very fat)

Solution :-

Data Required                :-       (i) Weight, W, in kg of the person

(ii) Height, H, in meters of the person

Processing involved       :-       NDX = W / (H * H)

Output Required            :-       an appropriate message

Algorithm :-

The algorithm is show below. Please pay special attention to the indentation.

Test the algorithm with the following test data :-

To simplify the test, values of NDX given instead of weight and height.

Normal data                   :- 18, 23, 27, 34

Extreme data        :- 19,20,21 —  24,25,26 — 29,30,31

Abnormal data     :- -23

Algorithm :- (only pseudocode shown; as a supplementary exercise do the flowchart)

Read weight (W) of the person

Read height (H) of the person

NDX = W / (H * H)

If NDX > 30 Then

output message “the person is obese”

Else

If NDX > 25 Then

output message “the person is over weight”

Else

If NDX >20 Then

output message “the person is of normal weight”

Else

output message “the person is under weight”

Endif

Endif

Endif

#### Pascal Program:-

program nsel2_ndx;

var

weight,height,ndx:real;

begin

write (‘Please enter weight of the person   ‘);

write (‘Please enter height of the person   ‘);

ndx:= weight / (height * height);

if ndx > 30 then

writeln(‘The person is obese’)

else

if ndx > 25 then

writeln(‘The person is over weight’)

else

if ndx > 20 then

writeln(‘The person is of normal weight’)

else

writeln (‘The person is under weight ‘)

end.

### 1.3.2     Multi-Case  Selection Examples

Example CSel1

Problem :- In a book shop there are 3 types of customers depending on certain criteria as follows :-

Customer type                Criteria                 Discount

1                           Students                  10%

2                           Teachers                    5%

The 3rd type of customer is neither a student nor a teacher.

The problem is to calculated the cost for any purchase.

Solution :-

Data Required                :-       (i) amount purchased

(ii) customer type

Processing involved       :-       calculate net amount to be paid

Output Required            :-       net amount due

Algorithm :- (only pseudocode shown; as a supplementary exercise do the flowchart)

case customer type of

1 : net amount due = amount purchased * 0.90

2 : net amount due = amount purchased * 0.95

otherwise net amount due = amount purchased

endcase

Note :- Only the process part of the algorithm is shown. Write down the complete algorithm showing the input, process and output.

#### Pascal Program:-

program csel1_discount;

var

customer_type                    : integer;

amount_purchased, net_amount_due : real;

begin

writeln(‘               Customer Types are:’);

writeln;

writeln(‘          1.  Students : 10% discount’);

writeln(‘          2.  Teachers : 05% discount’);

writeln;

write(‘Please enter amount purchased > ‘);

case customer_type of

1 : net_amount_due := amount_purchased * 0.90;

2 : net_amount_due := amount_purchased * 0.95;

else net_amount_due:= amount_purchased

end;

writeln(‘Net amount due is ‘, net_amount_due:7:2)

end.

1. For the book shop problem above (example CSel1) rewrite the algorithm using nested if selection instead of the multi-case selection.
2. Write an algorithm for the following problem. You are required to calculate and display an employee’s wages given the following rule :-Basic    : hours * rate
Double : hours * rate * 2
Treble  : hours * rate * 3

You will have to input an employee number, number of hours worked and a payment type. Payment type is B, D, or T indicating basic, double, or treble time.

3. Using nested if selection write an algorithm to award grades in an examination as follows :-
Grade 1   :   80 – 100 %
Grade 2   :   60 – 79 %
Grade 3   :   40 – 59 %
Grade 0   :   0 – 39 %
4. Cambridge SC exams of Jun.95 No. 13 & Nov.95 No 8, 13
5. For the currency conversion program, add another module to convert Mauritian rupees to American dollars.