I wanna print two diamonds side by side, but my code prints 1 diamond.
I do not know what else to do.
Any help would be appreciated.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int i,j,n, middle, spaceCount, starCount;
cin >> n;
middle = (n - 1) / 2;
for ( i = 0; i < n; i++)
{
spaceCount = abs(middle - i);
starCount = n - 2 * abs(middle - i);
for ( j = 0; j < spaceCount; j++)
cout << " ";
for (j = 0; j < starCount; j++)
cout << "*";
for (j = 0; j < spaceCount; j++)
cout << " ";
cout << endl;
}
}
input = odd numbers
desired output =
* *
*** ***
**********
*** ***
* *
You forgot to print you second diamond shape. Each iteration, you should print first a few spaces, then the stars, then the double of the spaces (to finish the first diamond and set spaces for the second diamond) and then you should draw you stars for your second diamond. This is an example of code that prints two diamonds:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int i,j,n, middle, spaceCount, starCount;
cin >> n;
middle = (n - 1) / 2;
for ( i = 0; i < n; i++)
{
spaceCount = abs(middle - i);
starCount = n - 2 * abs(middle - i);
// print a row of the first diamonds
// spaces at the beginning
for ( j = 0; j < spaceCount; j++)
cout << " ";
// the stars itself
for (j = 0; j < starCount; j++)
cout << "*";
// finish the rectangle of the first diamond
for (j = 0; j < spaceCount; j++)
cout << " ";
// print a row of the second diamond
// spaces at the beginning
for (j = 0; j < spaceCount; j++)
cout << " ";
// the stars itself
for (j = 0; j < starCount; j++)
cout << "*";
// spaces at the end are not necessarily required for the last diamond
cout << endl;
}
}
It would be even better to create a function to print one diamond (row) and call this function two times (this prevents duplicate code).