On a website it gives you questions and answers for computer science, yet does not explain the answer. I was hoping if someone could explain the pseudo-code (answer) for me, as I do not understand it. Especially the store index part - which is basically the entire pseudo-code. Thank you. More specifically: line 4, line 8 and why found = False in line 4 means item is found but in line 11 means item not found.

The question is:

Write an algorithm expressed in pseudo-code to ask a user for a search item and carry out a linear search on data stored in an array to find that item (6 marks).

The answer is:

SET found TO False
SET index to 0
WHILE found = False AND index <= LENGTH(store) - 1 DO
  IF store[index] = item THEN
    SET 'Item found' TO DISPLAY
    SET index TO index + 1
IF found = False THEN
   SEND 'Item not found.' TO DISPLAY
  • $\begingroup$ I suggest you just try to "run" this program using a pencil and paper. It really is very straightforward and you should be able to figure it out as long as you understand the basic elements of programming, such as loops, conditional statements and arrays. If you still can't figure it out, try to ask a more specific question about the part you don't understand. At the moment, it's very hard to judge what level to pitch an answer at. $\endgroup$ – David Richerby Feb 14 '18 at 16:05
  • $\begingroup$ More specifically: line 4, line 8 and why found = False in line 4 means item is found but in line 11 means item not found. $\endgroup$ – Python Feb 14 '18 at 17:13
  • $\begingroup$ It doesn't mean that the item is found in line 4. $\endgroup$ – David Richerby Feb 14 '18 at 17:17
  • $\begingroup$ There is a bug in the code. After (or before) the line SET 'Item found' TO DISPLAY, there should appear the line SET found TO true. $\endgroup$ – Yuval Filmus Feb 14 '18 at 17:33
  • $\begingroup$ @YuvalFilmus Well spotted. This is probably an editing error, since SET found TO true; SEND 'Item found' TO DISPLAY has had the middle part deleted. $\endgroup$ – David Richerby Feb 19 '18 at 8:07

The algorithm is pretty much straight forward. I'll try to explain this algorithm in basic English here without using pseudo-code.

First you are taking input from the user about the element they need to search for in an array you already have.

Then you are initializing a boolean data type to false(This is used to check whether you have found the element you need or not) and an integer data type to 0(This is used for index of the array).

In the next step, you run a while loop till you either reach the end of the array or you have found the number user wants.

You compare each element of an array with the number user wants and if the present element in the array is not the same as the user wants, you increase the value of index by 1. Else, if you have found the number user wanted, you just change the value of boolean data type to true and the loop gets terminated at that instant itself.

In the last step, you check whether the value of boolean changed or not. If it is still false, you display a message.

I hope this way it was clear.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.