0

All,

I'm writing a recursive function to do the following:

//addbig( ) -- This function is sent an array of integers and the length of the array.  
//It returns the sum of all integers in the array that are larger than 1000.

Somehow my function is not working. It is giving me zero as the output.

long addbig (const int arrInt[],int l)
{
        if (l == 0)
            return 0;
    else if(arrInt[l]>1000)
        return  arrInt[l] + addbig (arrInt,l-1);
    else
        return  addbig (arrInt,l-1);
}

My integer array is:

 int  arrInt[10]={1000,1,1000,2,1000,3,1000,4,1000,5};

Could anybody shed some light as to why this is not working, and help me a bit. haha no pun intended

6
  • 8
    none of the numbers in your array are larger than 1000. Commented Nov 18, 2013 at 17:07
  • @Daboyzuk is absolutely correct. 1000 is equal to 1000, not greater. Try 1001 ;) Commented Nov 18, 2013 at 17:09
  • 1
    In addition, it appears you're using l as both the index of the element you are trying to look at and the number of elements left to look at. As the code is written, you will never look at arrInt[0] Commented Nov 18, 2013 at 17:09
  • 1
    Why does this need to be recursive anyway? Just looping and keeping a sum would do the job...? Commented Nov 18, 2013 at 17:11
  • 2
    And also, it arrInt[l] is not in range of the array Commented Nov 18, 2013 at 17:11

4 Answers 4

4

First, none of the numbers in your test array are larger than 1000. So you would get 0.

Second, you are invoking UB:

long addbig (const int arrInt[],int l)
{
    if (l == 0)
        return 0;
    else if(arrInt[l] > 1000) // PROBLEM!
        return  arrInt[l] + addbig (arrInt,l-1);
    else
        return  addbig (arrInt,l-1);
}

If l is your array length, the first time you call this function will access 1 element beyond the array. What I think you want is:

long addbig (const int arrInt[],int l)
{
    if (l == 0)
        return 0;
    return addbig(arrInt, l - 1) + (arrInt[l - 1] > 1000 ? arrInt[l - 1] : 0);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your base case is wrong. I fixed it below. Note: none of the numbers in arrInt are greater than 1000, so you will always get zero as output.

long addbig (const int arrInt[],int l)
{
        if (l <= 0)
            return 0;
    else if(arrInt[l-1]>1000)
        return  arrInt[l-1] + addbig (arrInt,l-1);
    else
        return  addbig (arrInt,l-1);
}

3 Comments

This will still invoke UB on the first call.
This should not be the accepted answer as written. The else if (arrInt[l] > 1000) condition will invoke UB.
Wrong. What if l=0 ? arrInt[-1] is not defined.
0
long addbig( const int a[], size_t n )
{
   const int LIMIT = 1000;
   return ( n == 0 ? 0 : a[0] > LIMIT + addbig( a + 1, n - 1 ) );
}

2 Comments

This would just count the number of elements greater than the limit, not compute their sum.
@ Zac Howland you are right. I did not read the post carefully.:)
0

This can help you :D

#include <iostream>

using namespace std;

long addbig(const int arrInt[], int l)
{
    if (l == 0)
        return 0;
    else if (arrInt[l - 1] > 1000)
        return arrInt[l - 1] + addbig(arrInt, l - 1);
    else
        return addbig(arrInt, l - 1);
}

int main()
{
    int  arrInt[10] = {1001, 1, 2000, 2, 1000, 3, 1000, 4, 1000, 5};

    cout << addbig(arrInt, 10) << endl;

    return 0;
}

Comments

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.