取消
显示结果 
搜索替代 
您的意思是: 

return specific value from array

已解决!
转到解答

I have a set of code that returns an array of two values. The next step in my code is to choose only one value based on a set of rules:

If both items are equal, return either one of the values.

If exactly one item is equal to 12, return the remaining value (12 could be in the first or second position)

Otherwise, always return the largest value.

 

I can map this out in my head and it seems fairly straightforward, but when I try to turn it into code, it becomes more complicated.

 

I have attached my attempt. Am I overthinking this?

 

Thanks for any help.

0 项奖励
1 条消息(共 5 条)
2,849 次查看

c

Nghtcwrlr

---------------------------------------------------------------------------------------------
*************************************
---------------------------------------------------------------------------------------------
0 项奖励
2 条消息(共 5 条)
2,842 次查看
解答
已被主题作者 MTHPCB 接受

First of all, use the Array Max & Min to get the largest value.  Coincidentally, if they are equal, the Max and Min will be the same value.  So finding the Max will suffice for cases 1 and 3.  So really, we just need to figure out if one of them is 12.  Compare the array to 12.  This will give you a boolean array.  Now use the Boolean Array to Number to get a number and wire that number to a case structure.  If the number is 1, then the first value was 12 and you should return the second value.  If the number is 2, then the second value was 12 and you should return the first value.  If the number is anything else, return the max of the array.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
3 条消息(共 5 条)
2,842 次查看

Thanks. That is definitely much cleaner than I was making it.

0 项奖励
4 条消息(共 5 条)
2,815 次查看

For some reason I was thinking about this problem while washing the dishes and came up with another solution.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 项奖励
5 条消息(共 5 条)
2,797 次查看