Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Static Recursive Method 1

Status
Not open for further replies.

jeak

Technical User
Oct 9, 2002
16
0
0
US
I am working on the program below. I am trying to implement a static recursive method where indicated that has one parameter for an array of integers, which returns the sum of the elements in the array. I am not sure of the best way to do this. Does anybody have any suggestions? Thanks!

public class SumOfInts
{
public static void main(String[] args)
{
int[] testArray = new int[5];
testArray[0] = 5;
testArray[1] = 8;
testArray[2] = 42;
testArray[3] = 1;
testArray[4] = 19;
System.out.println("The integers in the array are:");
for(int i = 0; i < testArray.length; i++)
System.out.println(&quot;Value at index &quot; + i + &quot; = &quot;
+ testArray);
int sum = sumOfInts(testArray);
System.out.println(&quot;&quot;);
System.out.println(&quot;The sum should be 75&quot;);
System.out.println(&quot;The actual sum returned is &quot; + sum);
}

Static Method here...

}
 
hi

public class Test {

public static void main(String[] args)
{
int[] testArray = new int[5];
testArray[0] = 5;
testArray[1] = 8;
testArray[2] = 42;
testArray[3] = 1;
testArray[4] = 19;
System.out.println(&quot;The integers in the array are:&quot;);
for(int i = 0; i < testArray.length; i++)
System.out.println(&quot;Value at index &quot; + i + &quot; = &quot;
+ testArray);
int sum = sumOfInts(testArray,0);
System.out.println(&quot;&quot;);
System.out.println(&quot;The sum should be 75&quot;);
System.out.println(&quot;The actual sum returned is &quot; + sum);
}

public static int sumOfInts(int[] array, int index) {

int sum = 0;

if( index < array.length )
return array[index] + sumOfInts(array, index+1);
else
return 0;
}
} manu0
 
A couple of alternatives:
Code:
public class SumOfInts {
  public static int sum(int[] array) {
    int val = array[0];
    if(array.length == 1)
      return val;
    int[] shortened = new int[array.length - 1];
    System.arraycopy(array, 1, shortened, 0, array.length - 1);
    return val + sum(shortened);
  }

  public static void main(String[] args) {
    int[] array = new int[] { 5, 8, 42, 1, 19 };
    System.out.println(sum(array));
  }
}
OR:
Code:
public class SumOfInts {
  public static int sum(int[] array) {
    return sum(array, 0);
  }
  public static int sum(int[] array, int pos) {
    if(pos < array.length)
      return array[pos] + sum(array, ++pos);
    return 0;
  }

  public static void main(String[] args) {
    int[] array = new int[] { 5, 8, 42, 1, 19 };
    System.out.println(sum(array));
  }
}
Why must it be recursive?
Cheers, Neil :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top