0

Please repair my loop.

#include <iostream>
using namespace std;

int main(){
   int size;
   cout << "How large a triangle do you want? ";
   cin >> size;

   for (int r = 0; r < size; r++) {
     for (int c = 1; c <= size; c++) {
        if (c <= r) cout << " ";
        else cout << "*";
     }
     cout << endl;
   }

   return 0;
}
instead the output is:
******
 *****
  ****
   ***
    **
     *

make it like this:
     *
    **
   ***
  ****
 *****
******

thanks in advance!!

Edited by jhudielpeco: n/a

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by WaltP
0

You are on the right track. You just need to reverse how your loop goes. Look at your inner loop.

for (int c = 1; c <= size; c++)

If you reverse it from incremental loop value to decremental loop value with the range from (size-1) to 0 (inclusive), you should get the result you want.

Edited by Taywin: n/a

0

Please repair my loop.

No, you repair it? What seems to be wrong? It's upside down, right? So reverse the code properly inside the outer loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.