Recursive Function

This is a discussion on Recursive Function within the C++ Programming forums, part of the General Programming Boards category; I'm tring to write a recursive function that takes as a parameter a non-negative integer and generates the following pattern ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    9

    Recursive Function

    I'm tring to write a recursive function that takes as a parameter a non-negative integer and generates the following pattern of stars if the integer is 4.


    *
    * *
    * * *
    * * * *
    * * *
    * *
    *




    Below is the code I have so far. Which gives me


    *
    * *
    * * *
    * * * *



    Can anyone explain how to get the other half?


    Code:
    #include <iostream>
    #include <iomanip>
    #include<fstream>
    using namespace std;
    int func(int x);
     
    int main(int argc, char *argv[])
    {
    int number;
     
    func(4);
     
    system("PAUSE");
    return 0;
    }
    int func(int x)
    {
    int i;
     
    if (x>=1)
    func(x-1);
    for (i=1; i<=x; i++)
    {
    cout<<"* ";
    }
    cout << endl; 
    return 1;
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    There are multiple ways to do this

    You can add a second argument to the function that tells you whether it's growing or shrinking
    or...
    Check the value of x and if it's greater than 4, draw x - x&#37;5 stars

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    9
    I have added a second argument. I'm still getting the same result. I'm missing something somewhere. Just not sure where.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    9

    What am I missing?

    Code:
    #include <iostream>
    #include <iomanip>
    #include<fstream>
    
    using namespace std;
    
    int func(int x, int y);
    
    
    
    int main(int argc, char *argv[])
    {
    	int number;
    	
    	
    
    
    	func(4);
    	func(0);
    
    
    	system("PAUSE");
    	return 0;
    	
    }
    
    int func(int x, int y)
    
    {
    	int i, j;
    	
    
    
    	if (x<=4)
    		func(x+1);
    			
    	for (i=1; i>=x; i++)
    	{
    			cout<<"* ";
    	}
    	cout << endl;	
    	
    	
    
    
    
    	if (y<=0)
    		func(y+1);
    			
    	for (j=1; j>=y; j--)
    	{
    			cout<<"* ";
    	}
    	cout << endl;	
    	
    	return 1;
    			
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If the question is "what have I missed", I fear that the answer is "everything".

    For any recursion, you'll have to decide what the base case is -- the simple thing to do, the part where the recursion stops. In your example, it would appear to be the case where the line we need to print is the long line; if so just print it and be done.
    If we're not on the long line, we need to print out our short line, make a recursive call to print out the longer lines, and then print another short line at the bottom to make the pyramid shape.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    How does a recursive function work?
    Let me demonstrate an example:

    Code:
    Entering foo: 0
    Entering foo: 1
    Entering foo: 2
    Entering foo: 3
    Entering foo: 4
    Entering foo: 5
    Entering foo: 6
    Entering foo: 7
    Entering foo: 8
    Entering foo: 9
    Entering foo: 10
    Leaving foo: 10
    Leaving foo: 9
    Leaving foo: 8
    Leaving foo: 7
    Leaving foo: 6
    Leaving foo: 5
    Leaving foo: 4
    Leaving foo: 3
    Leaving foo: 2
    Leaving foo: 1
    Leaving foo: 0
    This is the output for some (secret) code. It shows the logic of recursive functions.

    With this information, you should be able to write your stars.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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