Jump to content

Programming Fundamentals/Math Statistics with Arrays

From Wikibooks, open books for an open world

Overview

[edit | edit source]

Statistics is a branch of mathematics dealing with the collection, organization, analysis, interpretation, and presentation of data. Common statistical methods include mean (or average) and standard deviation.[1]

Discussion

[edit | edit source]

Arrays can store words, letters/characters (strings) and numbers (integers/floats). Any type of array containing words, numbers or a combination can use a built-in function like len(Python exclusive) to find the number of elements in an array to help display output and parse lines. All arrays can also handle functions that allow the user to sort array values from highest to lowest (or vice versa). Other functions are only intended to handle arrays with numbers. For example, When arrays contain numbers, the elements of the array can be added together using the sum function. Since the built-in sum function cannot handle strings without producing an unsupported operand type error, we use this function only to add numbers, rather than join strings together.

We will continue learning about the sum function (also known as totaling) in this module. In the example below, the sum function totals the array passed to it. Other mathematical functions often associated with statistics such as: average, count, minimum, maximum, standard deviation, etc. are often developed for processing arrays.

Pseudocode

[edit | edit source]
Function Main
    Declare Integer Array ages[5]
    Declare Integer total
    
    Assign ages = [49, 48, 26, 19, 16]

    Assign total = sum(ages)

    Output "Total age is: " & total
End

Function sum (Integer Array array)
    Declare Integer total
    Declare Integer index
    
    Assign total = 0
    For index = 0 to Size(array) - 1
        Assign total = total + array[index]
    End
Return Integer total

Output

[edit | edit source]
Total age is: 158

Key Terms

[edit | edit source]
sum
is a built-in function, which adds the elements of an array together.
len
is a built-in function and it returns the number of items in an object.

References

[edit | edit source]