Thread: simple Recursion program - strange output

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yep, and you even named the variables the same way. Implement the recursive version then. You just need to invert the loop condition to be your recursive base case (or alternatively, you can put the entire body of the function within the body of the if statement).

    Actually, since you are only printing, you don't need to return an int, i.e., change rec's return type to void.
    Last edited by laserlight; 09-13-2019 at 08:52 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program output is strange characters, totally baffled
    By chronograff in forum C Programming
    Replies: 4
    Last Post: 05-16-2017, 06:43 AM
  2. Whats wrong with simple recursion printing program?
    By tmac619619 in forum C Programming
    Replies: 6
    Last Post: 09-02-2015, 02:41 PM
  3. Replies: 1
    Last Post: 03-16-2015, 10:36 AM
  4. Help: Really strange output for simple calculation.
    By gpix13 in forum C Programming
    Replies: 4
    Last Post: 01-16-2010, 08:27 AM
  5. Very strange behavior from very simple program
    By somekid413 in forum C Programming
    Replies: 5
    Last Post: 12-17-2009, 08:53 PM

Tags for this Thread