For Nested Loop Number Set Pattern

Hello,

Beginner C++ student here, first ever programming class. The program I am working on will ask the user for input and output a number pattern for that number.

Example:

User inputs '5'

the pattern returned is:

54321
65432
76543
87654
98765

So far I have the code below, but it only outputs 54321 and stops as shown in the screenshot in the link.

https://www.dropbox.com/s/kiv2ei4k70g43yq/for_loop_numbers.jpg?dl=0

I would greatly appreciate it if anyone can ever so kindly advise what may be wrong with the loop and what can be done to fix it.

Thank you so very much!

'side' is an int and that is what is being inputted by the user (cin >> side).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


void sameNumDiagRight(int side)
{
	for (int i = side; i <= side; i++){

		for (int j = i; j  > (i - side); j--) {
			cout << j;

		}

		cout << endl;

	}

}
Last edited on
Here's what is happening. Assuming side = 5:
1
2
3
4
5
for( i = 5; i <= 5; i++)
{
   ...  do some stuff
   ... at the end of the loop increment i
   ... i = 6

Now, the next time the for loop condition is evaluated here's what it looks like:
 
for( ...; 6 <= 5; ...) // 6 is greater that 5 so the program moves on 

One quick fix that I think will work, but I didn't test would be
1
2
3
for(int i = 0; i < side; i++)
or
for(int i = side; i < side + side; i++)

Test it out. Like I said, I didn't test it myself. Hope this is helpful
Thank you!!!

The second advised function worked. Working code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void sameNumDiagRight(int side)
{
	for (int i = side; i < side + side; i++){

		for (int j = i; j  > (i - side); j--) {
			cout << j;

		}

		cout << endl;

	}

}
Topic archived. No new replies allowed.