Correct algorithm for array search

2025-familiarisation-se-q08 · Code Response · 3 marks

Source: NESA 2025 HSC Software Engineering Familiarisation Q8

Question

The following subroutine is intended to search an array for an item and output the position of that item in the array. There are several mistakes in the algorithm.

BEGIN Search (SearchItem)
  Set LastIndex to the number of elements in ItemArray
  Index = 1
  WHILE Found = FALSE OR Index < LastIndex
    IF Index = SearchItem THEN
      Found = TRUE
    ENDIF
  ENDWHILE
  IF Found = TRUE THEN
    Display 'Position', Index
  ELSE
    Display 'Not found'
  ENDIF
END Search

Copy the algorithm into the space below and modify the algorithm logic so that it achieves its intended purpose. The array is called ItemArray and it is indexed from 1.

Response

Reveal answer
BEGIN Search (SearchItem)
  Set LastIndex to the number of elements in ItemArray
  Found = FALSE
  Index = 1
  WHILE Found = FALSE AND Index <= LastIndex
    IF ItemArray[Index] = SearchItem THEN
      Found = TRUE
    ELSE
      Index = Index + 1
    ENDIF
  ENDWHILE
  IF Found = TRUE THEN
    Display 'Position', Index
  ELSE
    Display 'Not found'
  ENDIF
END Search

Marking rubric

MarksDescription
3Corrects the search logic, loop condition, array comparison and index update.
2Corrects several major logic errors.
1Shows some understanding of array search logic.

Explanation

The algorithm needs to initialise Found, compare SearchItem with ItemArray[Index], increment the index, and stop when the item is found or the array has been fully searched.

Metadata

Submitter
Seed data
Created
2026-05-02
Status
published
Syllabus
y12-project-test-and-debug-code y12-project-tools-for-ideas-solutions
Tags
pseudocode arrays searching debugging