Basic recursion question

This is a discussion on Basic recursion question within the C Programming forums, part of the General Programming Boards category; Without using any loops, I need to create a function that will use recursion to print all the multiples of ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    Basic recursion question

    Without using any loops, I need to create a function that will use recursion to print all the multiples of a given number up to any limit given (the limit will be given to the function by the programmer). e.g. given 7, will print 7, 14, 21, 28...(until the end)

    Here is the code I have:
    Code:
    #include <stdio.h>
    
    int main() 
    {
        int a, b, c;
        
        printf("Enter in an integer and a number you wish to stop at:\n");
        scanf("%d%d", &a, &b);
        
        c = a; 
        c < b;  
        c += a;
        
    	printf("Your numbers are: %d.\n", c);
        
        
        return 0;
    }
    the problem is when I run the code the program only outputs the next number and not the original number or the numbers leading up to the number I want the program to end at.

    Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    You're way off base. Read a tutorial on recursion: Cprogramming.com Tutorial: Recursion.

    A quick note on your program
    Code:
        c = a;  // assign value in a to c
        c < b;  // compare to check if c is less than b, this has no effect
        c += a;  // increment c by value in a

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by anduril462 View Post
    You're way off base. Read a tutorial on recursion: Cprogramming.com Tutorial: Recursion.

    A quick note on your program
    Code:
        c = a;  // assign value in a to c
        c < b;  // compare to check if c is less than b, this has no effect
        c += a;  // increment c by value in a
    So I just read the tutorial and it was no help. PLEASE EXPLAIN!!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Read your class notes and text book, and search the web for more examples. There are many, far better tutorials out there than I can provide in a 2 minute break from work. Make an effort to write recursive code. Come back with a program that has a second function (other than main), and make sure that calls itself. Then, we'll start working on your base case and recursive steps, but you have to make an effort first.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    I've read 10 recursion tutorials and they are all based on the same examples. Does anyone know how I can change my code to output all the numbers not just to 1st?

    I am looking for my program to do the same thing as Simple for loop question without using a loop.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    122
    Quote Originally Posted by benrogers View Post
    So I just read the tutorial and it was no help. PLEASE EXPLAIN!!
    Really? You read that tutorial and this part didn't help at all

    Code:
    void count_to_ten ( int count )
    {
        /* we only keep counting if we have a value less than ten
           if ( count < 10 )   
           {
               count_to_ten( count + 1 );
           }
    }
    int main()
    {
      count_to_ten ( 0 ); 
    }

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by homer_3 View Post
    Really? You read that tutorial and this part didn't help at all

    Code:
    void count_to_ten ( int count )
    {
        /* we only keep counting if we have a value less than ten
           if ( count < 10 )   
           {
               count_to_ten( count + 1 );
           }
    }
    int main()
    {
      count_to_ten ( 0 ); 
    }
    Yes what does
    Code:
    void count_to_ten ( int count )
    even mean?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Think about the logic that your recursive function will need. In pseudo code:

    Code:
    void recurse(int theNumber, int aCounter, int limit) 
      if(theNumber * aCounter < limit) 
         //show your multiplication answer in here
         increment your counter 
         and call recurse function, again.
      end if
    end function
    If you start doing "number = number * counter", you'll be off into the weeds, very fast.

    It means "count to ten() is a function that takes the parameters (variables), that are listed between ( and ).

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by benrogers View Post
    Yes what does
    Code:
    void count_to_ten ( int count )
    even mean?
    If you don't know what that means, then you're not ready for recursion yet. Sorry, but you must have a good understanding of functions before you can do recusion. Read some function tutorials: Cprogramming.com Tutorial: Functions. Search the web for more. Master functions, then work on recursion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Basic question
    By voodoo3182 in forum Windows Programming
    Replies: 11
    Last Post: 09-12-2005, 08:40 PM
  2. Basic string question
    By charbach007 in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2005, 12:41 PM
  3. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 04:37 PM

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