Click here to Skip to main content
15,886,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
create a function to find the number in between using recursion
int within(int a[], int N, int lower, int upper, int result[])
Input:
int a[] = {4, 1, 3, 1, 3, 2};

Output range between 2 to 3:
{3,3,2}, count = 3

What I have tried:

int within(int a[], int N, int lower, int upper, int result[])
{
    int count = 0;
    if (N == 1 && a[0] >= lower && a[0] <= upper) return a[0]; // base case
    for(int i = 0; i < N - 1; i ++){
        if (a[i] >= lower && a[i] <= upper){
            return within(&a[0], N - 1, lower, upper,result);
            count++;
        }
        return count;
    }
}
Posted
Updated 6-Mar-18 3:04am
v2

C++
return within(&a[0], N - 1, lower, upper,result);
count++; // this will never be executed, as it follows a return statement
 
Share this answer
 
the compiler probably complain that 'all path do not have a return'
C++
int within(int a[], int N, int lower, int upper, int result[])
{
    int count = 0;
    if (N == 1 && a[0] >= lower && a[0] <= upper) return a[0]; // base case
    for(int i = 0; i < N - 1; i ++){
        if (a[i] >= lower && a[i] <= upper){
            return within(&a[0], N - 1, lower, upper,result);
            count++;
        }
        return count;
    }
    // because a return is missing here
}

your requirement make no sense to me. I don't see how you can get 4 as output with the sample input.
Your code also make no sense to me. I don't see its logic.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900