Share the knowledge

1.1 Logical structure “selection”

1.1.1 Selection Examples

Example Sel1

Problem :- The problem is a very simple one; finding the greatest of 2 numbers.

Solution :-      Data Required                :-       (i) 1st number        (ii) 2nd number

Processing involved       :-       comparing  the 2 numbers

Output Required            :-       the greatest one

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

Read 1st number

Read 2nd number

If 1st number > 2nd number Then

print 1st number

Else

print 2nd number

Endif


Pascal Program:-

program sel1_greatest;

var

num1,num2:integer;

begin

write (‘Please key in a number   ‘);

readln(num1);

write (‘Please key in another number   ‘);

readln(num2);

if num1 > num2 then

writeln (‘The greatest number is  ‘,num1)

else

writeln (‘The greatest number is  ‘,num2)

end.


Example Sel2

Problem :- The payroll program of example seq3  is modified as follows. It is now required to input an employee number and the number of hours worked. For the first 40 hr. the rate is Rs300 (normal rate) and for any hr. worked over that, the rate is Rs500 (overtime rate).

Solution :-

Data Required                :-       (i) employee number

(ii) no. of hours worked

Processing involved       :-       calculate the pay (see algo.)

Output Required            :-       pay

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

          Set normal rate to 100

Set overtime rate to 150

Read employee number

Read no. of hours worked

If hours <= 40 Then

pay = hours * normal rate

Else

pay = normal rate *40 + (hours – 40) * overtime rate

Endif

print the pay


Pascal Program:-

program sel2_overtime;

uses crt;

const

normal_rate = 100;

overtime_rate = 150;

var

hrs, empnum, pay : integer;

begin

write(‘Please enter employee number > ‘);

readln(empnum);

write(‘Please enter number of hours worked> ‘);

readln(hrs);

if hrs <= 40  then

pay := hrs * normal_rate

else

pay := normal_rate * 40 + (hrs-40) * overtime_rate;

writeln;

writeln(‘The pay of employee number  ‘,empnum, ‘ is ‘, pay)

end.


Example Sel3

Problem :- An electricity payment processing system is to be designed. The rates for each kilowatt hour (kWh) consumed are Rs1.65 and 2.65 for tariff 110 (residential) and tariff 310 (commercial) respectively. A fix meter rent of Rs2. is to be included in the bill. The problem is to find out the amount to be paid.

Solution :-

Data Required                :-       (i) customer number

(ii) no. of units consumed

(iii) the tariff

Processing involved       :-       calculate the amount due (see algo.)

Output Required            :-       the amount due

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

          Set meter rent to 2

Read customer number

Read no. of units consumed

Read tariff

If tariff = 110 Then

amount due = 1.65 * units consumed + meter rent

Else

amount due  = 2.65 * units consumed + meter rent

Endif

print the amount due


Pascal Program:-

program sel3_electricity;

uses crt;

const

meter_rent = 2;

var

custnum, units, tariff  : integer;

amount_due              : real;

begin

write(‘Please enter customer number > ‘);

readln(custnum);

write(‘Please enter number of units consumed> ‘);

readln(units);

write(‘Please enter the tariff > ‘);

readln(tariff);

if tariff = 110  then

amount_due := 1.65 * units + meter_rent

else

amount_due := 2.65 * units + meter_rent;

writeln(‘Amount due for customer  ‘,custnum, ‘ is ‘, amount_due)

end.


1.1.2 Selection Exercises

1. In the above example (sel3) what will happen if the tariff is neither 110 nor 310. Suggest how you could overcome this problem.

2. A single digit number is to be stored in the computer’s memory. A user must then be allowed to guess the number. As output either of two messages is required; one if the guess is correct and one if it is not. Write an appropriate algorithm and the corresponding pascal program to solve this problem.

3. Modify the algorithm of sequence exercise 2 (pg. 19) to display either of 2 messages viz. “passed” or “failed”. The pass mark is 40%.

4. Combine the 2 modules of the currency conversion program of sequence exercise 3 (pg. 19). Before entering any of the 2 modules, the user must be allowed to make a choice. Choice 1 for the 1st module and choice 2 for the 2nd one.

5. According to UCLES a candidate will be marked as absent if the project is not submitted, irrespective of the number of marks obtained in the theory exam. How would you modify the algorithm of example seq1 to take of this situation?


Honest review for Wealthy Affiliate

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *