(Redirected from Finding the Average of Numbers)

DocumentInQuestion.png It has been suggested that this page's contents be merged with the page Finding the Mode of Numbers. You can discuss this on the page's talk page. (July 2021)
DocumentInQuestion.png It has been suggested that this page's contents be merged with the page Finding the Median of Numbers. You can discuss this on the page's talk page. (July 2021)
DocumentInQuestion.png It has been suggested that this page be moved to the new title Finding the Average of Numbers. You can discuss the move on this page's talk page. (July 2021)
Reason: Because the Median and Mode articles should be merged into this

Wikipedia-logo.svg  For more information, see Arithmetic Mean on Wikipedia.

"Finding the Average of Numbers" redirects here. For other measures of the center of a set, see Finding the Average of Numbers (disambiguation).
"Mean" redirects here. For the FAQ page about inappropriate behavior, see What do I do if I see a Scratcher violating the Community Guidelines?.

Finding the mean of numbers (sometimes called finding the average) is adding up a set of values and dividing by the number of values.

How to do it in Scratch

There is more than one method, however, this article will only explore two of them, which are some of the simpler methods.

You must start off with creating four variables:

  • A variable that will ultimately contain the answer (called "sum" in this tutorial)
  • A variable that will hold the number of values you are calculating the mean of (called "amount" in this tutorial)
  • A variable that is used as a repeater (called "iterator" in this tutorial)
  • A list that holds the numbers which you are calculating the mean of (called "numbers" in this tutorial)

This tutorial will use the flag to start the script.

Start off by deleting all of "numbers" and setting the three variables to what they need to be.

when green flag clicked
delete all of [numbers v]
set [iterator v] to [1]
set [sum v] to [0]
set [amount v] to [0]

You then must ask the user how many numbers they are going to enter, and set that value to "amount." And then you must add all those numbers to the list, "numbers."

ask [How many numbers are you calculating the mean of?] and wait
set [amount v] to (answer)
repeat (amount)
  ask [Add a number] and wait
  add (answer) to [numbers v]
end

Then you must add up all the values, and set "sum" to the answer of that. Finally, you must set "sum" to "sum" divided "amount"."

repeat (length of [numbers v])
  change [sum v] by (item (iterator) of [numbers v])
  change [iterator v] by (1)
end
set [sum v] to ((sum) / (amount))

There is an alternative way to do this, requiring less scripting and therefore increasing speed. The script is as follows:

ask [How many numbers?] and wait
set [amount v] to (answer)
delete all of [numbers v]
repeat (amount)
  ask [Enter a number:] and wait
  change [total v] by (answer)
  add (answer) to [numbers v]
end
say ((total) / (amount))

And that is how to find the mean of numbers in Scratch.

See Also

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