0

Using swift programming

import UIKit

var str = "Hello, playground"

func ls(array: [Int]) -> (large: Int, small: Int)//function

{
    var lar = array[0] // array of large number
    var sma = array[0] // array of small number
    let num = 0 // check odd or even

    for i in array[1..<array.count] {
        if i < sma && num % 2 == 0  {
            sma = i
        }else
            if (i > lar) {
            lar = i

        } else
                if num  % 2 == 0 {
            print("this is an even number")
        } else
                {
            print("this is an odd number")
        }

    }
    return (lar, sma)
}
let num = ls(array: [40,12,-5,78,98])
print("Largest number is: \(num.large )  \("\n") smallest number is: \( num.small)")

Edited by Reverend Jim: Moved to programming forum

2
Contributors
5
Replies
29
Views
5 Months
Discussion Span
Last Post by JamesCherrill
1

That's because you have the if test for even/odd tangled up with the if tests for largest/smallest. Right now you don't get to the even/odd code (lines 20-25) if the number matches either of the if tests on lines 13 and 16.
You need to separate the largest/smallest stuff from the even/odd stuff

if is largest so far ...
else if is smallest so far ...

if is even ...
else ...

Edited by JamesCherrill

0

You still didn't separate the two sets of if tests - the else on line 21 is creating the problem.
Look again at the if/else structure I showed you in my previous post. See how yours is different?

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.