I got a different resolt than i was supposed to

  • Thread starter Thread starter transgalactic
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion centers on a Java method for generating subsets of a string, specifically using recursion. The user, identified as "donaldth smithts," attempted to generate subsets of length 2 from the string "abcd" but received unexpected results. The output included combinations like "abcd21" instead of the desired subsets. The analysis indicates that the logic in the recursive function is flawed, as it does not correctly handle the generation of subsets of the specified length.

PREREQUISITES
  • Understanding of Java programming and recursion
  • Familiarity with string manipulation in Java
  • Basic knowledge of algorithm design for subset generation
  • Experience with IDEs like NetBeans for Java development
NEXT STEPS
  • Review Java recursion techniques for generating subsets
  • Learn about Java String methods and their applications
  • Explore algorithm optimization for subset generation
  • Practice debugging recursive functions in Java using NetBeans
USEFUL FOR

Java developers, computer science students, and anyone interested in algorithm design and recursion in programming.

transgalactic
Messages
1,386
Reaction score
0
i tried to follow this recurstion
but instead of getting the resolt i expected i got
some thing really different

http://img185.imageshack.us/my.php?image=img8290nf7.jpg

Code:
// print all subsets of 1...n 
Public static void subsets(int n, String andAlso)
{
      if (n==0)
	  { 
          System.out.println(andAlso);
      }
      else
	 {
          subsets(n-1,andAlso + n);
          subsets(n-1,andAlso);
      }
}

i entered (2,abcd)
i was supposed to get


cd
bd
bc
ad
ac
ab

istead i got some other answer using this diagram
can you tell me where i got ot wrong??

[ February 14, 2008: Message edited by: donaldth smithts ]
 
Technology news on Phys.org
How is it supposed to output that?
cd
bd
bc
ad
ac
ab
Your diagram itself demonstrates why
abcd21
abcd2
abcd1
abcd
is the output ... We don't understand what its purpose is.
 
Your program is entertainingly mind boggling XD if it helps i think this is the run through of what it actually does:

0: subsets(2, "abcd") Pass in n = 2 andAlso = "abcd"
1.1: subsets(1, "abcd2") 1st line of else statement n-1 = 1 andAlso + n = "abcd2"
1.1.1: subsets(0, "abcd21") 1st line of else statement n-1 = 0 andAlso + n = "abcd21"
1.1.1.0: output = "abcd21" If statement so output
1.1.2: subsets(0, "abcd2") 2nd line of else statement n-1 = 0 andAlso = "abcd2"
1.1.2.0: output = "abcd2" If statement so output
1.2: subsets(1, "abcd") 2nd line of else statement n-1 = 1 andAlso = "abcd"
1.2.1: subsets(0, "abcd1") 1st line of else statement n-1 = 0 andAlso + n = "abcd1"
1.2.1.0: output = "abcd1" If statement so output
1.2.2: subsets(0, "abcd") 2nd line of else statement n-1 = 0 andAlso = "abcd"
1.2.2.0: output = "abcd" If statement so output

The main problem seems to be is what you want to do is to somehow make all possible subsets of the length you pass in (in your test case 2) of the string you pass in (in your test case "abcd") but your program logic doesn't seem right to do this, if it helps think of a string as an array of characters, it is possible to refer to each character individually. I would highly recommend netbeans to help, if you type in "String." for example it will list all methods associated with strings (save you remembering chunks of the API).
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
13
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
21K
  • · Replies 1 ·
Replies
1
Views
15K
  • · Replies 60 ·
3
Replies
60
Views
12K
Replies
19
Views
4K
  • · Replies 43 ·
2
Replies
43
Views
13K