# How does this algorithm expressed in pseudo-code work?

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:

RECEIVE item FROM KEYBOARD
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
ELSE
SET index TO index + 1
END IF
END WHILE
IF found = False THEN
SEND 'Item not found.' TO DISPLAY
END IF

• 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. – David Richerby Feb 14 '18 at 16:05
• 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. – Python Feb 14 '18 at 17:13
• It doesn't mean that the item is found in line 4. – David Richerby Feb 14 '18 at 17:17
• 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. – Yuval Filmus Feb 14 '18 at 17:33
• @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. – David Richerby Feb 19 '18 at 8:07

## 1 Answer

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.