Lucas
Lucas

Reputation: 65

Value of type Float has no member enumerated

I have an array. The goal is to, remove the first two index values THEN recall the max 3 values (and later average them). My problem is recalling the 2nd and 3rd max value My method is to recall max, then remove the max value and call the new array array1, then recall the max value from this array which should be the overall second highest/maximum value.

//To Call Max value

var ammendedInitialArray = initialArray.dropFirst(2)        
    var max = Float(-1.0)
    var stepmax1 = Int(-1)
    for (index, value) in ammendedInitialArray.enumerated() {
        if value > max{
            max = value
            stepmax1 = index
        }

ABOVE WORKS

 ammendedInitialArray1 = ammendedInitialArray.remove(at: stepmax1)
    var max2 = Float(-1.0)
    var stepmax2 = Int(-1)
   *****for (index, value) in ammendedInitialArray1.enumerated{  *****
        if value > max{
            max2 = value
            stepmax2 = index
        }

The ***** line comes up with the error: Value of type Float has no member enumerated

Well I didn't mean for it to be a float, I want it to be an array.

Upvotes: 1

Views: 2664

Answers (2)

Alexander
Alexander

Reputation: 63281

remove(at:) will remove the element from the array in place, and returns the element that has been removed. Not the array.

Try:

ammendedInitialArray.remove(at: stepmax1)
var max2 = Float(-1.0)
var stepmax2 = Int(-1)
for (index, value) in ammendedInitialArray.enumerated() {
    if value > max {
        max2 = value
        stepmax2 = index
    }

Upvotes: 0

Aaron
Aaron

Reputation: 6714

You could do the whole operation this way:

e.g:

var initialArray = [2,5,17,1,3,5,14,6,4,8,7,9]

var newArray = initialArray.dropFirst(2).sorted().reversed()
// ^ 17,14,9,8,7,6,5,4,3,1 
let topThreeValues = Array(newArray.prefix(3))
// ^ 17,14,9
topThreeValues.reduce(0, +)/topThreeValues.count
// ^ 13

Upvotes: 2

Related Questions