0

Basically,

I need to program something using STACK, backtracking to make all the possible sum, of "n" numbers.

Example. If a user input 5.

n = 5
1: 1 1 1 1 1
2: 1 1 1 2
3: 1 1 3
4: 1 2 2
5: 1 4
6: 2 3
7: 5

It will never even print the same kind. Like once it prints 2+3, it will not print 3+2.
And my professor said once it found the sum is 5, it will just print. It will not store
the value 1+1+1+1+1. so after every loop, the stack will have a new number in it. Can some one figure out the logic. Thanks

Steve

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Adak
0

So your logic needs to consider the lowest number first, and then continue adding the lowest number, until it's >= 5. If it's == 5 then print it.

Then you pop off the next number from the stack, and having backtracked to one for the second number, trying adding more one's until it's >=5, printing it if it's == 5.

The best way to kick-start your logic on a problem, is to keep working with the problem, keep seeing the resulting answers, and keep doing it by hand. Naturally, use your class notes as well, because you want to do this as much like your instructor wants, as possible.

Get clear on whether stack means using a data stack (a linked list or small array, as a stack), or if it means using the stack memory as a stack, in your program, by calling subordinate functions (and returning from them).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.