Like Tree6Likes

a program recursive print from 1 to 2^n

This is a discussion on a program recursive print from 1 to 2^n within the C Programming forums, part of the General Programming Boards category; i will write a program with function that recursively print from 1 to 2^n for example i give number 3 ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    22

    Question 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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Well....
    Code:
    void foo ( int depth ) {
      if ( depth >= 0 ) {
        foo(depth-1);
      }
    }
    stahta01 and abbaskhan like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    but this not works

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    That's because it's an "idea", not an "answer" you can just run back to teacher with, without thinking about it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    i know the way of a recursive, but i will know the algorithm of this program, i thinked very much, but i can't

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Well, show us what you've tried. Post your code in code tags (read this link), and we'll help you fix whatever problems you're having.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    i dont write any code but i thinked about it really

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    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.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    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

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    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".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    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]);}
    }

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    i changed to this shape, see that, a[k]'s is true but a[i] is wrong. why?

    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]);}
    	}
    }

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    I correct it on my own, the truth code is this:


    Code:
    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]);
    	}
    }

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    but maximum number is 14 when you enter 15 it will be negative 32768, i changed a type to unsigned int but the maximum number is 14 again, how can I up this range?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a recursive expression tree program
    By TeamRival in forum C Programming
    Replies: 6
    Last Post: 03-25-2011, 07:27 PM
  2. Tracing through a recursive program in C
    By Hybodus in forum C Programming
    Replies: 3
    Last Post: 12-18-2010, 04:48 PM
  3. Recursive print every time...
    By BobDole11 in forum C Programming
    Replies: 5
    Last Post: 10-27-2008, 12:42 AM
  4. Recursive binary search program help.
    By mooney in forum C Programming
    Replies: 3
    Last Post: 04-04-2008, 01:12 AM
  5. Help!!this program is Recursive Function or not?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-25-2002, 03:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21