0

Below is my code:

#include <iostream>
using namespace std;
int* func(int [], int, int, int);

int main() {
    int a[] = {1, 2, 3, 4};
    int *b;
    int target = 2;
    int min = 0, max = 3;
    b = func(a, min, max, target);
    cout << a[*(b + 0)]<< " " << a[*(b+1)] << " outside";
    return 0;
}

int* func(int a[], int min, int max, int target) {
    int middle = (min + max)>>1;
    int b[2];
    if (a[(min+max)>>1] == target){
        b[0] = middle;
        b[1] = middle;
        return b;
    } 
    if (a[(min+max)>>1] > target) {
        return func(a, min, (min+max)>>1, target);
    }

    if (a[(min+max)>>1] < target) {
        return func(a, (min+max)>>1, max, target);
    }

}

This code is returning a run time error. I could not find where it is going wrong.

if i comment out the other if statements that do not have the (==), it runs fine.

Im not sure what is going wrong with my recursive call. Please let me know.

user1455116
  • 2,034
  • 4
  • 24
  • 48

0 Answers0