Java and recursion

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2006
Posts: 2
Reputation: 3x108th is an unknown quantity at this point 
Solved Threads: 0
3x108th 3x108th is offline Offline
Newbie Poster

Java and recursion

 
0
  #1
Feb 11th, 2006
i am working on a program where the user enters a number and the computer will generate N number of characters and print them in all combonations. For example:

enter number: 3

aaa aab aac aba abb abc aca acb acc
baa bab bac bba bbb bbc bca bcb bcc
caa cab cac cba cbb cbc cca ccb ccc

I have made the program build the array and randomize the characters however i dont know how to use recursion to print all the combos. Here is what i have so far:

import cs1.Keyboard;
import java.util.Random;

public class Letter
{

public static void main(String[] args)
{
System.out.println("How many characters do you want");
int number = Keyboard.readInt();

char[] list = new char[number];

Random generator = new Random();

for(int index = 0; index < list.length; index++)
{
int temp = generator.nextInt(26) + 65;
list[index] = (char) temp;
}

//check if all characters are different
for(int index = 0; index < list.length-1; index++)
{
if(list[index] == (list[index+1]))
{
int temp = generator.nextInt(26) + 65;
list[index+1] = (char) temp;
index=0;
}
}
I believe recursion would be the best solution but i dont know how to use it to print the answer. Any help would be great. thanx
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Java and recursion

 
0
  #2
Feb 11th, 2006
This is the basic principle...

  1. char stuff[]="abc";
  2.  
  3. for(int i=0; i<3; i++)
  4. {
  5. for(int j=0; j<3; j++)
  6. {
  7. for(int k=0; k<3; k++)
  8. {
  9. print stuff[i];
  10. print stuff[j];
  11. print stuff[k];
  12. print newline
  13. }
  14.  
  15. }
  16. }

From there you should be able to devise a general model and see where the recursion comes into play.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2
Reputation: 3x108th is an unknown quantity at this point 
Solved Threads: 0
3x108th 3x108th is offline Offline
Newbie Poster

Re: Java and recursion

 
0
  #3
Feb 11th, 2006
I understand that. I know thats how it works, but i dont know how to get the for loops to repeat N number of times. The program i am making has to work for any number N.
I'm new at the topic of recursion so im lost in trying to use it. But thanks for the help so for.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Java and recursion

 
0
  #4
Feb 12th, 2006
so parameterise the whole thing to take a number n instead of using a fixed number of iterations...
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Java and recursion

 
0
  #5
Feb 24th, 2006
try the following:

  1. public void printLoop(int index; char[] list; char[] string) {
  2. if (index == list.length) {
  3. System.out.print(new String(string) + " ");
  4. return;
  5. }
  6.  
  7. for (int i = 0; i < list.length; i++) {
  8. string[index] = list[i];
  9. printLoop(index + 1, list, string);
  10. }
  11. }
  12.  
  13. char[] string = new char[list.length];
  14.  
  15. for (int i = 0; i < list.length; i++) {
  16. string[0] = list[i];
  17. printLoop(1, list, string);
  18. System.out.println("");
  19. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3685 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC