Step 2: Targeting a rectangle to search for

To search for a particular item in the list, we need to have the list sorted. We'll talk about sorting an unordered list in the next part of this tutorial, but for now we can see that the list of rectangles is already sorted from shortest to longest (since it was created that way).

We'll let the user choose a rectangle they'd like to search for by typing in a number between 100 and 150, and then the program will find that rectangle.

First, so that the user can briefly see the list of rectangles get created, we'll add a short time delay before we ask them to search for a rectangle. Grab the "100 milliseconds have passed" block from Control Flow, and drag it under the first function call, then drag the "Target a rectangle to search for" function call inside the time delay block.

Now we'll define what the function "Target a rectangle to search for" will do. We'll create a variable for the target rectangle height, and we'll prompt the user to enter a number between 100 and 150. Then we'll perform the linear search function.

Grab the "set number" variable from the Properties section of Variables, and create a new variable called "target rectangle height". Drag this block into the "Target a rectangle to search for" function. Then grab the "prompt for a number with message" block from Operators, and type "Type in a number from 100 to 150 to search for". Drag these blocks inside the variable block.

Then create a property number variable for "milliseconds it takes to query the list", and set it to 200.

Lastly, create a new function named "Perform linear search" and grab its function call block to drag to the bottom of the previous function.

Now we can move on to the linear search algorithm.

results matching ""

    No results matching ""