# a program recursive print from 1 to 2^n

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-10-2012
a program recursive print from 1 to 2^n
i will write a program with function that recursively print from 1 to 2^n
for example i give number 3 to pragram and it will be print 1 2 4 8
sorry for my bad english, plz help me on this program :redface:
• 01-10-2012
Salem
Well....
Code:

```void foo ( int depth ) {   if ( depth >= 0 ) {     foo(depth-1);   } }```
• 01-10-2012
but this not works:confused:
• 01-10-2012
Salem
That's because it's an "idea", not an "answer" you can just run back to teacher with, without thinking about it.
• 01-10-2012
i know the way of a recursive, but i will know the algorithm of this program, i thinked very much, but i can't
• 01-10-2012
anduril462
• 01-10-2012
Salem
> for example i give number 3 to pragram and it will be print 1 2 4 8
So figure out how to do it using just a simple for loop, then you'll have your answer.
• 01-10-2012
i dont write any code but i thinked about it really
• 01-10-2012
anduril462
Well, then just tell your teacher you thought about it and thought up a correct answer. I'm sure s/he will give you a good grade for "thinking about it". You have nothing to lose by trying. It's not like making a mistake will cause your computer to blow up, taking your hands and arms with it. Besides, you might even learn something along the way. Regardless, we sure wont do it for you, so you try something, and post your attempt, and we will help you.
• 01-10-2012
failure67
Just in case you come back for help, here's an additional hint. Take Salem's code and add one print statement to it; that's all you need to do. Also, look up the left shift operation.
Bitwise operation - Wikipedia, the free encyclopedia
• 01-10-2012
iMalc
I you aren't even going to go as far as making an attempt and posting the code then you obviously aren't cut out to be a programmer.
As a programmer you need to be able to push the boundaries, step outside of the box, and try things you haven't tried before. Otherwise you'll never get past "Hello World".
• 01-11-2012
i writed this program, this program good works until a[k] but a[i] don't print correctly, plz help on this.

Code:

```#include<conio.h> #include<stdio.h> void baz(int,int,int); void main(){         int n;         printf("enter n : ");         scanf("%d",&n);         printf("\n");         int k=n-1,l=n;         baz(n,k,l);         getch(); } void baz(int n,int k,int l){                 int i,s=1,a[100];         if (n>0){         for(i=0;i<n;i++){         s=s*2;}         a[k]=s;         printf("%d ",a[k]);         baz(n-1,k-1,l);         }         else{         a[k]=1;         for(i=0;i<l;i++)         printf("%d ",a[i]);} }```
• 01-11-2012
i changed to this shape, see that, a[k]'s is true but a[i] is wrong. why?:confused:

Code:

```void baz(int n,int k,int l){                 int i,s=1,a[100];         if (n>0){         for(i=0;i<n;i++){         s=s*2;}         a[k]=s;         printf("  %d  ",k);         printf("%d ",a[k]);         baz(n-1,k-1,l);         }         else{         printf("%d",k);         a[k]=1;         for(i=0;i<=l;i++){         printf("%d  ",i);         printf("%d ",a[i]);}         } }```
• 01-11-2012
```void baz(int n,int k,int l,int* a){                 int i,s=1;         if (n>0){         for(i=0;i<n;i++){         s=s*2;}         a[k]=s;         baz(n-1,k-1,l,a);         }         else{         a[k]=1;         for(i=0;i<=l;i++)         printf("%d ",a[i]);         } }```