From Test-Scratch-Wiki

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 you will need the following variables and lists:

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 you need is a slight alteration of the above script, by changing the > to a <, and changing the variable.

set [Iterator v] to [0]
Set [Lowest # v] to [0]
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 #))

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

Cookies help us deliver our services. By using our services, you agree to our use of cookies.