You are on page 1of 2

Functions Practice For Term I Test

Intro Honors Programming (Math 652)

//Add the sum of the squares of all the numbers in an array of Int's
func sumSquares(values: [Int]) ->Int

//Add the sum of the squares of all even numbers in an array


func sumSquaresEven(values: [Int]) ->Int{

//Given a dictionary of individuals Salaries (in whole dollars),


//find the average salary
func averageSalary(salaries: [String: Int])->Int{

//Given a dictionary of individuals Salaries (in whole dollars),


//find the average salary of all individuals who earn greater than a given amount
func averageSalary(salaries: [String: Int], testAmount: Int)->Int{

//Given an array of doubles find the median.


//reminder: to sort an array use sort()
func median(v: [Double])->Double {

//Consider this function


func mystery(info: [Int])->Int?{
var i = 0
while (i < info.count) && (info[i] % 3 == 0){
i += 1
}
if (i == info.count){
return nil
}
return i
}

//What will the following print:


print(mystery([3, 12, 15, 4, 18, 21]))
print(mystery([3, 12, 15, 33, 18, 21]))

//Consider this function


func mystery2(info: [Int: Int])->Int{
var sum = 0
var find: Int?
for i in 0..<10 {
find = info[i]
if (find != nil){
sum += find!
}
}
return sum
}

//What will the following print:


print(mystery2([3: 15, 12: 29, 8: 16, 13: 25, 1: 5]))
Scanned by CamScanner

You might also like