School ICT Self Study

Nested Structures and Arrays

163 viewsG11-01. Programming
0

  1. Write pseudo code to check if a number is positive and even.

  2. Write pseudo code to print a 3Γ—3 grid of asterisks using nested loops.

  3. Write pseudo code to check if a number is both prime and odd.

  4. What are the key properties of a one-dimensional array?

  5. Write pseudo code to initialize an array with the first five even numbers.

Ruwan Suraweera Changed status to publish May 26, 2025
0

Answers and Descriptions

  1. Answer:

INPUT number
IF number > 0 THEN
    IF number MOD 2 = 0 THEN
        OUTPUT "Positive and even"
    ELSE
        OUTPUT "Positive but odd"
    ENDIF
ELSE
    OUTPUT "Not positive"
ENDIF

Description: Nested If statements check for positive and even conditions, demonstrating layered decision-making in programming.

  1. Answer:

FOR i = 1 TO 3
    FOR j = 1 TO 3
        OUTPUT "*" + " "
    ENDFOR
    OUTPUT newline
ENDFOR

Description: Nested loops iterate over rows and columns to print a grid, testing the ability to manage multiple iteration levels.

  1. Answer:

INPUT n
isPrime = TRUE
IF n MOD 2 = 1 THEN
    FOR i = 2 TO SQRT(n)
        IF n MOD i = 0 THEN
            isPrime = FALSE
            BREAK
        ENDIF
    ENDFOR
    IF isPrime THEN
        OUTPUT "Prime and odd"
    ELSE
        OUTPUT "Odd but not prime"
    ENDIF
ELSE
    OUTPUT "Not odd"
ENDIF

Description: This combines selection (odd check) and iteration (prime check), testing complex nested logic integration.

  1. Answer: Index, contiguous locations, random access.
    Description: A one-dimensional array stores elements linearly, accessed via indices (starting at 0), in contiguous memory, supporting efficient random access.

  2. Answer:

DECLARE array[5] AS INTEGER
array[0] = 2
array[1] = 4
array[2] = 6
array[3] = 8
array[4] = 10

Description: This pseudo code declares and assigns even numbers to an array, demonstrating basic array manipulation skills.

Ruwan Suraweera Changed status to publish May 26, 2025
Write your answer.