# Class Method: $vararg::reset()

Updated: 1/18/2022, 5:31:44 PM
Created: 1/18/2022, 5:31:44 PM
Last Updated By: dependabot[bot]
Read Time: 1 minute(s)

# Description

The purpose of the reset() method is to allow reprocessing a list of arguments within the same method.

The reset() method performs the following actions to a $vararg object:

  • the argument list pointer is set to the beginning of the list
  • the size property of the $vararg object is reset to the original size of the argument list

# Example

Create this method, which calculates the Standard Deviation:

    method Math::standard_deviation()
* Credit: http://www.mathsisfun.com/data/standard-deviation-formulas.html

* First, find the mean of the set of numbers
        addends = new object("$vararg")
        sum = 0
        loop while addends->size() do
            sum += addends->next()
        repeat
        addends->reset()
        mean = sum/addends->size()

* Subract the mean and square the result
        squared_differences = 0
        loop while addends->size() do
            squared_differences += pwr((addends->next() - mean), 2)
        repeat

* Get the mean of the squared differences
        addends->reset()
        variance = (1/(addends->size()-1)) * squared_differences

* Take the square root of the variance
        standard_deviation = sqrt(variance)
        return standard_deviation
    end method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

Line 10 resets the addends object for both the calculation on line 11 and the subsequent loop on lines 15-17.

Line 20 resets the addends object for the calculation on line 21.

Note that, for the calculations on lines 11 and 21, we could have done this after line 5:

addends_size = addends->size()
1

and used addends_size, but we still would have had to reset the object for the loop on lines 15-17.

Create a program to use the method:

* Calculate the Standard Deviation of High Temperatures over a period of 10 days

    sd = Math::standard_deviation(56,66,68,59,71,49,52,73,75,65)
    crt "Standard Deviation = ":sd
1
2
3
4

Note that we are doing a static method call.

Result:

Standard Deviation = 8.9835
1

# Notes