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.
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 :
But my currently output is :
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.
My code is duplicating the output, and the output format is not what I want.
Here is the full Pseudo code.
Code:
int binary = 0;
int sum;
while(binary<256)
sum = 0;
for(go through all 8 digits)
if the i-th digit is 0
sum += i
if the i-th digit is 1
sum -= i
end for
if sum == 0
output
binary++
end while
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 :
Code:
Enter a number : 3 -1 -2 +3 = 0 1 +2 -3 = 0
Code:
Enter a number : 3 -1 -2 3 = 0 1 2 -3 = 0
Code:
CODE IS REMOVED.
PROBLEM OF FORMAT AND DUPLICATION OF OUTPUT ARE SOLVED
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.
Comment