-
Help..Need Assistance
I'm writing a C++ program that will print a diamond filled with *'s, with the size of the diamond to be detemined by the user. This is what I'm try to get:
*
***
*****
*******
*****
***
*
But my code gives me this:
*****
****
***
**
*
Here's my code:
//diamond_of_asterisks
cout << "Enter size n (1 to 80):" <<endl;
cin >> n;
for (int k = 1; k <=5; ++k)
{
cout << setw(k) << ' ';
for (int j =k; j <= 5; ++j;
cout << '*';
cout << endl;
I can't seem to complete the diamond. Can anyone help me? Thanks!
-
Neither of your examples look like a diamond. However, find a midpoint. Increase until you reach it. Decrease after.
-
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
cout << "Enter size: 1-80" << endl;
cin >> size;
if (size > 0 && size < 80)
{
for (int i = 0; i < size; i++)
{
int asterix = ++i;
for (; asterix != 0; asterix--)
{
cout << '*';
}
cout << endl;
}
for (i = size; i > 0; i--)
{
int asterix = --i;
for (; asterix != 0; asterix--)
{
cout << '*';
}
cout << endl;
}
}
-