Skip to content
Advertisements

Snap! (BYOB): Lesson 5: Finding the Smallest or Largest Number in a List

For background information and how to get started with Snap! (formerly known as BYOB), click here for lesson 1. I’m using BYOB version 3.1.1 for my examples, but you can follow along with either version linked below.

Snap! homepage

BYOB download

Previous lessons:

Snap! (BYOB): The Drag-and-Drop Programming Language for Beginners: Lesson 1

Snap! (BYOB): Lesson 2 – Making a Custom Block and Introduction to Costumes

Snap! (BYOB): Lesson 3 – Building a Number Guessing Game Using Variables, Loops, and If/Else Statements

Snap! (BYOB): Lesson 4: Building a Number Comparison Block Using a Reporter and How It Translates to C+

Sorting through lists is something you will encounter several times when beginning programming. So it’s important to understand the logic in comparing. In a future lesson, I’ll share the C++ equivalent of this Snap! program.

In this lesson, we’re building a program that sorts through a small list of scores and reports the largest number in the list. If you would like to find the smallest number in a list, there’s only one major block difference – the greater than becomes less than and variables named accordingly.

For this exercise, I’m using blocks from the following categories:

  • Control (Brown)
  • Operators (Green)
  • Variables (Orange)

Finding the Largest Number in a List

Under Variables, click Make a block. In the pop-up box, select a category for the custom block, select the reporter option, and name the block Largest number in. Click OK.

Screenshot (299)

Hover over Largest number in and click the plus sign on the right.

Screenshot (300)

On the Create input name box, click the black arrow.

Screenshot (301)

Under Create input name, write the word list. In the Input type, Shape of slot section, select List. Click OK.

By specifying the input type, the final block will only allow lists.

Screenshot (302)

Screenshot (303)

The block should look like this

Under Variables, grab the script variables block.

We will use this block to create all variables the program needs.

Screenshot (304)

Click on the little a. In the pop-up box, rename the variable largestNumber. Click OK.

Screenshot (305)

The black arrows allow us to add more variables. Make one more variable and name it indexLocation.

Screenshot (306)

Screenshot (307)

The block should look like this

Under Variables, grab two set _ to 0 blocks.

Screenshot (308)

Click the first box in both blocks. Set the first block to largestNumber and the second block to indexLocation.

Now, the variables need a starting point.

Screenshot (309)

Under Variables, grab the item 1 of _ block. Place it in the set largestNumber to _ block.

Screenshot (310)

Drag the list variable into the last slot.

The largestNumber is set to the first number in the list because it’s the largest number we’ve seen when the block begins running.

Screenshot (311)

In the set indexLocation to _ block, change the 0 to 1.

Screenshot (312)

Under Control, grab the repeat block. Place it at the end.

Screenshot (313)

Under Variables, grab the length of _ block and place it in the spot with the 10.

Screenshot (314)

Drag the list variable and place it in the empty slot in the length of _ block.

The repeat block will run through the entire list.

Screenshot (315)

Under Control, grab the if _ block. Place it inside the repeat block.

Screenshot (316)

Under Operators, grab the _ > _ (greater than) block. Place it as the conditional.

Screenshot (317)

Under Variables, grab the item 1 of _ block. Place it in the left slot in the greater than block.

Screenshot (318)

Drag the indexLocation and list variables and place them in the item 1 of _ block as pictured below.

Screenshot (319)

Drag the largestNumber variable and place it into the right slot of the greater than block.

Screenshot (320)

Under Variables, grab the set _ to 0 block. Place it inside the if block.

Screenshot (321)

Right click the item indexLocation of list block above and select duplicate.

Screenshot (322)

Place the copy in the right slot of the set _ to 0 block and change the left box to largestNumber.

Here’s what the if does: If the number at the current index is larger than the current value at largestNumber, it becomes the new largestNumber. This step-by-step comparison repeats throughout the length of the list.

Screenshot (323)

Under Variable, grab the change _ by 1 block. Place it after the if block, but stay inside the repeat block.

Screenshot (324)

Click the down facing arrow and select indexLocation. The 1 stays unchanged.

Here’s what it does: The index location begins at 1, comparing the  first number to largestNumber. The index changes to 2 and the comparison process continues.

Screenshot (325)

Under Control, grab the report _ block. Place it on the very end, outside the repeat block.

Screenshot (326)

Drag the largestNumber variable and place it inside the report block. Click OK.

Screenshot (327).png

Under Control (or whichever category you chose in the first step), grab our custom block. It should like the one below.

Screenshot (328)

Now we need lists to sort. Under Variables, click the make a list option. Name the list and click OK.

I’m making two lists to demonstrate the capabilities of our custom block. You can make one or two to follow along.

Screenshot (329)

Screenshot (330)

On the stage, you’ll see an empty list of scores. Click any corner and drag outward to resize the box.

Screenshot (331)

Click the plus sign on the bottom left to input scores. Write any score, press Enter, and repeat. When you have your desired number of scores, click anywhere off the list to exit.

Screenshot (332)

If you create too many slots, simply hover on the line to bring up an x (delete option).

Screenshot (333)

Screenshot (334)

My lists

The blocks for Scores1 and Scores2 (or the name of your list) are found under Variables.

Screenshot (335)

Grab the list name and place it in the Largest number in _ block.

Screenshot (336)

Click it for the result.

Screenshot (337)

Any list works in our custom block.

Screenshot (338)

It works!

I’ve included the blocks for smallest number in a list below.

Screenshot (340)

Screenshot (341)

It works too!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: