How do i get this pattern using a nested for loop in c++?


this is the pattern. (ignore dots)

0
.1
..2
...3


this is what i have so far

int userNum = 0;
int i = 0;
int j = 0;

for ( i = 0; i <= userNum; i++)
{

for ( j >= userNum; j <= i; j++)
{
cout << i;

}
cout << endl;
}
use align, setfill and setw
http://www.cplusplus.com/reference/iomanip/setw/
http://www.cplusplus.com/reference/ios/ios_base/setf/
http://www.cplusplus.com/reference/iomanip/setfill/

Then just fill it with spaces and align it to the right, with the width being how many spaces.

Btw, cout << i; is wrong in your example because you are in the j loop, which will make it output 1, j times before it changes the value of i to 2.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;



int main ()
{
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < i; j++ ) {
            cout << " ";
        }
        cout << i << endl;
    }
    
    return 0;
}
@ pured18 the variable i is still in scope when nested inside the j loop, i is not being assigned outside of the closing }where it was first declared, so it use still usable.
Last edited on
Start by structuring problem. You need:
for x from 0 to n
    print x spaces
    print x
Let's detalise print x spaces:
for a from 0 to x
    print space
Combining with previous:
for x from 0 to n    
    for a from 0 to x
        print space
    print x
Last edited on
thanks everyone! :)
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 ()
{
int num;
cout << "How far do you want this program to count minus one?\n";
cin >> num;
cout << endl;
    for (int i = 0; i < num; i++) {
        for (int j = 0; j < i; j++ ) {
            cout << ".";
        }
        cout << i << endl;
    }
    return 0;
}
Pured wrote:
Btw, cout << i; is wrong in your example because you are in the j loop, which will make it output 1, j times before it changes the value of i to 2.
novellof wrote:
@ pured18 the variable i is still in scope when nested inside the j loop, i is not being assigned outside of the closing }where it was first declared, so it use still usable.


@jasonwynn10 cout << "How far do you want this program to count minus one?\n"; ugh..why would you have the user do basic math you could do in the program :P That doesn't really make sense since you are doing [0,num) so it should iterate num times.

The best way I think would be what Pured mentioned and use setw and setfill

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> //std::cout, std::cin, std::endl;
#include <iomanip> //std::fill, std::setw

int main()
{
    unsigned rows = 0u;
    std::cout << "Please enter the number of rows: ";
    std::cin >> rows;
    
    std::cout << "Begin pattern...\n";
    std::cout << std::setfill(' ');
    
    for(unsigned row = 0u; row < rows; ++row)
    {
        //keep in mind the column for number is same as row
        std::cout << std::setw(row+1) << row << std::endl;
    }
    
    std::cout << "End pattern..." << std::endl;
    
    return 0;
}
Please enter the number of rows: 4
Begin pattern...
0
 1
  2
   3
End pattern...


*just read that the OP said ignore dots so I replaced '.' with ' '

You can alternatively use std::right
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> //std::cout, std::cin, std::endl;
#include <iomanip> //std::right, std::setw

int main()
{
    unsigned rows = 0u;
    std::cout << "Please enter the number of rows: ";
    std::cin >> rows;
    
    std::cout << "Begin pattern...\n";
    std::cout << std::right;
    //keep in mind the column for number is same as row
    for(unsigned row = 0u; row < rows; ++row)
    {
        std::cout << std::setw(row+1) << row << std::endl;
    }
    
    std::cout << "End pattern..." << std::endl;
    
    return 0;
}
Please enter the number of rows: 4
Begin pattern...
0
 1
  2
   3
End pattern...


*edit changed jasonwynn10 to jasonwynn10 :P
Last edited on
you splelled my username wrong @giblit
Topic archived. No new replies allowed.