Question Sheet 1 a Solution
Total Page:16
File Type:pdf, Size:1020Kb
Question Sheet 1 – A Solution
Q1. {Algorithm to count #a and #e in text input} READ character
number_a 0 number_e 0
WHILE ( character != ‘z’ )
IF ( character = ‘a’ ) THEN number_a number_a + 1 ENDIF
IF ( character = ‘e’ ) THEN number_e number_e + 1 ENDIF
READ character
ENDWHILE
PRINT “Number of a = “, number_a PRINT “Number of e = “, number_e Q2. {Algorithm to write out numbers in expanded form} PRINT “Input a number” READ number
WHILE ( number != 0 )
FOR ( duplicate FROM 1 TO number ) PRINT number ENDFOR
PRINT “Input another number” READ number
ENDWHILE
Q3.
number total value PRINT - - - READ 5 - - 5 1 - FOR 5 1 1 5 1 1 FOR 5 1 2 5 2 2 FOR 5 2 3 5 6 3 FOR 5 6 4 5 24 4 FOR 5 24 5 5 120 5 PRINT 5 120 6 1. Factorial 2. Result would always be 0 otherwise 3. Set iteration counter to 2 and not 1. Multiply by 1 is redundant.
Q4.
(a) Module Name: calculate_volume_hemisphere Inputs: radius Outputs: hemisphere_volume Process:
{Calculate volume of a hemisphere using the radius}
hemisphere_volume ( 2 * PI * radius3 ) / 3 RETURN hemisphere_volume
(b) Module Name: calculate_volume_cone Inputs: radius, height Outputs: cone_volume Process:
{Calculate volume of a cone using the radius and height} cone_volume ( PI * radius2 * height ) / 3 RETURN cone_volume
Q3. (c)
{ Algorithm to calculate ice-cream cone volumes }
PRINT “Enter radius and height” READ radius, height
WHILE ( radius != 0 OR height != 0 ) total_volume calculate_volume_hemisphere(radius) + calculate_volume_cone(radius,height) PRINT “Total Volume of Ice Cream is “, total_volume
PRINT “Enter another radius and height” READ radius, height
ENDWHILE