By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,648 Members | 1,528 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,648 IT Pros & Developers. It's quick & easy.

Need help for improvement.

P: 6
I need some help to improve my code.
My code is duplicating the output, and the output format is not what I want.

Here is the full Pseudo code.
Expand|Select|Wrap|Line Numbers
  1. int binary = 0;
  2. int sum;
  3.  
  4. while(binary<256)
  5. sum = 0;
  6. for(go through all 8 digits)
  7.     if the i-th digit is 0
  8.         sum += i
  9.     if the i-th digit is 1
  10.         sum -= i
  11. end for
  12.  
  13. if sum == 0
  14.     output
  15.  
  16. binary++
  17. end while
  18.  
Since sum, binary, and check is initialize as 0.

I have written this code using the Pseudocode given above. But seems like my code will duplicate the output and one more problem, the format.

I want the output be like this format :
Expand|Select|Wrap|Line Numbers
  1. Enter a number : 3
  2.  
  3. -1 -2 +3 = 0
  4. 1 +2 -3 = 0 
  5.  
But my currently output is :
Expand|Select|Wrap|Line Numbers
  1. Enter a number : 3
  2.  
  3. -1 -2 3 = 0
  4. 1 2 -3 = 0
  5.  
Expand|Select|Wrap|Line Numbers
  1.     CODE IS REMOVED.
  2.     PROBLEM OF FORMAT AND DUPLICATION OF OUTPUT ARE SOLVED
  3.  
The duplication is because of the binary < 256. As you draw the tree diagram out, I find out that 256 is equal to 2^8, which is also 256 possible solutions.
So, if I enter "3" inside the program, the program will definitely loop from 1 to 3 only, but the program will continue to loop for all 256 solutions, which is from 1 to 9. So, for sure there will be same answer printed out.
So, I change the looping to 2^n so that it will stop where I wanted.
Thanks for the helps from all.
Apr 13 '12 #1
Share this Question
Share on Google+
8 Replies


P: 62
It's not clear what you mean by "duplicates", but as for the output format, a positive number converted to a string will not have the + sign by default. If you wanted a +, you can add it yourself, like "+" + i + " ";
Apr 13 '12 #2

P: 6
@limweizhong

The actual answer is 10 times the answer in the example, meaning that the print out 10 times, 20 lines, and not 2 lines.
And the "+" sign I did it before, it just add before 1 and stop adding to the others positive number.
Apr 13 '12 #3

P: 62
There are no repeats in the binary representation of numbers 0 to 255, so based on your "pseudo-code", there should be no repeats. It's probably not your output code then. Anyway if you did type out exactly string += "+" + i + " "; in line 9 of your output code it should have worked. Please try without assuming.
Apr 13 '12 #4

P: 6
About the format, previously I put the "+" in wrong place, sorry for my fault.
I have added my full code, can you help me check it?
Thanks.
Apr 13 '12 #5

P: 62
You're not "[going] through all 8 digits" as you stated in the pseudo-code. If you go through only 3 digits, it will be only the last 3 digits of the binary numbers from 0 to 255 and thus this will contain many repeats, because the last 3 digits of 8n + k will be the same as k for all n. Moreover, if you input 9, your answer will be wrong. You must calculate the number to put in place of 256, i.e. 2 ^ input
Apr 13 '12 #6

P: 62
Also, this is a simple integral knapsack problem that also can be solved by dynamic programming.
Apr 13 '12 #7

P: 6
Thanks for your helps.
The pseudocode is given by someone replied my post at the answer.yahoo.com, so i used some time to study it.
I'm a very very new Java programmer, previously I just studied C. So, I'm a bit blur when see different things.
I'm apologize for my disturb.
Apr 13 '12 #8

P: 6
By the way, user only allow to input <=9 as mentioned in the question I found online. advanced.pdf
Apr 15 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.