programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Sum of array by recursion

Mark Nasr
Ranch Hand
Posts: 32
Hello guys, this is my first post here and one of the questions of an assignment for my university is to add the sum of an array with a recursion, but I don't understand how to use recursion. I just understand that it calls back the method. I am nearly done with the code I just need help with the recursion part and a small explanation thank you.

Stefan Evans
Bartender
Posts: 1837
10
Hi Welcome to the Ranch!

Recursion at its most basic is "split a problem up into smaller pieces"
In the case of a list a common split is "head" and "tail" - head being the item at the front of the list, and the tail being the rest.

In order to be able to do this, you need to provide more information to your method. Not only the array of numbers to sum, but an indication of where to start summing from.

Assume we have 10 numbers to sum (0-9)
Sum of all numbers in list from 0..9
= Item 0 + sum of numbers from 1..9
And the sum of numbers from 1..9 = Item 1 + sum of numbers from 2..9

So if you had a method with signature: sum(int[] arr, int startingAt) you could do this.

Mark Nasr
Ranch Hand
Posts: 32
So if I send to sum a counter that is initially set to 0, how do I write the code to keep executing sum again like in the code below?

Jude Niroshan
Ranch Hand
Posts: 132
5
• 1
Hi Mark,
Welcome to Ranch!
If you write a method to use for recursion, don't forget that method body is a loop body
I'll just give you an idea about recursion. Think you have a note book and pen with you. So, i am going to come to you and ask, 'could you please add all my marks for this semester?' Then i'll give you all my marks at a once. In that case you will need to write them all in your first paper as i'm speaking too fast. Assume that you are not good in maths(just kidding) and you only capable of add two digits at a time. So, you start my total marks is 0. (Think if i never attend to any of my exams).
You tear the paper what you wrote down all my marks, the give it to me as you can't hold note book, pen and paper all together.
Now, you turn to new page and write 0, as you thought my total. Then first you check whether i have attended for the exams. If so, then you write down the first mark on your new page in the note book. So, you add those two numbers.(cos you are a expert in adding two numbers ) Then again you calculate that total. Turn to a new page and write down that total you just calculated. Then again you ask me the paper what i have in my hand.
Again you check except the first mark you already taken, is there any other subjects i have attended. If that so, you will write down my second subject's mark in the current page(page what you have wrote the total). Then you'll add them and write down in a brand new page. Again you ask me the paper what i have and check whether i have a 3rd subject. So you found that i don't have anymore subjects. So, you just show me the last page what you wrote the total saying, man, see; i have calculated your total marks for this semester. How awesome is that?

So, you need to change what you written so like this.

I have written it in short form. That is the same thing what i have commented. Learn anything from those two lines, if you have to.
When you call this recursion function inside your main() :

Minal Silimkar-Urankar
Ranch Hand
Posts: 136
Hi Mark,

Your question is how to implement recursive function. Recursion is simply function calls itself. Below is sample code.

You need small change in your code is as follows:

Mark Nasr
Ranch Hand
Posts: 32
Thank you very much for your answers guys. I tried to correct my code with your answers but I get a strange outcome.
If I write this code, I only get the first value of the array for whatever size it is.

But if I write this code, I get the last value of the array for also whatever size the array.

Thank you again for your continuous help.

Jude Niroshan
Ranch Hand
Posts: 132
5
Please read my answer again. There i have took that variable 'sumOfNumbers' outside the the method(method sum()). I have declared it inside the main method. If you declare that variable inside the method, it will create a new variable (named as 'sumOfNumbers') for each time you call the method 'sum()'. So, it will only return the last value it has. That means, it will not give the expected answer.

Liutauras Vilda
Sheriff
Posts: 4928
334
Mark Nasr,

You wrote too much code without full understanding what that code does. I'm afraid you need to start over (which is not bad thing as you could imagine).
Since your thread is named "Sum of array by recursion", your task is to build fully functioning method and do not think about anything else outside it at the moment.

Some theory about what is recursion itself:
Recursive function is a function that calls itself.

A recursive function normally has two parts:
1. Stopping condition. Processing the case where the recursive function does not call itself.
2. Recursive step. Processing the case where the recursive function calls itself.

Important part to understand:
The recursive call must have different parameter values than those of the calling function because otherwise the program gets into an in finite loop (stopping condition mentioned above wouldn't be satisfied ever).

Liutauras Vilda
Sheriff
Posts: 4928
334
• 1
Observations about the code you wrote:
1. Line 2. Each time recursive function is called, you initialize sumOfNumbers to 0. You have to think, how to avoid that.
2. Line 5. Do not omit braces even if block has only an empty statement. It improves readability, and convince reader that you did what you meant to do.
Line 9. In this line suppose to be only a recursive call (without initialization of sumOfNumbers) with different parameters (remember?)
Line 8 + Line 9 would look like:

Mark Nasr
Ranch Hand
Posts: 32
Thank you very much the program worked, I will change the topic to resolved now

Liutauras Vilda
Sheriff
Posts: 4928
334
Mark Nasr wrote:Thank you very much the program worked, I will change the topic to resolved now

Would you mind to post your full working program for the future readers, as it could be a helpful for someone.