0% found this document useful (0 votes)
9 views

Pls 3 Solutions

The document discusses programming logic and provides examples of simple selection structures, complex selection structures, and iteration structures using pseudocode. Multiple solutions are provided for various programming challenges around determining qualifications for overtime pay, fitting tiles in a box, selecting the appropriate needle thickness, and calculating insurance premiums based on penalty points. Additional examples demonstrate using a while loop to add numbers and fill a box with tiles until a condition is met.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Pls 3 Solutions

The document discusses programming logic and provides examples of simple selection structures, complex selection structures, and iteration structures using pseudocode. Multiple solutions are provided for various programming challenges around determining qualifications for overtime pay, fitting tiles in a box, selecting the appropriate needle thickness, and calculating insurance premiums based on penalty points. Additional examples demonstrate using a while loop to add numbers and fill a box with tiles until a condition is met.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Logic

Session 3– Activities – Solutions

Note that there are multiple possible solutions to these activities. Those described here are
meant only to serve as a guide.

1. Simple Selection Structures

a)
PROGRAM OVERTIME

1. GET hours worked ;


2. IF hours worked > 38
3. DISPLAY “You qualify for overtime” ;
4. ENDIF

END

b)
c)
PROGRAM OVERTIME

1. GET hours worked ;


2. IF hours worked > 38
3. DISPLAY “You qualify for overtime” ;
4. ELSE
5. DISPLAY “You do not qualify for overtime” ;
6. ENDIF
END

d)
e)
PROGRAM TILES

1. GET width ;
2. GET length ;
3. SET area = width x length ;
4. IF area > 20
5. DISPLAY “Tile does not fit in box” ;
6. ELSE
7. DISPLAY “Tile fits in box” ;
8. ENDIF

END

2. Complex Selection Structures

a)

b)
PROGRAM NEEDLES

1. GET thickness ;
2. IF thickness > 5mm
3. Use 2mm needle ;
4. ELSEIF thickness > 3mm
5. Use 1.5mm needle ;
6. ELSE
7. Use 1mm needle ;
8. ENDIF

END

c)

d)
PROGRAM INSURANCE

1. GET penalty points ;


2. IF penalty points >= 12
3. refuse cover ;
4. ELSEIF penalty points >= 9
5. SET cover = 350 + (350 * 0.4) ;
6. ELSEIF penalty points >= 6
7. SET cover = 350 + (350 * 0.25) ;
8. ELSE
9. SET cover = 350 ;
10. ENDIF

END
3. Iteration Structures

a)

b)

This works

PROGRAM BOX TILES

1. Add tile to box ;


2. WHILE tiles < 12 // (Or tiles != 12)
3. Add tile to box ;
4. DISPLAY “Box not full” ;
5. SET tiles = tiles + 1 ;
6. ENDWHILE
7. DISPLAY: “Box full” ;
8. Shunt box ;

END
The logic:

Number of tiles Condition Outcome of Action


in box condition
1 1 < 12 True Add a tile
2 2 < 12 True Add a tile
3 3 < 12 True Add a tile
4 4 < 12 True Add a tile
5 5 < 12 True Add a tile
6 6 < 12 True Add a tile
7 7 < 12 True Add a tile
8 8 < 12 True Add a tile
9 9 < 12 True Add a tile
10 10 < 12 True Add a tile
11 11 < 12 True Add a tile
12 12 < 12 False Exit Loop

Additional Example of iteration with while loop.

Total the numbers 1 to 5.

1 + 2 + 3 + 4 + 5 = 15

The total at the start is zero. Because we have not yet added any numbers:

SET total = 0 ;

The first number to be added to the total is 1.


SET total = 0 ;
SET number = 1 ;

Set up the loop.

SET total = 0 ;
SET number = 1 ;
WHILE
ENDWHILE

Add the condition for the loop. We want the loop to iterate 5 times.

SET total = 0 ;
SET number = 1 ;
WHILE number < = 5
ENDWHILE

Each time the loop iterates we will add the next number to the total.

SET total = 0 ;
SET number = 1 ;
WHILE number < = 5
SET total = total + number ;
ENDWHILE

Then we will increase the number by a value of 1.

SET total = 0 ;
SET number = 1 ;
WHILE number < = 5
SET total = total + number ;
SET number = number + 1 ;
ENDWHILE

Finally, when all numbers have been added, we display the total.

SET total = 0 ;
SET number = 1 ;
WHILE number < = 5
SET total = total + number ;
SET number = number + 1 ;
ENDWHILE
DISPLAY total ;
The logic:

Total Number Condition Condition Set total Set number


0 1 1 <= 5 True 0+1=1 1+1=2
1 2 2 <= 5 True 1+2=3 2+1=3
3 3 3 <= 5 True 3+3=6 3+1=4
6 4 4 <= 5 True 6 + 4 = 10 4+1=5
10 5 5 <= 5 True 10 + 5 = 15 5+1=6
15 6 6 <= 5 False

You might also like