Results 1 to 6 of 6

Thread: Recursion

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

    Question Recursion

    Hi
    I have my problem regarding my program. I have no installer in java that's why i cant make my program run to identify if there is an error. this program is about summing the all elements of array asking from the user using recursion. Can you help me to solve this?
    Here's my program I create

    import javax.swing.*;
    public class ArraySum {
    public static void main(String []args)throws Exception {

    int sum;
    for(int y=0;y<=n;y++) {
    int x=Integer.parseInt(JOptionPane.showInputDialog("En ter a number:"));
    int z[]=array([x]);
    sum+=z[y];
    System.out.println(z[x]);
    }
    }

    public static array(int a, int n) {
    if(n==1){
    }else {
    return array((a,n-1)+a[n-1]);
    }
    }
    }
    Last edited by Mika; 01-03-2009 at 05:07 AM.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Lightbulb try this.

    Quote Originally Posted by Mika View Post
    Hi
    I have my problem regarding my program.
    yep.
    Quote Originally Posted by Mika View Post
    I have no installer in java that's why i cant make my program run to identify if there is an error.
    The code is very beginner, be patient while we find what the code is supposed to be teaching.
    Quote Originally Posted by Mika View Post
    This program is about summing the all elements of array asking from the user using recursion. Can you help me to solve this?
    Help, but not do all the work.
    Java Code:
    import javax.swing.*;
    public class ArraySum 
    {
        public static void main(String []args)
        {
            try
            {
                int x = Integer.parseInt(JOptionPane.showInputDialog("Enter 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());
            }
        }
    }
    { message edit, the way I did it is not recursion ... we need to get the code straightened first }
    Last edited by Nicholas Jordan; 12-27-2008 at 05:42 PM.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Intall Java

    It only takes a few momets to install the JDK. Install it.
    Some observations about the posted code:
    • Rename the array method to some meaingful, like sumArray or myArray or something else.
    • The array creation is wrong. Review the following link about arrays (I find this strange because you have posted previous code with correct array syntax):
      Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    • It would appear that you are trying to create an array and call the method at the same time... no can do. These should be two diferent code statements
    • You can't print an array that way. You have to print each element in the array using a loop (for example, a for loop)


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    Can you help me regarding recursion?

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    int nFactorial(int inwithint){while(inwithint > 0x00000000){nFactorial(inwithint);}}

    Simple, so what's your problem?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Arrays;
    
    public class RecursionSum {
        public static void main(String[] args) {
            int[] vals = { 6, 3, 5, 9 }; // 23
            int sum = addArrayValues(vals, 0, 0);
            System.out.println("sum of " + Arrays.toString(vals) +
                               " = " + sum);
        }
    
        private static int addArrayValues(int[] a, int index, int sum) {
            if(index == a.length-1) {
                return sum + a[a.length-1];
            } else {
                return addArrayValues(a, index+1, sum+a[index]);
            }
        }
    }

Similar Threads

  1. Recursion
    By Zosden in forum Algorithms
    Replies: 4
    Last Post: 05-05-2008, 06:49 AM
  2. help with recursion
    By Nari in forum New To Java
    Replies: 15
    Last Post: 04-24-2008, 10:13 AM
  3. Help With Recursion
    By andrew777 in forum New To Java
    Replies: 1
    Last Post: 03-29-2008, 01:51 PM
  4. recursion
    By kdeighan in forum New To Java
    Replies: 3
    Last Post: 01-25-2008, 10:48 PM
  5. Recursion
    By bozovilla in forum Advanced Java
    Replies: 3
    Last Post: 01-07-2008, 05:53 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
  •