Hi there,
My son has been recently hospitalized after getting into an accident. Now I am trying to help him with some of his projects. Right now I am stuck on this recursion problem. We need to figure out how to form a diamond shape out of *'s
using a recursive function. In my code here, I am using two recursive functions, because I cannot figure out how to use just one. Also, if you run this program, you will realize the top half of the diamond works, and the bottom half doesn't seem to work correctly. Please take a look at my code and REMEMBER, I am completely new to this. I don't even work with computers and I am trying to catch on quick to help my son keep up with his school work while he is hospitalized for the next couple of weeks. Your help is much appreciated, and please try not to laugh at my effort : ) Below is the diamond shape that I am trying to accomplish, and the code that I am using.
....*....
...* *...
..* * *..
.* * * *.
..* * *..
...* *...
....*....
The periods are just spaces.
#include <iostream>
using namespace std;
int printup(int m);
int print(int n);
int main(void)
{
printup(4);
print(3);
return 0;
}
int print(int n)
{
if(n == 0)
return 0;
for (int i = 0; i < n; i++)
if(i<=n)
cout << "* ";
else
cout << " ";
cout <<endl;
n = print(--n);
}
int printup(int m)
{
if (m == 0)
return 0;
for (int i = 0; i < 5; i++)
if(i>=m)
cout << "* ";
else
cout << " ";
cout << endl;
m = printup(--m);
}