Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*Create a playground project in XCode 9(Swift4) 1) Methods : i.) Write a method

ID: 3877441 • Letter: #

Question

*Create a playground project in XCode 9(Swift4)

1) Methods :

i.) Write a method that take an integer array as a parameter and returns the sum of positive odd numbers and sum of positive even numbers.

ii.) Define and implement a method that takes a string array as a parameter and returns the length of the shortest and the longest strings in the array

iii.) Implements the sequential search method that takes and array of integers and the item to be search as parameters and returns true if the item to be searched in the array, return false otherwise


2) Dictionary Data Structure

a) Declare a dictionary that stores the city name and the population in that city. For example, Paris = > 2.2 million (add 5 records to your dictionary)

b) Display city name with largest population

c) Display the city name with smallest population

Explanation / Answer

1 (i). DisplaySum method that will take an integer array as a parameter and returns the sum of positive odd numbers and sum of positive even numbers, bellow given the code is

func displaySum(array: [Int]) -> (oddSum: Int, evenSum: Int){
     var oddSum = 0
     var evenSum = 0
    for nums in array{
        if nums % 2 == 0{
            evenSum += nums
        }else{
            oddSum += nums
        }
    }
  
    return (oddSum, evenSum)
}

let theArray = [3,4,2,6,7,7,5,6,8]

let c = displaySum(array: theArray)

print("Even Sum is (c.evenSum) and Odd Sum is (c.oddSum)")

(ii). ReturnLengthOfString method that will takes a string array as a parameter and returns the length of the shortest and the longest strings in the array, bellow given the code for it

func returnLengthOfString(array: [String]) -> (lenghtShortest: Int, lengthLongest: Int){
    var indexL: Int = 0
    var elLengthL: Int = array[0].count
  
  
    var indexJ: Int = 0
    var elLengthJ: Int = array[0].count
    var j: Int = 0
  
    for var i in 0..<array.count{
        if array[i].count > elLengthL{
            i = i+1
            indexL = i
            elLengthL = array[i].count
        }else{
            j = j+1
            indexJ = i
            elLengthJ = array[i].count
        }
    }
  
  
  
    var lenghtShortest: Int = array[indexJ].count
    var lengthLongest: Int = array[indexL].count
  
    return (lenghtShortest, lengthLongest)
}

let theArray = ["hello","hello world","hello test array","xy","xyzx"]
let c = returnLengthOfString(array: theArray)
print("Length of Shortest string is (c.lenghtShortest) and Length of Longest string is (c.lengthLongest)")

(iii). Implements the sequential search method that takes and array of integers and the item to be search as parameters and returns true if the item to be searched in the array, return false otherwise, bellow the code given

func seqSearchData(array: [Int], element: Int) -> Bool{
    var result: Bool = false;
    for var i in 0..<array.count{
        if array[i] == element{
            result = true;
        }else{
            result = false;
        }
    }
  
    return result
}

let theArray = [4,6,2,1,5,6,7,8,9,3,5]
let c = seqSearchData(array: theArray, element: 12)
print(c)