3

I have a problem with a function, using recursion. It's a bit difficult and long to explain it as it is, so I will try to write just the essentials and give some pseudocode. Hope it's understandable enough.

Lets say I have an array of numbers - the positive ones should be added to a given number, while if there are negative numbers, their absolute value tells from which position of the array to start adding the next possitive numbers.

Example:

The given number is 5.

array = [1, 2, -3, 5, 7, 8, 9]
         0  1   2  3  4  5  6   <------- positions

So we have: 5(it is given) + 1+2 + (array[from 3th element] = 5+7+8+9) = 5 + 3 + 29 = 37

and I have (lets say that the body of the function have access and changes the variable named number):

number = 5;
sum(array)
{
    for each element from first to last in the array {
        //here i have some other actions, saving some states

        if (element < 0) {
            return sum(array[abs(element) to end])
        }
        number += element
    }
}

Can you please give me some idea or directions how to remove the recursion?

PS: Sorry and please excuse me if the question is too general or not understandable, if it is such I will delete it as soon as possible.

5
  • Ummmm... I don't see where you got 5 + 3 + 29 from...? Commented Jan 14, 2014 at 13:17
  • are you sure its not return number + sum(array[abs(element) to end]) ? Commented Jan 14, 2014 at 13:18
  • No me neither looks like he counted the 5 twice.. , 5 + f([1,2,-3,5,7,8,9] would make sense though Commented Jan 14, 2014 at 13:20
  • If your language supports it you can make sure your function is tail-recursive using an accumulator. Commented Jan 14, 2014 at 13:21
  • "Here I have some other actions, saving some states" - this is the type of scenario where it would probably be better to give an actual example, rather than a vague description. Also, any recursion can (often fairly easily) be converted to iterative by using a stack data structure. Commented Jan 14, 2014 at 13:54

4 Answers 4

3

Can't you just use a straightforward loop?

for (sum=i=0;i<array_length;) {
  if (array[i]<0) i=-array[i];
  else sum += array[i++];
}

Or am I missing something?

Sign up to request clarification or add additional context in comments.

5 Comments

Well he never said what language, and some won't let you change the loop variable?
I guess this will do be the job, but there are some actions in the beginning of the function which I would have to repeat again if a start summing from a new position. Any ideas :S
I don't understand, sorry. What actions would you have to repeat?
for each element from first to last in the array { //here i have some other actions, saving some states
@Faery Can't the other actions go at the top of the loop, before the if() statement?
2

I think this does the trick?

private long Sum(int[] array)
{
    long result = 0;
    int index = 0;
    while (index < array.Length)
    {
        int val = array[index];
        if (val < 0)
        {
            index = Math.Abs(val);
        }
        else
        {
            result += array[index++];
        }

    }
    return result;
}

Comments

1
i=0
a = [1,2,-3,5,7,8,9]
sum = 0
while (i < a.count)
{
  if a[i] >= 0
    sum += a[i]
    i++
  else
    i = abs(a[i])
} 

1 Comment

oops !, what a twit I am
1

Maybe something like that?

sum(array, s) // s is the initial sum value
{
    for i = 0 to last array index
    {
        if (array[i] < 0)
        {
            for j = -array[i] to last array index
            {
                s += array[j]
            }
            return s
        }
        s += array[i]
    }
}

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.