Hi all, brand newbie here

...but it looks like you guys can help!

I have to do this assignment in my class and all they are asking me to do is use for statements to create this shape on the screen.

*
**
***
****
*****
******
*******
********
*********
**********

**********
*********
********
*******
******
*****
****
***
**
*

**********
*********
********
*******
******
*****
****
***
**
*

*
**
***
****
*****
******
*******
********
*********
**********
Press any key to continue . . .


I have been able to do other assignments with fair ease, but this one is stumping me. Our last assignment was to use if statements, which went over well. I am not sure what to do to make this one happen though. (I have been buried in my book for a few hours now, and all of the for statement examples have to do with counting numbers and adding them up, not inputting asterisks to the screen to create shapes. Go figure)

This is all that I have so far. I know it is far from the end product.....

#include <iostream>
using namespace std;

int main()
{

int a;
int b;


cout << '*' ;

for ( int a = 1; a <= 10; a++ )

if (a <= 10 )

cout << '*' ;


cout << endl;

return 0;

}


All that comes out is 10 asterisks in a row. Does anybody care to help walk me through this? I will more than likely be a little bit further along later as I am going to continue with this project, but any help is greatly appreciated. Thanks in advance!!!
Update....for some reason, the second set of triangles did not output as I typed it. They should be mirrored. (I guess there is a fixed left align on the forums)

The third triangle down should begin like this:

10 asterisks
1 space 9 asterisks
2 spaces 8 asterisks
3 spaces 7 asterisks

etc.....
You need a loop within the 'a' loop. Like so:
1
2
3
4
5
6
7
for ( int a = 1; a <= 10; a++ )
{
  for (int b = 0; b < a; b++) // if (a <= 10 ) isn't needed, it's always true!
  {
    cout << '*' ;
  }
}
This is the first half of the shape.
For the second half = second loop a hint: The above 'b' loop goes from 1...10. For the second half the b loop goes from 10...1 (consider b--)
Last edited on
Here's what I have so far. I was able to get the first two triangles to print correctly, yay. Now for the third and fourth triangle, I am seeing that I need to have the screen output spaces BEFORE the asterisks, but this too has me completely stumped. Did my question make any sense? Here is the code that I have right now. If you run it, you will notice that third triangle prints just one diagonal line of asterisks.......great, now if I could just get the rest of the asterisks to print, I think I'm in good shape!

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

int main()
{
//First Triangle
for (int i = 0; i < 10; i++) //this prints stars as columns
{
for (int a = 0; a <= i; a++) //this prints stars as rows
{
cout << "*";
}
cout << endl;
}

cout << endl;

//Second Triangle
for (int i = 1; i <= 10; i++)
{
for (int a = 10; a >= i; a--)
{
cout << "*";
}
cout << endl;
}

cout << endl;



//Third Triangle
for (int i = 0; i < 10; i++) //this prints stars as columns
{

for (int a = 0; a <= i; a++) //this prints stars as rows
{
cout << " ";
}
cout << "*";

cout << endl;
}

cout << endl;

//Fourth Triangle
for (int i = 1; i <= 10; i++)
{
for (int a = 10; a >= i; a--)
{
cout << "*";
}
cout << endl;
}
return 0;
}

I'm still having problems with creating two measly triangles. Can anybody help? :(
@magicbag2

Only had a few small problems to overcome. Try this. Works for me..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Triangles.cpp : main project file.

#include <iostream>
#include <conio.h>
#include <iomanip>

using namespace std;

int main()
{
	//First Triangle
	for (int i = 0; i < 10; i++) //this prints stars as columns
	{
		for (int a = 0; a <= i; a++) //this prints stars as rows
		{
			cout << "*";
		}
		cout << endl;
	}

	cout << endl;

	//Second Triangle
	for (int i = 1; i <= 10; i++)
	{
		for (int a = 10; a >= i; a--)
		{
			cout << "*";
		}
		cout << endl;
	}

	cout << endl;



	//Third Triangle
	for (int i = 0; i < 10; i++) //this prints stars as columns
	{

		for (int a = 0; a < 10; a++) //this prints stars as rows
		{
			if (a<i)
				cout << " ";
			else
				cout << "*";
		}
		cout << endl;
	}

cout << endl;

	//Fourth Triangle
	for (int i = 1; i <= 10; i++)
	{
		for (int a = 10; a > 0; a--)
		{
			if (a>i)
				cout << " ";
			else
				cout << "*";
		}
		cout << endl;
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.