| | |
All I need is a simple yes or no answer
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 67
Reputation:
Solved Threads: 0
Is this an example of a recursive function?? Somebody on another forum helped me put this together, as I am trying to make a diamond shape out of asterisks. But he did not know if this was a correct way of using recursion. Can somebody just tell me yes or no, is this a correct example of a recursive function?? Thanks
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; int spaces = 5; int main() { for (int i = 1; i < 5; i = i + 1) { cout << setw(spaces); for (int j = 0 ; j < i; j++) cout << "* "; cout << endl; --spaces; } for (int i = 1; i < 4; i = i + 1) { cout << setw(spaces + 2); for (int j = 4 ; j > i; j--) cout << "* "; cout << endl; ++spaces; } }
Well, no. It's not.
Do you understand recursion? It's essentially another method of looping but rather than using while() or for(), for example, a function recursively calls itself:
That's not the most imaginative example of recursion but it should give you an idea.
Also. Using recursion on main() is normally considered not good.
Do you understand recursion? It's essentially another method of looping but rather than using while() or for(), for example, a function recursively calls itself:
cpp Syntax (Toggle Plain Text)
void count_down( int num ) { if ( num>=0 ) { std::cout<< num; cout_down( num-1 ); // Calls the function again but this time the // parameter is one less than before. By this // you can iterate through all the values you // need to. Use control statements to determine // how to proceed. } }
That's not the most imaginative example of recursion but it should give you an idea.
Also. Using recursion on main() is normally considered not good.
Last edited by twomers; Oct 23rd, 2007 at 6:47 pm.
•
•
Join Date: Oct 2007
Posts: 67
Reputation:
Solved Threads: 0
Ok, I understand how recursion works and everything. And some kind guy gave me the following example to show me how a recursive function can decrement through numbers until it hits zero and then go back up and print those numbers. However, my problem is that I cannot figure out how to make it print the asterisks (*). For instance, I am trying to print a diamond shape, so I need line 1 to print 1 *, line 2 to print 2 *'s, line 3 to print 3 *'s...etc until it gets to 4 *'s and then go back down again to make a diamond shape. When I try to apply asterisks to this program, all I get of course is a line of asterisks like so:
*
*
*
*
* etc.....
Below is the code the guy gave me as an example. If you would please, look at that code and look at my code for printing a diamond, and tell me how I might go about putting the two together to print the asterisks. Thanks a lot to whoever saves the day for me.
*
*
*
*
* etc.....
Below is the code the guy gave me as an example. If you would please, look at that code and look at my code for printing a diamond, and tell me how I might go about putting the two together to print the asterisks. Thanks a lot to whoever saves the day for me.
c++ Syntax (Toggle Plain Text)
#include <stdio.h> void recursiveFunction(int val) { // The working area of the function printf("called value is %d \n", val); val--; // decrement val // The test of the end-value // In this simple example it's simply waiting until val becomes 0 if (val > 0) { recursiveFunction(val); // >0 call again } else { return; // <= 0, done } // Returns will come here and val will be restored to // it's previous value // Do the next 'work' printf("return value is %d \n", val); return; } int main() { recursiveFunction(5); // start the function off return 0; }
•
•
•
•
Is this an example of a recursive function?? Somebody on another forum helped me put this together, as I am trying to make a diamond shape out of asterisks. But he did not know if this was a correct way of using recursion. Can somebody just tell me yes or no, is this a correct example of a recursive function?? Thanks
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- A Simple Question (Java)
- How Do I Overclock A Video Card (Monitors, Displays and Video Cards)
- XP anf Novell Client Dual Login (Novell)
- Some Simple Console App Questons (Java) (Java)
- A simple question about CMOS batteries (Motherboards, CPUs and RAM)
- Simple answer for vexing problem? (OS X)
- Developing, building, and testing. How do it the best? Learning from the world leader (C)
- Spyware? (Windows NT / 2000 / XP)
- Windows Remote Desktop (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: expert! how to do this? PHONEBOOK!
- Next Thread: C++ words
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






