cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Finding index

SOLVED
Garnet

Finding index

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

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Finding index

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.

 

15 REPLIES 15

Re: Finding index

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?

Re: Finding index

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.

Highlighted

Re: Finding index

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?

Re: Finding index

yes, it is considered as a sign change. Till now, my array will have different signed numbers so it will not be a case where all has the same sign numbers. The input will be the values itself but my task is to find the index numbers where I have a sign change as mentioned previously. Thank you

Re: Finding index

Still not clear!

 

So the input (apart from the vector) is a value, not an index, but the output is an index, correct?

So a new question arouses: 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)

 

I'm referring to my answers above now

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

This is the case, if the vector is [1;2;3;4;-5;6] and your input is 3. There is no sign change before the 3

 

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?

 


@fahmed-2 wrote:

yes, it is considered as a sign change.

So, just to be sure: [4;0;5] is a vector without any sign changes and [-4;0;-5] is considered a vector with two sign changes, correct?

 


Till now, my array will have different signed numbers so it will not be a case

This does not matter as the function to be written has to cope with all eventualities. And even if there are always values with different signs in your vector it might happen that there are no sign changes in front of a specific value as written above. Furthermore you may chose the very first element of the vector which has no predecessor and of course no sign change before. It must also be specified how the program should react in this case.

Re: Finding index

I am sorry I did not explain clearly. The main focus of my problem is to determine the index number when it had experienced a sign change in the latest past.  If the problem does not experience a sign change then I have a separate approach for that. In my analysis, I have encountered an array which had several positive numbers and several negative numbers together and I need to find the index number where it last experienced a sign change. That is why I have shown the attached example as, 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". 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. And "0" is considered as a positive number.

Re: Finding index

So your example "array" contains the numbers 1,-1,3,-1,2,-2,-3,-4,1,-5,1 and 4.

What do you mean by: 'work with "-4" ', and how does that relate to the index numbers of "2" and "3"?

Obviously "-4" cannot be an index, but the value -4 occurs as 8th element in the array, that is at index position 7, because by default index starts numbering at 0.

 

You have to be MUCH more clear about what you want.

Luc

Re: Finding index

Thank you for your answer. Let me start over again. I have an array that has a set of positive and negative numbers, not index values. The user will give an input that is within the given array of numbers. In the array, some positive and some negative numbers are placed side by side as attached in the example. The program is only meant to find the index number in the "earliest previous" within the given array where it has an opposite sign. The word "work with" is meant to be "if the input is ". For example, in my array, if the user inputs "-4" the program has to trace the index number of "2", not "3". Similarly, if the user input is "4" it has to trace the index number of "-5", not "-4". The program is not concerned what if there is no "sign" change. My version is Prime 4.0.

Re: Finding index

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