The flowchart below shows an algorithm that searches an array of 5 elements for a target value. The array Data contains: [12, 7, 25, 3, 18] (a) State the ty...

Assessment: Computer Science (9-1) 0984 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science (9-1) - 0984

Question 1 Report

The flowchart below shows an algorithm that searches an array of 5 elements for a target value.

diagram

The array Data contains: [12, 7, 25, 3, 18]

(a) State the type of search shown in this flowchart. [1]

(b) State the output if the Target is 25. [1]

(c) Explain why this flowchart uses a WHILE-style loop rather than a simple FOR loop. [2]

Answer Details

(a) The flowchart shows a linear search. [1]

A linear search checks each element of an array one by one, starting from the first element and moving sequentially through the array until the target value is found or the end of the array is reached.

(b) If the Target is 25: [1]

The search starts at Index 1. Data[1]=12, not 25. Index becomes 2. Data[2]=7, not 25. Index becomes 3. Data[3]=25, which equals the Target. Found is set to TRUE. The loop exits because Found = TRUE. The final decision box checks Found = TRUE, which is true, so the algorithm outputs that the item was found at position 3.

(c) Why the flowchart uses a WHILE-style loop rather than a simple FOR loop: [2]

The WHILE-style loop has two conditions: Index <= 5 AND Found = FALSE. This allows the search to stop early as soon as the target is found. [1] When Found becomes TRUE, the loop condition fails and the algorithm exits immediately without checking the remaining elements.

A simple FOR loop would continue iterating through all remaining elements even after the target has been located. [1] For an array of 5 elements this is a minor inefficiency, but for large arrays with thousands of elements, stopping early when the target is near the beginning saves significant processing time. The WHILE-style loop is therefore more efficient on average.

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning