c++ - Direct And Indirect Recursion Issue -
so i'm having recursion bug.
output want...
input 4
* * * *
* * *
* *
*
* *
* * *
* * * *
output get..
input 4
* big blank space*
i cant seem wrap head around recursion.
#include<iostream> #include<fstream> #include<string> #include<windows.h> #include<ctime> using namespace std; int i; bool end = false; int changer = -1; int placeholder; bool recursionup(int num1) { if(num1 == placeholder) { return true; } for(i = placeholder; == num1; i--) { cout << "*"; } cout << "\n"; recursionup(num1 + 1); } bool cont = false; int recursion(int num1) { if(num1 == 0) { cont = recursionup(num1); } for(i = 1; <= num1; i++) { cout << "*"; } recursion(num1 - 1); if(cont) { return 0; } } int main() { int number; cout << "input star number...\n"; cout << "\t input: "; cin >> number; placeholder = number; recursion(number); return 0; }
can point out mistake?
as recursion there 2 things you'll need define:
- the end condition
- a
depth
value (and other information that's needed)
in case end condition when depth == number - 1
you'll want print before , after each recursion.
void recursion(int depth){ if (depth <= 1) { cout << '*' << endl << endl; }else{ (int = 0; < depth; ++i)cout << "* "; cout << endl << endl; recursion(depth - 1); (int = 0; < depth; ++i)cout <<"* "; cout << endl << endl; } }
Comments
Post a Comment