Why this simple program doesn't work ? It should print out min and max of an array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why this simple program doesn't work ? It should print out min and max of an array

var array = [9,3,5,7,24,32] var CurrMax = array[0] var CurrMin = array[0] func MinMax(array:[Int])->(Max:Int,Min:Int){ for i in 1...array.count{ if CurrMax<array[i]{ CurrMax = array[i] } else if CurrMin>array[i]{ CurrMin = array[i] } } return (CurrMax,CurrMin) } MinMax(array:[9,3,5,7,24,32])

21st Jun 2017, 9:41 AM
Developer 39
Developer 39 - avatar
1 Answer
+ 1
I always follow the KISS (Keep It Simple, Stupid) principle in programming (and life) 😀 on top of that I am a lazy boy who doesn't want to reinvent the wheel 😗 I don't know SWIFT but in Swift 3 - should one want to use built-in functions -what works is this: let numbers = [8, 99, -78, 0, 44] print(numbers.min()) print(numbers.max()) I may be missing the point. In such a case please be more specific.
23rd Jun 2017, 3:31 PM
Skipper
Skipper - avatar