Results 1 to 11 of 11
  1. #1
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default looking for recursive help using Void method

    What do see that is wrong? Can you help me with my else statement?

    • Write a recursive method with method header void printArray(int[] a, int size) that prints the first size elements of array a on separate lines.

    printArray(…) should check to make sure size is a positive number less than or equal to the length of array a – if not, it should print out an error message and use System.exit(0) to exit the program.
    If size is 0, printArray(…) should return without printing anything.
    If size is 1, printArray(…) should print a[0] and return.
    If size is > 1, printArray(…) should call printArray(a, size-1), then print a[size-1], and then return.

    import java.util.*;
    public class extra {
    public static void printArray(int[] a, int size)
    {
    if (size<0)
    System.out.println("Error");
    if (size==0)
    System.out.println();
    if (size==1)
    System.out.println ("0");
    else
    {
    ( factorial(n-1)); //Wrong, need help with this
    }

    }
    Last edited by tripline; 10-27-2011 at 05:41 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: looking for recursive help using Void method

    You've not followed the directions, specifically when size is > 1.

  3. #3
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: looking for recursive help using Void method

    Quote Originally Posted by Fubarable View Post
    You've not followed the directions, specifically when size is > 1.
    Edited.
    But that is where the else statement comes in. I don't know how to associate recursive to a void method, I keep getting errors on blue J.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: looking for recursive help using Void method

    OK, now you're calling some method that is not even defined in your code above. The key question to ask is this "what method must be called recursively"? The answer: the method you are currently in. So call that method, printArray, not some method pulled out of left field, factoral. Recursive means to call a method from within that same method, either directly or indirectly. Factoralization is just one example where recursion works well, but that's not what you're doing here.

  5. #5
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: looking for recursive help using Void method

    I just learned this today so please bear with me and my vocab.
    factorial means (n-1), (n-2)... and that is what the problem wanted me to do as a recursive right?
    How do i call printArray(a, size-1)? Something needs to be in front of that statement but im not sure what.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: looking for recursive help using Void method

    Hurray! Spoonfeeding! (this code snippet prints the array backwards).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: looking for recursive help using Void method

    Can you tell me the different between?When to use brackets, when to use paras?
    (a[size - 1])
    printArray(a, size - 1)

  8. #8
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: looking for recursive help using Void method

    helpline - I see what you did there, but I am trying to find a way to connect both methods together.

    public static void main(String[] args) {
    int b[] = {3, 4, 5, 6};
    int size = b.length;
    printArray(a, size-1); //a is suppose to be from the public static void printArray(int[] a, int size)

    can you help me with this?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: looking for recursive help using Void method

    Quote Originally Posted by tripline View Post
    Can you tell me the different between?When to use brackets, when to use paras?
    (a[size - 1])
    printArray(a, size - 1)
    Those square brackets are the array indexing operator: array[index] is the index-th element of the array. Those parentheses bracket the parameter list for methods. Read your textbook for a complete explanation of it all.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: looking for recursive help using Void method

    Quote Originally Posted by JosAH View Post
    Hurray! Spoonfeeding! (this code snippet prints the array backwards).

    kind regards,

    Jos
    Actually, no, it prints it forwards, because the output is done AFTER the call to printArray. So the recursive definition is like this:

    To output n elements:
    if n=0, finish
    otherwise, output n-1 elements, followed by the final element.

    Well, except that, for some reason, the assignment specs gave an unnecessary special case for when n=1.


    @OP: As Fubarable said, factorial has nothing to do with the problem you are trying to solve, it is simply one example that is frequently used to demonstrate recursion.

    Your printArray method is meant to be recursive, which means that it should call itself.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: looking for recursive help using Void method

    Quote Originally Posted by DiamondSoul View Post
    Actually, no, it prints it forwards, because the output is done AFTER the call to printArray.
    Check again ... (or just run it)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. accessing variables in a method that returns a void
    By mochajava in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 09:00 PM
  2. How to test a void method in Junit?
    By jRookie in forum New To Java
    Replies: 7
    Last Post: 09-01-2010, 08:47 AM
  3. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 4
    Last Post: 05-23-2008, 02:05 PM
  4. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 1
    Last Post: 05-21-2008, 06:40 AM

Posting Permissions

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