user3282760
user3282760

Reputation: 13

recursion, computing sum of positive integers

so, my whole point is to print out the sum of positive numbers, and i have it adding and printing just fine, the only issue it is also adding the negative numbers.

any ideas on why this could be? i just want it to add the positive numbers.

public static double computeSumPositive(double[]numbers, int count) 
{
double total=0;
{
if(count>0) 
        {
            total = numbers[count-1] + computeSumPositive(numbers, count -1); 
            return total;
        }
        else 
            return total;
        } 
}

Upvotes: 0

Views: 3253

Answers (3)

Jason Yeung
Jason Yeung

Reputation: 81

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int sumPosRecur(int array[], int len) 
{
    if(array)
    {
        /* len greater than 0 */
        if(len > 0)
        {
            if(len == 1)
            {
                /* return a positive int */
                if(array[0] > 0)
                {
                    return array[0];
                }

                else return 0;/* return 0 if negative int */
            }       

            if(len > 1)
            {
                if(array[len-1] > 0)
                {
                    return array[len - 1] + sumPosRecur(array, len - 1);/* return sum of all positive ints by recursively calling */
                }

                else return sumPosRecur(array, len - 1);/* go next round of recursively call if isn't positive int */
            }
        }/* if(len > 0) */

        /* return 0 */
        if (len == 0)
        {
            return 0;
        }

        /* return -1 */
        if (len < 0)
        {
            return -1;
        }

    }/* if(array) */

    /* return -2 */
    else return -2;
}

int main() 
{
    int array[] = {1, 2, 3, 4, 5};
    int len = 5;
    int result;

    result = sumPosRecur(array, len);
    printf("%d\n", result);
    return 0;
}

Upvotes: 0

Alvin Baena
Alvin Baena

Reputation: 903

Because you are not checking if the number in question is negative, So if I understand correcly you want to ignore all negative numbers in the numbers array, like this:

public static double computeSumPositive(double[] numbers, int count) {
    double total = 0;
    if (count > 0) {
        if (numbers[count - 1] < 0) {
            total = numbers[count - 1] + computeSumPositive(numbers, count - 1);
        } else {
            total = computeSumPositive(numbers, count - 1);
        }
    }
    return total;
}

You also dont need to return inside the if jus return the value every time the code loops.

Upvotes: 0

acarlon
acarlon

Reputation: 17272

You need to check the value you are adding. Something like:

    public static double computeSumPositive( double[] numbers, int count )
    {
        double total = 0;
        {
            if( count > 0 )
            {
                double val = (numbers[count - 1] > 0)?numbers[count - 1]:0;
                total = val + computeSumPositive( numbers, count - 1 );
                return total;
            }
            else
                return total;
        }
    }

Upvotes: 1

Related Questions