Results 1 to 11 of 11
- 10-27-2011, 04:08 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
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 04:41 AM.
-
Re: looking for recursive help using Void method
You've not followed the directions, specifically when size is > 1.
- 10-27-2011, 04:41 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
-
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.
- 10-27-2011, 05:39 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
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.
- 10-27-2011, 08:21 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: looking for recursive help using Void method
Hurray! Spoonfeeding! (this code snippet prints the array backwards).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-27-2011, 04:50 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
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)
- 10-27-2011, 05:02 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
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?
- 10-27-2011, 05:12 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: looking for recursive help using Void method
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-27-2011, 06:43 PM #10
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: looking for recursive help using Void method
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.
- 10-27-2011, 06:48 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
accessing variables in a method that returns a void
By mochajava in forum New To JavaReplies: 2Last Post: 02-12-2011, 08:00 PM -
How to test a void method in Junit?
By jRookie in forum New To JavaReplies: 7Last Post: 09-01-2010, 07:47 AM -
calling a public void method from a class button
By supa_kali_frajilistik in forum AWT / SwingReplies: 4Last Post: 05-23-2008, 01:05 PM -
calling a public void method from a class button
By supa_kali_frajilistik in forum AWT / SwingReplies: 1Last Post: 05-21-2008, 05:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks