Answer to Question #21559 in C++ for SAm Sikander

Question #21559
write a program that displays a diamond of asterisks using nested loop
1
Expert's answer
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS