## 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?