Loops within loops

Hi, so in my Uni course I've been given this code that will be below and they have asked me to "understand" it and change it twice so the outputs looks like these

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

and again but like this

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

The problem is I have no clue how to do this, I understand what the code is doing, but to make it do that is just so confusing and annoying, I asked the Lecturer but he just said a bunch of words that went over my head and when I asked to explain better he left to help another student..

ANY HELP WOULD BE AMAZING!!!! if you could explain how the code works to make those two examples would be great!!!

P.S do people tutor on here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>

using namespace std;

int main(void)
{
	for (int row = 0; row < 10; row++) {

		cout << "\n\t";

		for (int col = 1; col<4; col++) {
			cout << row*col << "";
		}
	}

	cout << "\n\n";

	system("pause");
	return 0;
}
Last edited on
Heres a start. Im not very good of this but I want to be.

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>

using namespace std;

char* star = "*";

int main()
{
	
	for (int row = 0; row < 10; row++) {

		cout << "*" << "\n\t";
		for (int col = 1; col < 10; col++) 
		{
			cout << star << ""; 
		}

	}
	
	cout << "\n\n";

	return 0;

}
@patricc48, tried the code but it didn't work. I've managed to get the first part working, but I cannot get the second one working

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
#include <iostream>

using namespace std;

int main(void)
{
	for (int row = 0; row < 10; row++) {

		cout << "\n\t";

		for (int col = 1; col<row+1; col++) {
			cout << "*"; 
		}
	}

	for (int row = 10; row > 0; row--) {

		cout << "\n\t";

		for (int col = 1; col>row - 1; col--) {
			cout << "*";
		}
	}

	cout << "\n\n";

	system("pause");
	return 0;
}


any help would be great.
Last edited on
Don't do the work for him, he won't learn that way. The only way to learn it to try it and see what the results are. He needs to make an attempt at it, or speak to his lecturer again.
@ajh32 one he did not do the work for me, merely suggested a way that he would do for me to study and understand the code, as a matter of fact after 2 hours of just tinkering with the code myself and a lot of luck I've mentioned to do both of them.

don't make assumptions like I need to make an attempt when I clearly showed that I have. if you are not here to help in assisting me do not bother looking or typing a message.
1
2
3
4
5
6
7
8
9
10
11
12
13
	for (int row = 0; row < 10; row++) {
		for (int col = 0; col<row; col++) {
			cout << "*"; 
		}
		cout << "\n\t";
	}

	for (int row = 10; row > 0; row--) {
		for (int col = 0; col>row; col--) { // This one you need to edit
			cout << "*";
		}
		cout << "\n\t";
	}


To solve it, you can't have more cols than rows (1st row has 1 star, 2nd has 2 and so on..) and reversed your first row (row = 10;) can't have more cols than that.

Don't know if that makes it any clearer for you.
@Jezze Yeah thanks mate, after just tinkering around deleting and changing stuff randomly I was able to get the result I need, granted would have preferred to have come to the correct answer through intelligence means rather than guess work but I understand it better now.
Topic archived. No new replies allowed.