well this piece of code is working well, whereas the code i have written for getting maximum marks is not working, can anyone help?

func minMax(array: [Int])->(min:Int, max:Int)?
{
if array.isEmpty
{
return nil
}
var currentMin=array[0]
var currentMax=array[0]

for obj in array[1..<array.count]
{
if obj<currentMin
{
currentMin=obj
}else if obj>currentMax
{
currentMax=obj
}

}
return (currentMin,currentMax)
}
if let bounds=minMax(array:[8, -6, 2, 1,999])
{
print("The minimum value is:\(bounds.min)")
print("The maximum value is:\(bounds.max)")
}
 
 
func topperOfClass(array: [Int])->(Topper: Int){
if array.isEmpty
{
return nil
}
else if ((array.count)<1)
{
return nil
}
else if ((array.count)>1)
{
var temp=array[0]
for obj in array[1..<array.count]
{
if obj>temp{
temp=obj
}
}
return temp

}
}
var abc=topperOfClass(array:[20,10,30,49,46,47])
print("The highest marks obtained in class are: ", abc.Topper)
You already invited:

JavierPons

Upvotes from:

This code has sintaxe erros. But use only `.max()` in the array and it’s enough
1. you are trying to return a Tupple with only one element, it’s not possible.
2. I should be like this:
 
func topperOfClass(array: [Int]) -> Int? {
if !array.isEmpty {
var temp = array[0]

for obj in array[1..<array.count] {
if obj > temp {
temp = obj
}
}

return temp
}

return nil
}

If you wanna answer this question please Login or Register