Skip to main content
14-Alexandrite
July 12, 2019
Solved

Finding index

  • July 12, 2019
  • 2 replies
  • 5957 views

Hello,

 

I am working with an array of positive and negative numbers. My logic involves such that it has to find the index value of a changed sign number immediately above. For example, in my array, if I work with "-4" it has to find the index number of "2", not "3". Similarly, if I work with "4" it has to find the index number of "-5", not "-4". Please help me with this problem. My version is Prime 4.0.

 

Thanks,

Faisal

Best answer by Werner_E

If you want to ignore the first few values when you chose a value which occurs multiple times in your vector, then the attached file should help.

 

Concerning your answers: Sure the question about the value which occurs multiple times in the vector was the most important one and I am glad you realized yourself at last that this is a problematic situation.

But as with the others - stating that this or that problematic situation will not occur as you will never enter an input value which has no sign change before, does not help much. In my opinion when programming a function, it must always be exactly determined what should happen in every possible input case. So even though at the moment you do not intend to input an invalid value, it must be defined what the reaction of the program should be if one day you do. The reaction could be a customized error message, or a return value which signals that something unusual had happened (could be a NaN or an invalid index value).

Here is a routine which copes with the most obvious errors and I decided that the function should fail with an appropriate error message without returning any result.

B1.png

You can find a version which uses if-statements instead of the if functions in the attached sheet.

Without any customized error messages it could be reduced to

B2.png

and here is yet another, different approach (could be a 1-liner which also works in Prime Express if you don't mind typing "ORIGIN" instead of just "O" five times) - again without error handling (which I still regard as a bad design error):

B3.png

BTW, you claimed that Luc's approach would not return the index but the value itself. Thats not true! Luc's function "f" returns the index as you demanded. He just showed after calling his function which value would be addressed. His version avoids programming and implements the necessary loops via recursive calls of an if-function. That way his approach also works in Prime Express. Its quite easy to modify his version to fit the new conditions. Additionally I got rid of the function "Sign" and changed the order of the arguments to bring it in line with my functions.

B4.png

So its up to you now to decide for one of the versions.

 

2 replies

25-Diamond I
July 12, 2019

Whats the input (apart from the vector) of the function your a looking for and whats the output?

 

Is the output

a) the value with a different sign right before the input

or

b) the index of the value with the different sign

As I understand your description its b). What should be returned if no suitable value is found? Maybe ORIGIN-1 or maybe NaN?

 

Its the input

a) the index of a value in the vector

or

b) the value itself?

If b), what should be the output when the input is a value which occurs multiple times? I in your example vector, what would be the out for -1? Should it be 1 or 3 or both in a result vector? What is the output if the input is 1?

 

What about zeros? Do the count as changed sign or not?

What if the input value is zero?

What should be the output if no value with different sign is found above the input value?

fahmed-214-AlexandriteAuthor
14-Alexandrite
July 12, 2019

The input will be an array of positive and negative numbers. Yes, there can be zeros and that should be considered as a positive. The goal is to find the index of a changed sign number just immediately previous. For example, in my array, if I work with "-4" it has to find the index number of "2", not "3". Similarly, if I work with "4" it has to find the index number of "-5", not "-4". Please help me with this problem. My version is Prime 4.0.

25-Diamond I
July 12, 2019

You haven't answered the questions!

You simply repeated what you had already written in your first post.

 

Additionally:

If zero is considered positive, then 5;0;4 would mean no sign change, bit -5;0;-4 is considers a sign change. Correct?

fahmed-214-AlexandriteAuthor
14-Alexandrite
July 13, 2019

I think I found a solution but I believe there is a much smarter and shorter way. Also, it does not do well if some numbers are repetitive. For example, I have two "-3" s and if I need to work with the later one, it will not give the right index value

25-Diamond I
July 13, 2019

@fahmed-2 wrote:

I think I found a solution but I believe there is a much smarter and shorter way. Also, it does not do well if some numbers are repetitive. For example, I have two "-3" s and if I need to work with the later one, it will not give the right index value


Unfortunately you avoided to answer all of my questions, systematically one after the other.
The problem you experience with repetitive numbers was in fact already addressed by one of my questions - here is the question for the third time:

"If b), what should be the output when the input is a value which occurs multiple times? I in your example vector, what would be the out for -1? Should it be the index of the 1 or the index of the 3 or both in a result vector? What is the output if the input is 1?"

How should a program know which of the number -3's in your new example you would like to "work with" if you just provide the number -3 as input?

I can think of different ways to deal with that situation:

  1. Always use the very first occurrence of the number and ignore the others (this is what Luc's approach and yours,too, are doing).
    Given your remark it seems not to be what you want
  2. Always use the very last occurrence of the number and ignore the others. I suspect this is also not what you want to happen.
  3. The program could return the result indices for ALL of the multiple numbers in a vector.
    In your new example the result of input=-3 would be a vector with the elements 1 and 4, the indices of the second number 1 and number 2.
    If this is what you like, you would further have to decide and say if you want the result be a vector in all cases or just when there are multiple results.
  4. Provide as input the index of the number you want to work with instead of the number itself.
    For me this seems to be the most straightforward approach.

 

Another unanswered question is the decision what you want to happen if no sign change is found. In your example this is the case when your input is 1. Your approach fails with an error and Lucs function returns NaN instead of an index number.

 

fahmed-214-AlexandriteAuthor
14-Alexandrite
July 13, 2019

Except the second one, I think I have addressed your questions

 

1. What, if the input value is a number which does not occur in the vector? What should the function return? Error message, NaN, ORIGIN-1 (an invalid index)

 

My reply was: If you are concerned about what if the input does not match the given array then please consider that the input is based on the given array, it will not input anything outside that

 

3. What should be returned if no suitable value is found? Maybe ORIGIN-1 or maybe NaN?

 

My reply was: If the problem does not experience a sign change then I have a separate approach for that

 

Regarding number 2 problem, I think  your solution as "Always use the very last occurrence of the number and ignore the others" will fit my logic. Luc's approach is great but as I mentioned, I need the index number of the first occurrence of opposite sign number above, not the number itself