In other languages

Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

Finding the Range of Numbers

Finding the Range of Numbers means finding what a list of numbers ranges from. This can be done by subtracting the lowest value in a list of numbers, from the highest value.

Doing it in Scratch

Method 1

For this tutorial the following variables and lists will be required:

Variables:

  • Highest # — Holds the value of the highest number
  • Lowest # — Holds the value of the lowest number
  • Iterator — A counter variable
  • Range — Holds the answer

Lists:

  • List of Numbers — The list in which all the values are kept

Step 1

This step finds the highest value in the list. To do this use this simple script:

set [Iterator v] to [0]
set [Highest # v] to [0]
repeat (length of [List of Numbers v])
change [Iterator v] by (1)
if <(item (Iterator) of [List of Numbers v]) > (Highest #)> then
set [Highest # v] to (item (Iterator) of [List of Numbers v])
end
end

Step 2

This step finds the lowest value in the list. To do this, all that is needed is a slight alteration of the above script, by changing the > to a <, changing the variable, and setting the lowest number to the highest number.

set [Iterator v] to [0]
Set [Lowest # v] to (Highest #)
repeat (length of [List of Numbers v])
change [Iterator v] by (1)
if <(item (Iterator) of [List of Numbers v]) < (Lowest #)> then
set [Lowest # v] to (item (Iterator) of [List of Numbers v])
end
end

Step 3

The final step simply subtracts the lowest value from the highest value, and sets the answer variable (Range) to that value.

set [Range v] to ((Highest #) - (Lowest #))
Warning Note: This method only works if the highest number is positive.

Method 2

In this method, the first step is sorting the list. Then, add this script to the bottom:

set [Range v] to ((item (last v) of [Text v]) - (item (1 v) of [Text v]))

Range will contain the range.

See Also

  • This page was last modified on 5 January 2018, at 07:46.