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

# sum of positive integers

 P: 4 pls help me to write a program (in c) such that we input an integer x,where x>0. For x, the program has to convert it into the sum of positive integers (all posible!). for e.g. x =8 1. 8 2. 7 + 1 3. 6 + 2 4. 6 + 1 + 1 5. 5 + 3 6. 5 + 2 + 1 7. 5 + 1 + 1 + 1 8. 4 + 4 9. 4 + 3 + 1 10. 4 + 2 + 2 11. 4 + 2 + 1 + 1 12. 4 + 1 + 1 + 1 + 1 13. 3 + 3 + 2 14. 3 + 3 + 1 + 1 15. 3 + 2 + 2 + 1 16. 3 + 2 + 1 + 1 + 1 17. 3 + 1 + 1 + 1 + 1 + 1 18. 2 + 2 + 2 + 2 19. 2 + 2 + 2 + 1 + 1 20. 2 + 2 + 1 + 1 + 1 + 1 21. 2 + 1 + 1 + 1 + 1 + 1 + 1 22. 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 Result should be displayed. thanx in advance Nov 27 '07 #1
Share this Question
7 Replies

 Expert Mod 100+ P: 931 pls help me to write a program (in c) such that we input an integer x,where x>0. For x, the program has to convert it into the sum of positive integers (all posible!). for e.g. x =8 1. 8 2. 7 + 1 3. 6 + 2 4. 6 + 1 + 1 5. 5 + 3 6. 5 + 2 + 1 7. 5 + 1 + 1 + 1 8. 4 + 4 9. 4 + 3 + 1 10. 4 + 2 + 2 11. 4 + 2 + 1 + 1 12. 4 + 1 + 1 + 1 + 1 13. 3 + 3 + 2 14. 3 + 3 + 1 + 1 15. 3 + 2 + 2 + 1 16. 3 + 2 + 1 + 1 + 1 17. 3 + 1 + 1 + 1 + 1 + 1 18. 2 + 2 + 2 + 2 19. 2 + 2 + 2 + 1 + 1 20. 2 + 2 + 1 + 1 + 1 + 1 21. 2 + 1 + 1 + 1 + 1 + 1 + 1 22. 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 Result should be displayed. thanx in advance Help in what sense? What have you tried so far? Please post your efforts here so we can help you. Just apply simple mathematical logic and try to convert it into C conventions. Please read posting guidelines. Regards Nov 27 '07 #2

 P: 4 I have problem with converting my idea into C/c++ conventions, because now I begin my adventure with this language. I know that to split integer into two parts you have to use something like: b=1; while(a!=0, a>=b) { a--; printf("%d + %d\n", a, b); b++; } so if we could split number a, and b again it should work, but I think it won't work because it will be problem with order of numbers, and many more. The second idea I have is to initiate chart with growing size, in with we first put our number( size=1) then size=2 and split our X, then size 3 in which we firstly split number to two first "columns" and in 3th place would be "1". When it's done we add 1 to the 2nd number (-1 in 1st), and than program should try add to the 3th as lond as 3th=<2nd, than we again add 1 to 2nd, and the same with 3th, the same should work with as long as we obtain "1" in everywhere. I think the second solution should work, but it comlicated and I have problem with how to write it. I thinking about it from a few days, and I have no idea how to do it. You could help me even by sugestin me other way of solwing this problem. And sorry for my english! Nov 27 '07 #3

 Expert 2.5K+ P: 3,652 I would try using a recursive function to find all the integers adding up to the int value passed. You would call it with 8, and it would first print 8, then 7 and whatever integers added up to 1 (i.e. 1), then 6 and whatever integers added up to 2 (i.e. 2 and 1 + 1), etc etc. Your base condition would be when the number is 1, the sum of integers resulting in it is simply 1. After this, all that remains is fancy printing. Nov 27 '07 #4

 P: 4 But what with that exemple: 18. 2 + 2 + 2 + 2 in this case we don't have 1. Nov 28 '07 #5

 Expert 100+ P: 671 Hmm, yes, so your algorithm clearly can't rely on there being a 1 at the end, right? Look, you've just dumped your question here, and walked away. Finding out the algorithm is part of being a programmer and doing the homework assignment. We aren't going to to tell you what the algorithm is. So get to work. Sit down with pencil and paper and come up with ideas. Nov 28 '07 #6

 Expert 2.5K+ P: 3,652 But what with that exemple: 18. 2 + 2 + 2 + 2 in this case we don't have 1. This is actually included in the recursive solution - I leave it up to you to show that it is. Think of the similarity with 19. 2 + 2 + 2 + 1 + 1 (i.e. (2 + 2 + 2) + 2 and (2 + 2 + 2) + 1 + 1) Nov 28 '07 #7

 P: 4 Ok Ganon11, you are right. I found this: http://www.site.uottawa.ca/~ivan/F49-int-part.pdf and I will try to use it. Nov 28 '07 #8

### Post your reply

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