I got a different resolt than i was supposed to

  • Thread starter transgalactic
  • Start date
In summary, the program creates a diagram of all subsets of a string (in this case "abcd"), but it is unclear what the purpose of the diagram is.
  • #1
transgalactic
1,395
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
  • #2
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.
 
  • #3
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).
 

1. Why did I get a different result than I was supposed to?

There are several reasons why you may have gotten a different result than you were expecting. It could be due to a mistake in your experimental procedure, an error in your calculations, or a problem with the equipment you used. It is important to carefully review your methods and data to determine the source of the discrepancy.

2. Can experimental error cause a different result than expected?

Yes, experimental error can contribute to getting a different result than expected. This can be caused by human error, faulty equipment, or unforeseen external factors. It is important to account for and minimize experimental error to ensure accurate results.

3. How do I troubleshoot a different result?

To troubleshoot a different result, start by reviewing your experimental procedure and data to identify any potential errors or sources of discrepancy. You may also want to repeat the experiment or try using different equipment to see if you get the same result. Consulting with colleagues or seeking guidance from a mentor can also be helpful.

4. Should I be concerned if I get a different result than expected?

Getting a different result than expected can be concerning, but it is not uncommon in the scientific process. It is important to carefully analyze and evaluate the data to determine the cause of the discrepancy. If the result is still unexpected, it may be necessary to repeat the experiment or seek additional guidance.

5. How do I ensure accurate and consistent results in my experiments?

To ensure accurate and consistent results in your experiments, it is important to carefully plan and execute your experimental procedures, accurately record and analyze your data, and properly calibrate and maintain your equipment. It is also important to be aware of potential sources of error and take steps to minimize their impact on your results.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
13
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Math Proof Training and Practice
2
Replies
60
Views
8K
  • Programming and Computer Science
Replies
13
Views
19K
  • Calculus and Beyond Homework Help
Replies
7
Views
2K
  • Introductory Physics Homework Help
Replies
19
Views
4K
Replies
1
Views
2K
  • Math Proof Training and Practice
2
Replies
43
Views
9K
  • Programming and Computer Science
Replies
1
Views
14K
Back
Top