Results 1 to 5 of 5

Thread: Sum of Array

  1. #1
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Question 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Default

    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,n-1)+a[n-1]);
    ^
    C:\ArraySum.java:17: ')' expected
    return array((a,n-1)+a[n-1]);
    ^

  4. #4
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Question 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; 01-03-2009 at 09:29 AM.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Mika View Post
    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,n-1)+a[n-1]);
    ^
    C:\ArraySum.java:17: ')' expected
    return array((a,n-1)+a[n-1]);
    ^
    Here the initialization of an array is completely wrong.

Similar Threads

  1. new array ?
    By seandingobat in forum New To Java
    Replies: 7
    Last Post: 10-03-2008, 07:31 PM
  2. 3 by 3 array
    By fromme87 in forum New To Java
    Replies: 5
    Last Post: 10-02-2008, 08:34 AM
  3. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM
  4. can anyone help... 2d Array
    By Mark1989 in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 08:59 PM
  5. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 02:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •