@Piyush: Awesome solution dude!
To avoid repetitions you had started the recursion from the current
index instead of zero.


On Jun 23, 11:01 am, Piyush Sinha <ecstasy.piy...@gmail.com> wrote:
> sorry a lil bit modification in the above answer..
>
> *int ref[] = {1,2,3};*
>
> *void printcombination(int n,int index,int i)*
>
> *{*
>
> *            static int a[100];*
>
> *            int j;*
>
> *            if (n == 0)*
>
> *           {*
>
> *                      for(j=0;j<index;j++)*
>
> *                              printf("%d ",a[j]);*
>
> *                       printf("\n");*
>
> *            }*
>
> *           else if(n>0)*
>
> *           {*
>
> *               for(j=i;j<3;j++)*
>
> *              {*
>
> *                    a[index]=ref[j];*
>
> *                  printcombination(n-ref[j],index+1,j);*
>
> *               }*
>
> *          }*
>
> *} *
>
> *main()*
>
> *{*
>
> *          int n;*
>
> *           printf("enter value of n :");*
>
> *          scanf("%d",&n);*
>
> *          printcombination(n,0,0);*
>
> *}*
>
> On Thu, Jun 23, 2011 at 11:17 AM, Piyush Sinha 
> <ecstasy.piy...@gmail.com>wrote:
>
>
>
>
>
>
>
>
>
> > pass one more argument to the function "*int index*" and instead of
> > starting the loop from *i = 0 to N, *make it start from *i = index to N *and
> > then call
>
> >             *printcombinations(a,sum-a[i],level+1,index+1);
> > *I think it will work then...
> >   On Thu, Jun 23, 2011 at 10:48 AM, ross <jagadish1...@gmail.com> wrote:
>
> >> Given an array and a sum S output all combinations of elements that
> >> sum to S.
> >> eg: 1 2 3
>
> >> sum = 3
> >> 1+1+1,
> >> 2+1
> >> 3
>
> >> I came up with the foll algorithm, but it outputs 2+1 and 1+2 again.
> >> (does not handle repetitions)
>
> >> printcombinations(int a[],int sum,int level) {
> >> if(sum==0) { print array}
> >> else if (sum>0) {
> >> for ( i = 0 to N ) {
> >> array[level]=a[i];
> >> printcombinations(a,sum-a[i],level+1);
> >> }
> >> }
> >> }
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Algorithm Geeks" group.
> >> To post to this group, send email to algogeeks@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> algogeeks+unsubscr...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/algogeeks?hl=en.
>
> > --
> > *Piyush Sinha*
> > *IIIT, Allahabad*
> > *+91-8792136657*
> > *+91-7483122727*
> > *https://www.facebook.com/profile.php?id=100000655377926*
>
> --
> *Piyush Sinha*
> *IIIT, Allahabad*
> *+91-8792136657*
> *+91-7483122727*
> *https://www.facebook.com/profile.php?id=100000655377926*

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to