Find an array index before and after a certain value

Alright, just like the title says I am trying to find the index of an array's value that is before and after a users chosen value. I know this sounds confusing..so let me break it down.

Let's say I have an array {1,3,5,9,13}
The user inputs 6 as their value.
I am trying to figure out how to find the index of 5 and 9, which are the values directly below and above 6. I know from looking at this array that the indexes are 2 and 3, but I can't figure out how to code something like this. Any ideas?
Last edited on
start with
1
2
3
4
5
int main( )
{

   return 0;
}


and see how far you can get, then come back and post your code and point out what's troubling you and people will give you some feedback!
Here you go. It's not like I haven't done anything!! The bolded area is where I am tied up at. I want to find the index of the values directly below and after the "angle" which is the user input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if(angle >= left[0] && angle <= left[lpos-1]){
            for(int i=0; i < lpos; i++){
                if(angle == left[i]){ // Checking to see if value user entered is in data set
                    cout << "Coefficient of lift is " << right[i] << endl;
                    equals = true;
                }
                else if(i == (lpos-1) &&  equals == false){
                    if(left[i] < angle){
                        lowerAngle = i;
                    }
                    if(left[i] > angle && (lowerAngle + 1) == i){
                        higherAngle = i;
                    }
                }
                
                // Calculating Coefficient if not a value in data set
                coeff = left[higherAngle] + left[lowerAngle];
                cout << "Coefficient of lift is " << coeff << endl;
            }
        }else{
            cout << "The angle you entered is not within the bounds of the data set." << endl;
        }
Last edited on
the second if statement uses lowerAngle + 1, are you 100% sure lowerAngle has been initialized? In other words when you declare it does it look like
 
int lowerAngle;


or

 
int lowerAngle = 0;


Just a thought...
Topic archived. No new replies allowed.