Triangle using for loop

I am currently trying to get my code to do this.
If user enters 3:
----1 //Space = dash
--1-2
1-2-3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int num, outside, inside, space, inside2;
	cout << "Enter a num: \n";
	cin >> num;
	space = num;
	for (outside = 1; outside <= num; outside++)
	{
		for (inside2 = 1; inside2 <= space; inside2++)
		{
			cout << " ";
			space--;
		}
		for (inside = 1; inside <= outside; inside++)
			cout << inside << " ";
		cout << endl;
	}
	system("pause");
	return 0;
}


This code gives
--1----
-1-2--- //Space = dash
1-2-3--

How do I fix this??

This code under here gives

1
1 2
1 2 3

Which solves half the problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
	int num, outside, inside;
	cout << "Enter a num: \n";
	cin >> num;
	for (outside = 1; outside <= num; outside++)
	{
		for (inside = 1; inside <= outside; inside++)
			cout << inside << " ";
		cout << endl;
	}
	system("pause");
	return 0;
}


Thanks
Last edited on
In your first sample code, space should be initialised as 2*num-1, and not num (e.g., if num is 3, the line must be length 5 to get the last line right). In the first inner loop you then need to write two spaces each time, not one, and decrement space by 2, not by 1 each time.

Not sure what you intend to do when num reaches double digits.
I understand what you mean, but when I changed it to space = 2*num-1 it gave me this

----1
-1-2
-1-2-3

Still unsure how to fix...
Last edited on
Can you post the code that gave you your last result. If you have decremented space by 2 as I suggested, I don't see how it can go from 4 spaces at the front to 1.
Yea, I only changed the variable space.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int num, outside, inside, space, inside2;
	cout << "Enter a num: \n";
	cin >> num;
	space = 2*num-1;
	for (outside = 1; outside <= num; outside++)
	{
		for (inside2 = 1; inside2 <= space; inside2++)
		{
			cout << " ";
			space--;
		}
		for (inside = 1; inside <= outside; inside++)
			cout << inside << " ";
		cout << endl;
	}
	system("pause");
	return 0;
}


Would really appreciate if someone can figure this out.
So now can you change the two lines
1
2
			cout << " ";
			space--;

so that it does the other two things I suggested: write TWO spaces (not 1, as above) and decrement space by 2 (your code above decrements by 1; use the -= composite operator if you like).

Also, to make it compile on all c++ compilers, please add the header
#include <cstdlib>
or it may not allow system() to be used.

Like this??
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
#include <cstdlib> 
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int num, outside, inside, space, inside2;
	cout << "Enter a num: \n";
	cin >> num;
	space = 2*num-1;
	for (outside = 1; outside <= num; outside++)
	{
		for (inside2 = 1; inside2 <= space; inside2++)
		{
			cout << "  ";
			space-=2;
		}

		for (inside = 1; inside <= outside; inside++)
			cout << inside << " ";
		cout << endl;
	}
	system("pause");
	return 0;
}


The problem is it only works with three then...
If I plug in four I get this:
-------1
- 1 2
1 2 3
1 2 3 4

When I want this
------1
----2 3
--1 2 3
1 2 3 4

Last edited on
Whoops - my mistake entirely! Apologies!

Go back to outputting a single space in line 16.
space needs to be decremented (by 2) outside of the inside2 loop - after the
cout << endl;
line would do.

Again, please accept my apologies - I should have tried more numbers. Should work then up to num=9, but double digits are a different matter.
Topic archived. No new replies allowed.