1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
38
Rep Power
2

#### Recursive sum

Write a program that constantly asks the user for an integer. When "exit" is entered, output the sum of all the integers. Store each number in a Vector of Integers. Your code must include a method that recursively sums up all the values in the Vector.

Here is my code:

Code:
``` import java.util.Scanner;
import java.util.Vector;

public class RecursiveInteger
{
static int sum = 0;

public static int sumUp (Vector<Integer> numberStore, int n)
{

if(n == 0)
{
return sum;
}
else
{
return sum + sumUp(numberStore, (n - 1));
}

}

public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
Vector<Integer> numStore = new Vector<Integer>();
String str = "";
int count = 0;

do
{
System.out.println("Enter a number to add to the sum. Enter \"exit\" to end.");
str = keyboard.nextLine();
count++;

}while(!str.contentEquals("exit"));

System.out.println("Sum: " + sumUp(numStore, count));

}

}```
I keep getting "Sum: 0." Any advice on how I can fix this? Thanks.
2. Hi,

your sumUp() method doesn't do anything except repeatedly adding the sum variable (which is and stays 0).

I think you started writing the code too early. First think about what you want to do (draw a diagram, write down examples, maybe write the function in pseudo code). And only after you really know how everything works should you do the Java implementation.

OK, what is the method supposed to do? It should iterate through a vector of integers and add them. So you need to pass both the vector and an index so that the methods knows where it's currently at (you've already done that). The adding can be from left to right or right to left.

And how could the recursion step look like? You could take the value at the current index and add it do the sum of the next elements.

In pseudo code:

Code:
```function sum(numbers, startingAt):
if startingAt = number.length - 1:
return numbers[startingAt]
else
return numbers[startingAt] + sum(numbers, startingAt + 1)```
Applying the function to e. g. the list (3, 10, 12) would look like this:

Code:
```  sum((3, 10, 12), 0)
= 3 + sum((3, 10, 12), 1)
= 3 + 10 + sum((3, 10, 12), 2)
= 3 + 10 + 12```
Last edited by Jacques1; November 11th, 2012 at 03:59 PM.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
38
Rep Power
2

And how could the recursion step look like?

The recursive step will keep calling the function, until the base step is hit. The base step in this problem would be when the user inputs just one integer (index length would be 1.) For example, if the user inputted the number "5" and nothing else, then that would be the sum. So, if the user inputted "1, 2 and then 3," the "3" would be stored in a stack, and then "2" would be stored in a stack, and then "1" would be stored in the stack. Once the compiler sees that the index is down to 1, then all of those integers will be added in the Vector. Am I right about this?

The recursive method should be something like... each integer is stored in a vector, once the index reaches 1, sum up each integer stored in the vector.

Originally Posted by Jacques1
Hi,

your sumUp() method doesn't do anything except repeatedly adding the sum variable (which is and stays 0).

I think you started writing the code too early. First think about what you want to do (draw a diagram, write down examples, maybe write the function in pseudo code). And only after you really know how everything works should you do the Java implementation.

OK, what is the method supposed to do? It should iterate through a vector of integers and add them. So you need to pass both the vector and an index so that the methods knows where it's currently at (you've already done that). The adding can be from left to right or right to left.

And how could the recursion step look like? You could take the value at the current index and add it do the sum of the next elements.

In pseudo code:

Code:
```function sum(numbers, startingAt):
if startingAt = number.length - 1:
return numbers[startingAt]
else
return numbers[startingAt] + sum(numbers, startingAt + 1)```
Applying the function to e. g. the list (3, 10, 12) would look like this:

Code:
```  sum((3, 10, 12), 0)
= 3 + sum((3, 10, 12), 1)
= 3 + 10 + sum((3, 10, 12), 2)
= 3 + 10 + 12```
4. Originally Posted by anonymousme
The recursive step will keep calling the function, until the base step is hit. The base step in this problem would be when the user inputs just one integer (index length would be 1.) For example, if the user inputted the number "5" and nothing else, then that would be the sum. So, if the user inputted "1, 2 and then 3," the "3" would be stored in a stack, and then "2" would be stored in a stack, and then "1" would be stored in the stack. Once the compiler sees that the index is down to 1, then all of those integers will be added in the Vector. Am I right about this?
No, you misunderstand the assignment. Collecting the user input has nothing to do with the recursive method. Those are two completely different steps.

At first you're supposed to collect the integers entered by the user in an integer vector. So you'll need a numStore.add(...) somewhere. I guess you can fix that yourself?

After the user has entered "exit", you're then supposed to sum the elements of the vector. This is a separate problem which has nothing to do with the previous code. I've already written down some hints for that, and I strongly suggest putting away the Java code and actually solving this problem on a piece of paper. Write down what you want to do like I did with the pseudo code. Afterwards it shouldn't be a problem to implement it in Java.

If you know another programming language (JavaScript, Ruby, Python, whatever), it might also be a good idea to play with recursion in that language. Java has a lot of declaration stuff which sometimes makes it hard to see what's actually going on.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
38
Rep Power
2
This is the code I came up with.

Code:
```import java.util.Scanner;
import java.util.Vector;

public class RecursiveSum
{

public static int add_up(Vector<Integer> v, int counter)
{

if (counter == v.size())
{

return 0;
}
else
{
return v.get(counter) + add_up(v, (counter + 1));

}
}

public static void main (String[] args)
{
Vector<Integer> my_list = new Vector <Integer>();
Scanner keyboard = new Scanner (System.in);
String str = "";
int count = 0;

do
{
System.out.println("Enter a number to add to the sum.  Enter \"exit\" to end.");
str = keyboard.nextLine();
if (!str.equals("exit"))
{
int the_num = Integer.parseInt(str);
count++;
}

}while(!str.contentEquals("exit"));

}

}```
What the code is doing is adding up the indexes of the vector, and not the values of those indexes. I have no idea why it is doing this.

Originally Posted by Jacques1
No, you misunderstand the assignment. Collecting the user input has nothing to do with the recursive method. Those are two completely different steps.

At first you're supposed to collect the integers entered by the user in an integer vector. So you'll need a numStore.add(...) somewhere. I guess you can fix that yourself?

After the user has entered "exit", you're then supposed to sum the elements of the vector. This is a separate problem which has nothing to do with the previous code. I've already written down some hints for that, and I strongly suggest putting away the Java code and actually solving this problem on a piece of paper. Write down what you want to do like I did with the pseudo code. Afterwards it shouldn't be a problem to implement it in Java.

If you know another programming language (JavaScript, Ruby, Python, whatever), it might also be a good idea to play with recursion in that language. Java has a lot of declaration stuff which sometimes makes it hard to see what's actually going on.
6. Can you post the contents of the console from when you execute the code that shows the input and output for the program?
Try debugging the code by printing out some of the variables.
For example print out what is in my_list after it is filled.
Last edited by NormR; November 15th, 2012 at 07:36 AM.
7. Take a look at this. Notice what you're adding to the vector.

Code:
```do
{
System.out.println("Enter a number to add to the sum.  Enter \"exit\" to end.");
str = keyboard.nextLine();
if (!str.equals("exit"))
{
int the_num = Integer.parseInt(str);
count++;```
8. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
38
Rep Power
2
That was it! Thanks!

Originally Posted by bullet
Take a look at this. Notice what you're adding to the vector.

Code:
```do
{
System.out.println("Enter a number to add to the sum.  Enter \"exit\" to end.");
str = keyboard.nextLine();
if (!str.equals("exit"))
{
int the_num = Integer.parseInt(str);