[recursion]- can someone look at this program?

I need to add 10 integers in an array using recursion, i am getting the error:

C:\MCS142\Recursion.java:5: '.class' expected

this.addArray(array[], first, last);

publicclass Recursion

{

publicstaticvoid main(String[] args)

{

this.addArray(array[], first, last);

}

int[] array ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int first = 0;

int last = 9;

int sum = 0;

publicvoid addArray(int array[],int first,int last)

{

if (first < last)

sum = sum + array[first];

addArray(array, first + 1, last);

System.out.println(sum);

}

}

[1355 byte] By [LuckY07a] at [2007-11-26 20:00:27]
# 1

> I need to add 10 integers in an array using

> recursion, i am getting the error:

>

> C:\MCS142\Recursion.java:5: '.class' expected

> this.addArray(array[], first, last);

You can't call the addArray(..) method on a method, which is what you're telling it to do with the 'this' keyword; this error is telling you that you must call the addArray(...) method on an object, such as a class. Try removing 'this'.

Djaunla at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 2
You error isn't related to recursion, it's how to pass an array to a method: don't include [ ]:addArray(array[], first, last); //noaddArray(array, first, last); //yes
DrLaszloJamfa at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 3
isnt that what recursion is, calling a method inside another method? How would I fix this program? tks.
LuckY07a at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 4
see reply #2, you haven't declared any int[] array.
Ruly-o_Oa at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 5

I changed the code to the following, but am now getting this error:

C:\MCS142\Recursion.java:6: non-static variable array cannot be referenced from a static context

a.addArray(array, first, last);

^

C:\MCS142\Recursion.java:6: non-static variable first cannot be referenced from a static context

a.addArray(array, first, last);

^

C:\MCS142\Recursion.java:6: non-static variable last cannot be referenced from a static context

a.addArray(array, first, last);

^

3 errors

public class Recursion

{

public static void main(String[] args)

{

Recursion a = new Recursion();

a.addArray(array, first, last);

}

int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int first = 0;

int last = 9;

int sum = 0;

public void addArray(int array[], int first, int last)

{

if (first < last)

sum = sum + array[first];

addArray(array, first + 1, last);

System.out.println(sum);

}

}

LuckY07a at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 6
> isnt that what recursion is, calling a method inside> another method? How would I fix this program? tks.You could start by following the suggestions in Reply 1 and Reply 2.
DrClapa at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 7

I give up:

public class Recursion {

public static void main(String[] args) {

int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int sum = addArray(array);

System.out.println("answer = " + sum);

}

public static int addArray(int[] array) {

return add(array, 0, array.length);

}

public static int add(int[] array, int lo, int hi) {

if (lo < hi)

return array[lo] + add(array, lo+1, hi);

else

return 0;

}

}

DrLaszloJamfa at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 8

> isnt that what recursion is, calling a method inside

> another method? How would I fix this program? tks.

You can call a method inside a method, but you cannot call a method on another method. With the 'this' keyword, you're basically saying:

main.addArray(...)

Your main method cannot have an array added to it (or a method called on it).

Djaunla at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 9
thank you very much for that code. I was wondering if you could delete the method 'addArray'? and do the adding in 'add' and call add from the driver?
LuckY07a at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 10
Feel free to edit the code. I think that having a top-level method thatjust takes an array is a nice touch, though.
DrLaszloJamfa at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 11

I tried to simplify the code, and I got the following message, even though I have a return statement:

C:\MCS142\Recursion.java:14: missing return statement

}

public class Recursion

{

public static void main(String[] args)

{

int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int sum = addArray(array, 0, array.length);

System.out.println(sum);

}

public static int addArray(int array[], int first, int last)

{

if (first < last)

return array[first] + addArray(array, first+1, last);

}

}

LuckY07a at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 12
What value does addArray return if "first < last" is false?
DrLaszloJamfa at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 13

I figured it out. I guess if you have a RETURN statement in an IF loop, you need an ELSE statement with a return. Thanks for the help, here is my completed code:

public class Recursion

{

public static void main(String[] args)

{

int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int sum = addArray(array, 0, array.length);

System.out.println("answer = " + sum);

}

public static int addArray(int array[], int first, int last)

{

if (first < last)

return array[first] + addArray(array, first+1, last);

else

return 0;

}

}

LuckY07a at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 14
So what you did is get rid of my lovely top-level method. *Sob*
DrLaszloJamfa at 2007-7-9 22:57:58 > top of Java-index,Java Essentials,Java Programming...
# 15

I am trying to go backwards, and am getting an out of bounds exception.

Here is the code:

public class Recursion2

{

public static void main(String[] args)

{

int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int sum = addArray(array, 0, array.length);

System.out.println("answer = " + sum);

}

public static int addArray(int array[], int first, int last)

{

if (first <= last)

return array[last] + addArray(array, first, last - 1);

else

return 0;

}

}

LuckY07a at 2007-7-21 17:48:12 > top of Java-index,Java Essentials,Java Programming...
# 16
Recall that addArray(int array[], int first, int last) adds up the values of array[j], where first <= j < last.
DrLaszloJamfa at 2007-7-21 17:48:12 > top of Java-index,Java Essentials,Java Programming...
# 17
im kinda confused. Where am I going wrong in my code? tks.
LuckY07a at 2007-7-21 17:48:12 > top of Java-index,Java Essentials,Java Programming...
# 18

What's going wrong is that you are not grasping the significance of first and last.

Again, addArray(int array[], int first, int last) adds up the values of array[j], where first <= j < last.

As an example, suppose I define:

int[] v = {7,8,9};

What is the value of v.length? What is the offset of value 9 in this array?

DrLaszloJamfa at 2007-7-21 17:48:12 > top of Java-index,Java Essentials,Java Programming...
# 19
v.length would be 3. I fixed the code by removing array.length and replacing it with array.length - 1. I guess I just am having a tough time seeing how it runs, I will look at it closer.. tks.
LuckY07a at 2007-7-21 17:48:12 > top of Java-index,Java Essentials,Java Programming...
# 20
Note: There is no such thing as an if loop.
floundera at 2007-7-21 17:48:12 > top of Java-index,Java Essentials,Java Programming...
# 21
> Note: There is no such thing as an if loop.I missed that.
DrLaszloJamfa at 2007-7-21 17:48:12 > top of Java-index,Java Essentials,Java Programming...
# 22

I am trying to divide the array in half, and then add the sum of integers recursively.. It looks like it should work, however I am getting a .class error:

C:\MCS142\Recursion3.java:24: '.class' expected

int sum = array[first] + addArray(array, first + 1, mid);

^

C:\MCS142\Recursion3.java:24: not a statement

int sum = array[first] + addArray(array, first + 1, mid);

^

C:\MCS142\Recursion3.java:30: '.class' expected

int sum2 = array[mid2] + addArray(array, mid2+1, last);

^

C:\MCS142\Recursion3.java:30: not a statement

int sum2 = array[mid2] + addArray(array, mid2+1, last);

/**

Program that demonstrates recursion using an array.

@task = to add up all integers in the array using 3 types of recursion.

*/

public class Recursion3

{

public static void main(String[] args)

{

int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int total = addArray(array, 0, array.length);

System.out.println("answer = " + total);

}

public static int addArray(int array[], int first, int last)

{

int mid = (first + last)/2;

addArray(array, first, mid);

if (first < mid)

int sum = array[first] + addArray(array, first + 1, mid);

addArray(array, mid + 1, last);

int mid2 = mid + 1;

if (mid2 < last)

int sum2 = array[mid2] + addArray(array, mid2+1, last);

return sum + sum2;

}

}

Message was edited by:

LuckY07

LuckY07a at 2007-7-21 17:48:12 > top of Java-index,Java Essentials,Java Programming...