Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Input: .
Output: Print every way of writing as a sum of natural numbers, with repetitions, in nondecreasing order.
Format:
Each number in the sum should be separated by a '+'.
The numbers should be listed in non-decreasing order.
End each line with \n
Examples:
Input instance: 3 \n
Output:
1+1+1 \n
1+2 \n
3 \n
Input instance: 5 \n
Output:
1+1+1+1+1 \n
1+1+1+2 \n
1+1+3 \n
1+2+2 \n
1+4 \n
2+3 \n
5 \n
Observation: In each line, the numbers appear in non-decreasing order.


What I have tried:

#include <stdio.h>
int main()
{
    int i,j;
    int n;
    scanf("%d",&n);
    int sum=0;
    if(sum!=n){
        for(i=1;i<n;i++){
        for(j=i;j<n;j++){
            printf("%d+",1);
        }
        printf("%d\n",i);
        }
    }
    printf("%d",n);

}
Posted
Updated 10-Feb-22 9:14am
Comments
Luc Pattyn 10-Feb-22 13:18pm    
can you come up with any value of n for which your code generates the correct output?
did you try n=3? n=5?
shaym roy 10-Feb-22 13:23pm    
when I tried n=5.Its output is
1+1+1+1+1
1+1+1+2
1+1+3
1+4
5 . I cant understand how should I print 1+2+2 and 2+3
Luc Pattyn 10-Feb-22 13:31pm    
when it fails for one or more cases, the algorithm is wrong.
Before you start coding, you should think about the problem, formulate a potential approach and test it manually.

I will not help you any further except for this one hint: when searching all correct summations in the required order you can't solve this by printing before you got a correct sum. Reduce the problem to a slightly smaller one.

To add to what Luc - rightly - said, have a look here: How to Write Code to Solve a Problem, A Beginner's Guide[^]
It won't give you the code, but it may help you work out a way to solve it.
 
Share this answer
 
Quote:
I cant understand how should I print 1+2+2 and 2+3

It is just that your code do not try to answer those cases, your algorithm is just wrong.
Your code just print a bunch of '1' and the last digit.
For a given n, your code will print n lines, how do you know the number of lines in answer is n ?

Advice: try to write the solution by hand with decreasing digits, figure out the rules leading to solutions.
Since each line of solution have a variable number of digits, recursive code or backtracking code may help.
Recursion (computer science) - Wikipedia[^]
Backtracking - Wikipedia[^]
5
4+1
3+2
3+1+1
2+2+1
...


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v5
Comments
Luc Pattyn 10-Feb-22 15:21pm    
Sorry, I disagree.
(1) Debuggers can be used to pinpoint coding errors, however debugger sessions don't fix wrong algorithm choices.
(2) Working backwards is the obvious way, however it will not yield the requested order!
Patrice T 10-Feb-22 15:59pm    
IMO,
1) Using the debugger helps to understand how the algorithm is working, and thus, why it fails. It is a step to solution.
2) I see it as an easier way to list all solutions, then, after one just have to reorder digits as requested.
Luc Pattyn 10-Feb-22 16:11pm    
No no
(1) you don't need an implementation to understand an algorithm. Abstract thinking is required, not implementation and certainly not tools to validate or improve implementation.
(2) You seem to have missed part of the assignment, not only do the numbers have to be monotonic within each line, but also the lines need to be in a specific order, see the n=5 example where 1+1+3 precedes 1+2+2
Patrice T 10-Feb-22 16:33pm    
1) Obviously, the OP do not understand why its own code do not give correct result, that is why I suggest to use the debugger.
2) I consider that even out of order, an algorithm that all correct solutions is an improvement over actual code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900