0% found this document useful (0 votes)
64 views3 pages

Statistical Functions in Swift

The document defines functions to calculate the sum, mean, median, and standard deviation of an array of double values. It provides an example array of numbers, calls each function on that array, and prints the results.

Uploaded by

Bob Jeff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views3 pages

Statistical Functions in Swift

The document defines functions to calculate the sum, mean, median, and standard deviation of an array of double values. It provides an example array of numbers, calls each function on that array, and prints the results.

Uploaded by

Bob Jeff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Chris Mundanchery

10/16/2020
Assignment #8

func sum(_ values: [Double]) -> Double {


var sum2 = 0.0
for i in values {
sum2 += i
}
return (sum2)
}

let values = [5.0, 10.0, 15.0, 20.0, 25.0, 30.0]


print("sample data array: \(values)")
print("sum is... \(sum(values))")

func mean(_ values: [Double]) -> Double {


var sum3 = 0.0
for i in values {
sum3 += i
}
return (sum3/Double(values.count))
}

print("mean is... \(mean(values))")


func median(_ values: [Double]) -> Double {
if (values.count % 2) == 1 {
return values[((values.count - 1)/2)]
} else if (values.count % 2) == 0 {
return (values[(((values.count)/2) - 1)] +
values[((values.count)/2)])/2
}
return values[((values.count - 1)/2) - 2]
return values[((values.count)/2) - 1]
}

print("median is... \(median(values))")

import Foundation

func stdev(_ values: [Double]) -> Double {


let avg = mean(values)
var sum = 0.0
for i in 0..<values.count {
sum += (values[i] - avg) * (values[i] - avg)
}
return sqrt(sum/Double((values.count - 1)))
}

print("standard deviation is... \(stdev(values))")

You might also like