(Redirected from Finding the Mean of Numbers)
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) |
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) |
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 |
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 arithmetic 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 are diffenrent ways to calculate means. This article currently covers two of them.
Start by 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]
Then, ask the user how many numbers they are going to enter, and set that value to "amount." And then, 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, add up all the values, and set "sum" to the answer of that. Finally, 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))
Alternative method
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))