Omitting pyramid pattern rows

Good afternoon all! I'm requesting an integer and using the same integer to print a pyramid pattern using nested loops. However i'm also trying to omit the requested integer's non-factors from appearing in the patterns. So for example I request the integer 6, rows 4 and 5 would not appear in the pattern. Problem is, I don't know how to do it!

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

int main()
{
  int size = 0;

     cout << " Enter an integer: " << endl;
     cin >> size;

for(int row=0; row < size; row++)
{
    for(int col=0; col <= row; col++)
         cout << "* ";

        cout << endl;
}
cout << endl;

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
    int size = 0;

    cout << " Enter an integer: " << endl;
    cin >> size;

    for(int row=0; row < size; row++)
    {
        if (size % (row + 1) > 0) continue;
        for(int col=0; col <= row; col++)
            cout << "* ";
        cout << endl;
    }
// return 0;
}

 Enter an integer: 
6
* 
* * 
* * * 
* * * * * * 


Edit: iterate with continue;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

string operator *( int n, string s ) { return n <= 1 ? s : ( n - 1 ) * s + s; }

int main()
{
    int size;
    cout << "Enter a positive integer: ";   cin >> size;
    for( int row = 1; row <= size; row++ ) if ( !(size % row) ) cout << row * string( "* " ) << '\n';
}


Enter a positive integer: 6
* 
* * 
* * * 
* * * * * * 
Last edited on
Nice! Your solution merges several methods from the primer. I also wanted to eliminate the inner loop, alas str.append(i, '*') refuses to work with "* ". C++ is not REXX :(
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <valarray>
using namespace std;

int main()
{
    int size;
    cout << "Enter a positive integer: ";   cin >> size;
    for( int row = 1; row <= size; row++ ) if ( !(size % row) ) cout << valarray<string>( "* ", row ).sum() << '\n';
}


Enter a positive integer: 6
* 
* * 
* * * 
* * * * * * 
cout << row * string( "* " )

This is the first time I've seen that. It appears that operator*(int N,const std::string &S) returns a string with N copies of S. Is that right? I can't find this in any reference, can you point me to one?
dhayden wrote:
I can't find this in any reference, can you point me to one?


It is at the top of the code ...
string operator *( int n, string s ) { return n <= 1 ? s : ( n - 1 ) * s + s; }

Wouldn't I really love this to be in the standard!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iomanip>
#include <iostream>
#include <string>


int main()
{
    std::cout << "Please enter a positive integer: ";
    int howmany;
    std::cin >> howmany;

    for(int i { 1 }; i < howmany; ++i) {
        if ( !(howmany %i) ) {
            std::cout << std::string(i, '*') << '\n';
        }
    }
}


However the OP is required to use nested loops:
ECito wrote:
using nested loops

@Enoizat
First:
However the OP is required to use nested loops
Wrong (IMO). Pls read carefully the 2nd sentence of OP again: "I'm requesting an integer and using the same integer to print a pyramid pattern using nested loops."
I translate "I'm requesting an integer" by 'my program prompts for an integer'. Similar "using nested loops" looks for me like a description but not a mandatory way to do it.
Nothing about order/assignment/requested to... or similar. (As said before, IMO, because I am not a native speaker.)

Second:
Did you run your code?
Please enter a positive integer: 6
*
**
***

Seems you omit the last row with howmany = user input stars. In that case you may also omit the first one with howmany = 1 -- it is constantly the same for all runs. But foremost you do omit the blanks between the stars intentionally?
MikeStgt wrote:
Did you run your code?

Yes, I did.

MikeStgt wrote:
do omit the blanks between the stars intentionally?

Yes, I do.
intentionally
In that case I withdraw my remarks :)
Topic archived. No new replies allowed.