0

I have two arrays

var Status: [Int] = [0,1,0,1,1] //which will contain at random a 0 or 1 at all five index

var Total: [Int] = [0,85,90,90,0] //which will contain any positive Int at all five index

I want to write a func that will scan Status and return only the indexes which contain a 1.

Then scan Total at these same indexes to see if the Ints are equal

In this case indexes 1,3,4 would be returned from Status scan, and Total would be scanned at [1,3,4] to return 85,90,0.

So something like this if I could come up with the correct syntax

var Status: [Int] = [0,1,1,1,1]

var Total: [Int] = [3,4,16,2,3]

var IndexesToCompare: [Int] = [Int]()

Status.scan for value '1', return indexes to IndexesToCompare

var TotalsToCompare: [Int] = [Int]()

Total.scan @Index[IndexesToCompare], return values to TotalsToCompare

if TotalsToCompare.scanIfAllIntegarsAreEqual == true {println("EQUAL")} else {println("Unequal")}

This would print out "Unequal", but if Status were changed to [1,0,0,0,1] it would print out "EQUAL"

1 Answer 1

1

If I understand what you want. You want to look at Status and find all the 1's and with those get the corresponding values in Total. Then with those values find the average value and then compare that to each individual value. If all the values equal the average then return true else false. I'm assuming the the function has access to Status and Total and that they are the same size. Returns true if all the values are the same and false if they are not.

func yourFunc()->Bool{
    var values = [Int]()
    for index in 0..<Status.count{//loop through Status
        if Status[index] == 1{//when you find a 1
            //append it's corresponding value in Total to values
            values.append(Total[index])
        }
    }
    var average = values.reduce(0,+)/values.count
    for value in values{
        if value != average{
            return false
        }
    }
    return true
}

Note: I personally only like to capitalize Classes and not variables as convention.

Sign up to request clarification or add additional context in comments.

5 Comments

this works for returning all Total values that matched to Status's 1's, so here it would return [4,16,2,3]...but how do I test values to see if all members are equal or not?
basically i want to know when values is [90,90,90] or [85,85] or [0,0,0,0,0]
Look at the edit. Second loop looks at the values and returns false if one of them doesn't match.
You could also use the half open range operator, "for index in 0..<Status.count" instead of "for index in 0...Status.count-1"
needs to be var average = values.reduce(0,+)/values.count and are index and value specific terms or can they be total ambiguous variable identifiers?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.