Hey guys! It's been a long time since I visited daniweb,but I need some help understanding something.A friend of mine gave me problem to solve, but the assignment is strange,I can't seem to find the actually meaning of what I need to generate as an answer.Here goes:
By a given natural number n ,find all the possible unordered representations of n as a sum of natural numbers. The ouput must be lexicographical.
Ok,lexicographical would mean alphabetically,which is exactly like the words orderd in dictionaries. So how does this work for numbers?
Let's say the number 6:
1+1+1+1+1+1
1+1+1+1+2
1+1+1+3
1+1+2+2
1+1+4
1+5
I have no idea if this is the correct way. Give me some clues,because there is a deadline I want to get this over with. The teacher who gave this problem to my friend told her she needs to be careful about repetitions and that the output is going to be massive,so it's best to print it to a file,and that's the only clue given about assignment . I am posting a code I found ,but it does not output exactly what I need.
#include<stdio.h>
void show(int length, int *psdata){
int i;
for(i=1;i<length;i++)
printf("%d+",psdata[i]);
printf("%d\n",psdata[length]);
}
void func(int n, int pos, int *pdata){
int k;
for(k=n;k>=1;k--)
{
if(n!=k){
pdata[pos]=k;
if(pdata[pos]<=pdata[pos-1])
func(n-k, pos+1, pdata);
}
else{
pdata[pos]=k;
if(pdata[pos]<=pdata[pos-1])
show(pos, pdata);
}
}
}
int main(){
int num, *data;
printf("Enter number: ");
scanf("%d",&num);
data = (int*)malloc(sizeof(int)*num);
data[0]=num+1;
func(num,1, data);
system("pause");
return 0;
}