Answer to Question #21559 in C++ for SAm Sikander
write a program that displays a diamond of asterisks using nested loop
1
2013-01-28T09:35:31-0500
#include <conio.h>
#include <iostream>
using namespace std;
void print_diamond(int height)
{
int align;
int delta_width = 2;
int current_width = 1;
int max_width = height + 1;
for (int y = 0; y < height; y++)
{
align = (max_width - current_width) / 2;
for (int x = 0; x < align; x++) //fill left side with spaces
cout << ' ';
for (int x = 0; x < current_width; x++) //print astrerisks
cout << '*';
for (int x = 0; x < align; x++) & //fill right side with spaces
cout << ' ';
/* Change the delta between the count of asterisks
on the current and the next row */
if (y == height / 2)
delta_width = -delta_width;
current_width += delta_width;
cout << endl;
}
}
void main()
{
print_diamond(9);
getch();
}
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
C++
Comments
Leave a comment