0

This is something wired happening in my program, hope you can help me I m trying to compare array elements inside a if condition, This works when comparing value is zero.

if((y[5]<0) && (y[4]>0)){
//do something
} 

But when i try to compare the value with 20, it fails, if condition is not working!

if((y[5]<20) && (y[4]>20)){
    //do something
    } 

Since my array contains floating numbers, i tried this too, but it too fails,if condition is not working!

if((y[5]<20.00f) && (y[4]>20.00f)){
        //do something
        } 

Any idea why this happens? It drives me crazy! :( :( :(

9
  • can you post the content of y? Btw: you should remove the nsarray tag because you are not using a NSArray Commented Jun 4, 2012 at 9:48
  • Can you try printing the value of the array, so we can know that it is correct? NSLog is what you need and since you are using a float I believe %f should be required: NSLog(@"%f",y[5]); Commented Jun 4, 2012 at 9:53
  • Array contains data from accelerometer of the ipod, This is a dynamic array, so i need to compare the elements and increment a counter if correct, I have created a global array and assessed it using float *y=[[Shared sharedInstance] input]; .This condition works for zero comparison but not to any numbers, like 20? Why is that? Commented Jun 4, 2012 at 9:53
  • You could also try this (float)y[5]<20 and see if this works Commented Jun 4, 2012 at 9:55
  • @Lefteris - I tried it too, No problem with it, Even i see that accelerometer values satisfying this condition-> y[5]<20.00f && (y[4]>20.00f . But it's not comparing and updating inside the if, Why? Commented Jun 4, 2012 at 9:56

1 Answer 1

2

You should try :

  if(([[y objectAtIndex:5] intValue]<20) && ([[y objectAtIndex:5] intValue]>20))
  {
    //do something
  } 
Sign up to request clarification or add additional context in comments.

2 Comments

Hey bro, i have no problem with the zero comparison, Array contains data from accelerometer of the ipod, This is a dynamic array, so i need to compare the elements and increment a counter if correct, I have created a global array and assessed it using float *y=[[Shared sharedInstance] input]; .This condition works for zero comparison but not to any numbers, like 20? Why is that?
Hi, NSArray and NSMutableArray contains objects. I am not sure why it work with zero comparison, but if you need to compare with array objects, you need to get the object and cast that to int/float/double to compare with any number.

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.