C++ Diamond of Asterisks -- help plz?
Hey everyone, I am a c++ beginner and im taking a course for it right now. We are not at the point of learning this, but I like to get a head start =Þ.
Here is the problem.
Write a program that prints a diamond shape. You may use output statements that print either a single asterisk (*) or a single blank. Maximize your use of repitition (with nested for structures) and minimize the number of output statements.
Is there any one who would be willing to give me a good headstart here? it would be much appreciated. Thanks in advance!
Happysmileman wrote: Well I suppose have a number ('x') and a for loop that just creates a line of *'s 'x' wide until you get to the widest point, then another that does it decreasing 'x'…
Hmm… i see what your saying… something like this
int x;
for (x = 1; x <= 5; x++)
cout << "*";
– I realize that will get me a line of asterisks "x" amount of spaces, but what im struggling with is figuring out how to create a diamond using a for loop. I realize i need to use spaces and asterisks, but i dont know how to arrange the for loop to do so… this is an example of what i need
————–– the lines represent spaces, i hope that clears up my ––––—– question a bit. ———– ––––—— ———*—––
Took me awhile of screwing around, but I finally came up with some working code which I'm sure can be improved on but I haven't looked it over much yet. I've also included some random math stuff that seemed to help me figure it out, but I'm not sure how much sense it will make to you guys :P
#include <iostream>
int main(int argc, char **argv)
{
const int MAX_ROW = 16, MAX_COL = 16;
int total_spaces;
for(int row=0; row<=MAX_ROW; ++row)
{
int total_spaces=abs((row*2)-(MAX_ROW));
for(int spaces=abs((row*2)-(MAX_ROW)) / 2; spaces>0; --spaces)
std::cout << " ";
for(int col=1; col<(MAX_COL-total_spaces); ++col)
std::cout << "*";
std::cout << std::endl;
}
return 0;
}
/*
TOP HALF OF DIAMOND
Row 1 = abs(1 - 5) = 4 / 2 = 2 <--# of spaces on either side of the asterisk
Row 2 = abs(3 - 5) = 2 / 2 = 1
Row 3 = abs(5 - 5) = 0 / 2 = 0
MIDDLE PART OF DIAMOND
Row 4 = abs(7 - 5) = 2 / 2 = 1
BOTTOM HALF OF DIAMOND
Row 5 = abs(5 - 5) = 0 / 2 = 0
Row 6 = abs(3 - 5) = 2 / 2 = 1
Row 7 = abs(1 - 5) = 4 / 2 = 2
1-321x123 <--MAX_COL - spaces = # of 'x' s for each row
2-21xxx12
3-1xxxxx1
4-xxxxxxx
5-1xxxxx1
6-21xxx12
7-321x123
*/
Cheers, T-Metal
Thanks a lot for the code, thats a little to advanced for where im at right now… so i took the n00b way … previously in the excersizes we had to make triangles out of asterisks…4 different onces. i realized putting them together made a diamond shape…so thats just what i did LOL… check out my code
#include <iostream>
using namespace std;
int main()
{
// TOP HALF!
for (int count = 0; count <=10; count++)
{
//Bottom left triangle
for (int topspaces = 10; topspaces >= count; topspaces--)
cout << " ";
for (int topstars = 10; topstars >= (10 - count); topstars--)
cout << "*";
// bottom right triangle
for (int otherspaces = 0; otherspaces >= 10; otherspaces++)
cout << " ";
for(int otherstars = 10; otherstars >= (10 - count); otherstars--)
cout << "*";
cout << endl;
}
// BOTTOM HALF OF DIAMOND!
for (int count2 = 0; count2 <=10; count2++)
{
//Bottom left triangle
for (int spaces = 0; spaces <= count2; spaces++)
cout << " ";
for (int stars = 0; stars <= (10 - count2); stars++)
cout << "*";
// bottom right triangle
for (int endspaces = 10; endspaces <= 0; endspaces--)
cout << " ";
for(int endstars = 0; endstars <= (10 - count2); endstars++)
cout << "*";
cout << endl;
}
return 0;
}
Thanks a lot for the code, thats a little to advanced for where im at right now… so i took the n00b way … previously in the excersizes we had to make triangles out of asterisks…4 different onces. i realized putting them together made a diamond shape…so thats just what i did LOL… check out my code
#include <iostream>
using namespace std;
int main()
{
// TOP HALF!
for (int count = 0; count <=10; count++)
{
//Bottom left triangle
for (int topspaces = 10; topspaces >= count; topspaces--)
cout << " ";
for (int topstars = 10; topstars >= (10 - count); topstars--)
cout << "*";
// bottom right triangle
for (int otherspaces = 0; otherspaces >= 10; otherspaces++)
cout << " ";
for(int otherstars = 10; otherstars >= (10 - count); otherstars--)
cout << "*";
cout << endl;
}
// BOTTOM HALF OF DIAMOND!
for (int count2 = 0; count2 <=10; count2++)
{
//Bottom left triangle
for (int spaces = 0; spaces <= count2; spaces++)
cout << " ";
for (int stars = 0; stars <= (10 - count2); stars++)
cout << "*";
// bottom right triangle
for (int endspaces = 10; endspaces <= 0; endspaces--)
cout << " ";
for(int endstars = 0; endstars <= (10 - count2); endstars++)
cout << "*";
cout << endl;
}
return 0;
}