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)")

Recommended Answers

All 5 Replies

OK, that's neat code. Did you have a question?

It doesn't show the even or odd number

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 ...

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.