I was given an assignment to write a recursive function to print a diamond comprised of astrisks (less the dashes) such as:
---*
--* *
-* * *
* * * *
-* * *
--* *
---*
The parameter is the number of astricks in the largest row. I have written the function which works fine but, I am lost when it comes to converting this function to a recursive function. Any help would be appreciated.
#include <iostream>
#include <iomanip>
using namespace std;
void diamond(int row);
int main()
{
diamond(12);
}
void diamond(int row)
{
int spaces = row * 2;
for (int i = 0; i <= row + 1; i++)
{
cout << setw(spaces);
for (int j = 0 ; j < i - 1; j++)
cout << "* ";
cout << endl;
--spaces;
}
for (int i = 0; i < row; i++)
{
cout << setw(spaces+2);
for (int j = row - 1 ; j > i; j--)
cout << "* ";
cout << endl;
++spaces;
}
}