Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/640942496630091776
deleted 13 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Creating number pattern (triangle numbers) in c++C++ with minimum loops

We were asked to make a triangular no.number pattern in c++C++ with min.minimum loops. The triangle pattern is as follows:

____1_____
___2__3____
__4__5__6__
7__8__9__10
____1_____
___2__3____
__4__5__6__
7__8__9__10

Code:

#include <iostream>

using namespace std;
int main() {
    int n=0, r=0, i=0;
    cout << "No. of rows: ";
    cin >> r;

    for( n=1; n<=r; n++) {
        for( i=1; i<=r-n; i++) {
            cout << "  ";
        }
        for( i=(n*(n-1)/2)+1; i<=(n*(n+1)/2); i++ ) {
            if( i<10 )
                cout << " " << i << "  ";
            else
                cout << i << "  ";
        }
        cout << "\n";
    }
    return 0;
}

Output:

enter image description here

Here's what I'd like reviewed:

  1. Is it wise to use pattern generating-generating formulas? for egFor example, for putting value of ii in the last loop, iI used the formula for the pattern \$1,2,4,7\$... as \$\frac{n*(n-1)}{2}+1\$. Is it efficient? This involves four operations in each iteration, and it's fairly slow.
  2. Is it possible to reduce the number of loops? Is it better to reduce variables or reduce loops?

Creating number pattern (triangle numbers) in c++ with minimum loops

We were asked to make a triangular no. pattern in c++ with min. loops. The triangle pattern is as follows:

____1_____
___2__3____
__4__5__6__
7__8__9__10

Code:

#include <iostream>

using namespace std;
int main() {
    int n=0, r=0, i=0;
    cout << "No. of rows: ";
    cin >> r;

    for( n=1; n<=r; n++) {
        for( i=1; i<=r-n; i++) {
            cout << "  ";
        }
        for( i=(n*(n-1)/2)+1; i<=(n*(n+1)/2); i++ ) {
            if( i<10 )
                cout << " " << i << "  ";
            else
                cout << i << "  ";
        }
        cout << "\n";
    }
    return 0;
}

Output:

enter image description here

Here's what I'd like reviewed:

  1. Is it wise to use pattern generating formulas? for eg, for putting value of i in last loop, i used the formula for the pattern \$1,2,4,7\$... as \$\frac{n*(n-1)}{2}+1\$. Is it efficient? This involves four operations in each iteration, and it's fairly slow.
  2. Is it possible to reduce the number of loops? Is it better to reduce variables or reduce loops?

Creating number pattern (triangle numbers) in C++ with minimum loops

We were asked to make a triangular number pattern in C++ with minimum loops:

____1_____
___2__3____
__4__5__6__
7__8__9__10

Code:

#include <iostream>

using namespace std;
int main() {
    int n=0, r=0, i=0;
    cout << "No. of rows: ";
    cin >> r;

    for( n=1; n<=r; n++) {
        for( i=1; i<=r-n; i++) {
            cout << "  ";
        }
        for( i=(n*(n-1)/2)+1; i<=(n*(n+1)/2); i++ ) {
            if( i<10 )
                cout << " " << i << "  ";
            else
                cout << i << "  ";
        }
        cout << "\n";
    }
    return 0;
}

Output:

enter image description here

Here's what I'd like reviewed:

  1. Is it wise to use pattern-generating formulas? For example, for putting value of i in the last loop, I used the formula for the pattern \$1,2,4,7\$... as \$\frac{n*(n-1)}{2}+1\$. Is it efficient? This involves four operations in each iteration, and it's fairly slow.
  2. Is it possible to reduce the number of loops? Is it better to reduce variables or reduce loops?
deleted 107 characters in body
Source Link
Ethan Bierlein
  • 15.9k
  • 4
  • 60
  • 146

We were asked to make a triangular no. pattern in c++ with min. loops. The triangle pattern is as follows:

____1_____
___2__3____
__4__5__6__
7__8__9__10

My code: Code:

#include <iostream>

using namespace std;
int main() {
    int n=0, r=0, i=0;
    cout << "No. of rows: ";
    cin >> r;

    for( n=1; n<=r; n++) {
        for( i=1; i<=r-n; i++) {
            cout << "  ";
        }
        for( i=(n*(n-1)/2)+1; i<=(n*(n+1)/2); i++ ) {
            if( i<10 )
                cout << " " << i << "  ";
            else
                cout << i << "  ";
        }
        cout << "\n";
    }
    return 0;
}

OUTPUT Output:

enter image description here

QUESTIONS

1) Is it wise to use pattern generating formulas? for eg, for putting value of i in last loop, i used the formula for the pattern \$1,2,4,7\$... as \$\frac{n*(n-1)}{2}+1\$. Is it efficient? I am asking this because this involves 4 operations in every iteration, which would make it prettu slow!

2) Is it possible to reduce no. of loops? is it better to reduce variables or reduce loops?

3) Can anyone show a recursive approach (or iterative)? Here's what I'd like reviewed:

THANK YOU!

  1. Is it wise to use pattern generating formulas? for eg, for putting value of i in last loop, i used the formula for the pattern \$1,2,4,7\$... as \$\frac{n*(n-1)}{2}+1\$. Is it efficient? This involves four operations in each iteration, and it's fairly slow.
  2. Is it possible to reduce the number of loops? Is it better to reduce variables or reduce loops?

We were asked to make a triangular no. pattern in c++ with min. loops. The triangle pattern is as follows:

____1_____
___2__3____
__4__5__6__
7__8__9__10

My code:

#include <iostream>

using namespace std;
int main() {
    int n=0, r=0, i=0;
    cout << "No. of rows: ";
    cin >> r;

    for( n=1; n<=r; n++) {
        for( i=1; i<=r-n; i++) {
            cout << "  ";
        }
        for( i=(n*(n-1)/2)+1; i<=(n*(n+1)/2); i++ ) {
            if( i<10 )
                cout << " " << i << "  ";
            else
                cout << i << "  ";
        }
        cout << "\n";
    }
    return 0;
}

OUTPUT

enter image description here

QUESTIONS

1) Is it wise to use pattern generating formulas? for eg, for putting value of i in last loop, i used the formula for the pattern \$1,2,4,7\$... as \$\frac{n*(n-1)}{2}+1\$. Is it efficient? I am asking this because this involves 4 operations in every iteration, which would make it prettu slow!

2) Is it possible to reduce no. of loops? is it better to reduce variables or reduce loops?

3) Can anyone show a recursive approach (or iterative)?

THANK YOU!

We were asked to make a triangular no. pattern in c++ with min. loops. The triangle pattern is as follows:

____1_____
___2__3____
__4__5__6__
7__8__9__10

Code:

#include <iostream>

using namespace std;
int main() {
    int n=0, r=0, i=0;
    cout << "No. of rows: ";
    cin >> r;

    for( n=1; n<=r; n++) {
        for( i=1; i<=r-n; i++) {
            cout << "  ";
        }
        for( i=(n*(n-1)/2)+1; i<=(n*(n+1)/2); i++ ) {
            if( i<10 )
                cout << " " << i << "  ";
            else
                cout << i << "  ";
        }
        cout << "\n";
    }
    return 0;
}

Output:

enter image description here

Here's what I'd like reviewed:

  1. Is it wise to use pattern generating formulas? for eg, for putting value of i in last loop, i used the formula for the pattern \$1,2,4,7\$... as \$\frac{n*(n-1)}{2}+1\$. Is it efficient? This involves four operations in each iteration, and it's fairly slow.
  2. Is it possible to reduce the number of loops? Is it better to reduce variables or reduce loops?
added 105 characters in body
Source Link
Max Payne
  • 359
  • 5
  • 12

We were asked to make a triangular no. pattern in c++ with min. loops. The triangle pattern is as follows:

____1_____
___2__3____
__4__5__6__
7__8__9__10

My code:

#include <iostream>

using namespace std;
int main() {
    int n=0, r=0, i=0;
    cout << "No. of rows: ";
    cin >> r;

    for( n=1; n<=r; n++) {
        for( i=1; i<=r-n; i++) {
            cout << "  ";
        }
        for( i=(n*(n-1)/2)+1; i<=(n*(n+1)/2); i++ ) {
            if( i<10 )
                cout << " " << i << "  ";
            else
                cout << i << "  ";
        }
        cout << "\n";
    }
    return 0;
}

OUTPUT

enter image description here

QUESTIONS

1) Is it wise to use pattern generating formulas? for eg, for putting value of i in last loop, i used the formula for the pattern \$1,2,4,7\$... as \$\frac{n*(n-1)}{2}+1\$. Is it efficient? I am asking this because this involves 4 operations in every iteration, which would make it prettu slow!

2) Is it possible to reduce no. of loops? is it better to reduce variables or reduce loops?

3) Can anyone show a recursive approach (or iterative)?

THANK YOU!

We were asked to make a triangular no. pattern in c++ with min. loops. The triangle pattern is as follows:

____1_____
___2__3____
__4__5__6__
7__8__9__10

My code:

#include <iostream>

using namespace std;
int main() {
    int n=0, r=0, i=0;
    cout << "No. of rows: ";
    cin >> r;

    for( n=1; n<=r; n++) {
        for( i=1; i<=r-n; i++) {
            cout << "  ";
        }
        for( i=(n*(n-1)/2)+1; i<=(n*(n+1)/2); i++ ) {
            if( i<10 )
                cout << " " << i << "  ";
            else
                cout << i << "  ";
        }
        cout << "\n";
    }
    return 0;
}

OUTPUT

enter image description here

QUESTIONS

1) Is it wise to use pattern generating formulas? for eg, for putting value of i in last loop, i used the formula for the pattern \$1,2,4,7\$... as \$\frac{n*(n-1)}{2}+1\$. Is it efficient?

2) Is it possible to reduce no. of loops? is it better to reduce variables or reduce loops?

3) Can anyone show a recursive approach (or iterative)?

THANK YOU!

We were asked to make a triangular no. pattern in c++ with min. loops. The triangle pattern is as follows:

____1_____
___2__3____
__4__5__6__
7__8__9__10

My code:

#include <iostream>

using namespace std;
int main() {
    int n=0, r=0, i=0;
    cout << "No. of rows: ";
    cin >> r;

    for( n=1; n<=r; n++) {
        for( i=1; i<=r-n; i++) {
            cout << "  ";
        }
        for( i=(n*(n-1)/2)+1; i<=(n*(n+1)/2); i++ ) {
            if( i<10 )
                cout << " " << i << "  ";
            else
                cout << i << "  ";
        }
        cout << "\n";
    }
    return 0;
}

OUTPUT

enter image description here

QUESTIONS

1) Is it wise to use pattern generating formulas? for eg, for putting value of i in last loop, i used the formula for the pattern \$1,2,4,7\$... as \$\frac{n*(n-1)}{2}+1\$. Is it efficient? I am asking this because this involves 4 operations in every iteration, which would make it prettu slow!

2) Is it possible to reduce no. of loops? is it better to reduce variables or reduce loops?

3) Can anyone show a recursive approach (or iterative)?

THANK YOU!

MathJax is love. MathJax is life.
Source Link
Morwenn
  • 20.2k
  • 3
  • 69
  • 132
Loading
Source Link
Max Payne
  • 359
  • 5
  • 12
Loading