Results 1 to 5 of 5
Thread: Sum of Array
 01032009, 09:43 AM #1Member
 Join Date
 Dec 2008
 Location
 Davao Oriental
 Posts
 29
 Rep Power
 0
Sum of Array
Can you help me regarding recursion in java?
This is Mr. Nicholas program but he did not use recursion in java in this program.Can you give the program using recursion in java.This is about summing the elements within an array.
import javax.swing.*;
public class ArraySum
{
public static void main(String []args)
{
try
{
int x = Integer.parseInt(JOptionPane.showInputDialog("Ente r a number:"));
if( x > 0)
int y = x;
int z = 0;
// allocate an array for integers
int array[] = new int[x];
//
while(y > 0)
{
z += x;
array[y] = z;
}
int index = 0;
final int limit = array.length;
while(++index < limit)
{
System.out.println(array[index]);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
your help is very well appreciated.Thank You
 01032009, 09:45 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
 01032009, 10:13 AM #3Member
 Join Date
 Dec 2008
 Location
 Davao Oriental
 Posts
 29
 Rep Power
 0
I have her my progarm but i dont know how to implement it coorectly. can you help me. it has 5 errors, her the statement
C:\ArraySum.java:8: illegal start of expression
int z[]=array([x]);
^
C:\ArraySum.java:8: ')' expected
int z[]=array([x]);
^
C:\ArraySum.java:14: invalid method declaration; return type required
public static array(int a, int n) {
^
C:\ArraySum.java:17: ')' expected
return array((a,n1)+a[n1]);
^
C:\ArraySum.java:17: ')' expected
return array((a,n1)+a[n1]);
^
 01032009, 10:27 AM #4Member
 Join Date
 Dec 2008
 Location
 Davao Oriental
 Posts
 29
 Rep Power
 0
sum of array
Here's another program i make but it has 1 error in my main method,what should i do?what variable should i declare to receive the result from the recursive function.Can you help me?pls..
import javax.swing.*;
public class ArraySum {
public static void main(String []args)throws Exception {
for(int y=0;y<=10;y++) {
int x=Integer.parseInt(JOptionPane.showInputDialog("En ter a number:"));
int z=result();
System.out.println(z);
}
}
int sum( int arr[], int size )
{
int result;
if ( size == 0 ) // base case
result = 0;
else
{
// recursive call
int smallResult = sum( arr, size  1 );
// use solution of recursive call to solve this problem
result = smallResult + arr[ size  1 ];
}
return result;
}
}Last edited by Mika; 01032009 at 10:29 AM.
 01032009, 10:38 AM #5
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Similar Threads

new array ?
By seandingobat in forum New To JavaReplies: 7Last Post: 10032008, 07:31 PM 
3 by 3 array
By fromme87 in forum New To JavaReplies: 5Last Post: 10022008, 08:34 AM 
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04232008, 08:08 PM 
can anyone help... 2d Array
By Mark1989 in forum New To JavaReplies: 2Last Post: 03122008, 09:59 PM 
Would appreciate your help with 2d Array..
By cloudkicker in forum New To JavaReplies: 1Last Post: 02112008, 03:34 PM
Bookmarks