Click here to Skip to main content
14,176,277 members
Rate this:
 
Please Sign up or sign in to vote.
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

return within(&a[0], N - 1, lower, upper,result);
count++; // this will never be executed, as it follows a return statement
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

the compiler probably complain that 'all path do not have a return'
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.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190525.1 | Last Updated 6 Mar 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100