Parallelogram using For Loops

I'm trying to write a program to output a parallelogram. It's suppose to be of a size the user choses and with a symbol chosen by the user as well. I am able to get a triangle and know that the basic principle is to treat it as two separate parts with one upside down triangle (or at least that's a hint I was given).

How do I get the second half to print?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <iostream>

using namespace std;

int main ()

{

int i,j,rows;
char symbol;

cout << "How long do you want each side to be? ";
cin >> rows;
cout << "Please enter the character you want it to be made of: "; 
cin >> symbol;

    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           cout<< symbol;
        }
        cout<<"\n";
   
return 0;

}


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
   char symbol;
   int rows;
   cout << "Input rows: ";   cin >> rows;
   cout << "Input symbol: ";   cin >> symbol;
   for ( int i = 1 - rows; i < rows; i++ ) cout << string( (i+abs(i))/2, ' ' ) + string( rows-abs(i), symbol ) + '\n';
}

Input rows: 5
Input symbol: *
*
**
***
****
*****
 ****
  ***
   **
    *


Thank you for your help!

This is what I ended up with


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#include <iostream>
using namespace std;

int main()


{

   int n;
   char symbol;

   cout << "This program will output a parallelogram." << endl;
   cout << "How long do you want each side to be? ";
   cin >> n;
   cout << "Please enter the character you want it to be made of: ";
   cin >> symbol;

  
   for ( int i = 0 - n; i < n; i++ )
   cout << string( (i+abs(i))/2, ' ' ) + string( n-abs(i), symbol ) + '\n';
 

return 0;

}


It outputs like so:




This program will output a parallelogram.
How long do you want each side to be?  6
Please enter the character you want it to be made of:  @

@
@@
@@@
@@@@
@@@@@
@@@@@@
 @@@@@
  @@@@
   @@@
    @@
     @



Here's a couple of other obscure variants. I'm still working on a recursive version to see if I can get away without any (explicit) source-code loops.

Positional method with nested loops:
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()
{
   char symbol;
   int side;
   cout << "Input side: ";   cin >> side;
   cout << "Input symbol: ";   cin >> symbol;
   for ( int i = 1; i <= 2 * side - 1; i++ )
   {
      for ( int j = 1; j <= i; j++ )
      {
              if ( j <= i - side ) cout << ' ';
         else if ( j <= side     ) cout << symbol;
      }
      cout << '\n';
   }
}



"Windowed" strings:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main()
{
   char symbol;
   int side;
   cout << "Input side: ";   cin >> side;
   cout << "Input symbol: ";   cin >> symbol;
   string line = string( side, ' ' ) + string( side, symbol ) + string( side, ' ' );
   for ( int i = 2 * side - 1; i >= 1; i-- ) cout << line.substr( i, side ) << endl;
}


Input side: 5
Input symbol: *
*
**   
***  
**** 
*****
 ****
  ***
   **
    *


Last edited on
Topic archived. No new replies allowed.