Paralleogram design?

I have a program here where I need to print out a paralellogram with the form of.
1
2
3
4
5
6
7
      
   *       
  **
 *** 
 **
 *
               

I let the user input the number of rows, IF it is even I increment it by one. Values are from 3 to 23. I am 90 percent done with the code but my problem is that
I get to the "Midpoint" where i need the stars to decrease and the space to increase from right to left its not working properly.
My paralleogram looks like this
*
**
***
****
*****
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 #include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int Rows, i, j, k;
	char Stars = '*';
	char Blankspace = ' ';
	int Starlimit = 1;
	int Shapechange;
	double blanktotal;
	int Numblanks;
	cout << "Enter the number of rows this program will output.\n";
	cin >> Rows;
	if (Rows < 3 || Rows > 23)
	{
		cout << "\nRows need to be less than 3 or greater than 23\n";
		cout << "\nRe-enter the values please.\n";
		cin >> Rows;
	}
	Numblanks = Rows / 2; //change dramatically changes at half way point of ROW TOTAL
	if (Rows % 2 == 0)
	{
		cout << "\nThe number you entered is even and will be incremented by 1 to print a parallelogram.\n";
		Rows = Rows + 1;
	}
	else
		cout << "The value you entered is odd.\n";
	for (int i = 0; i < Rows;i++)
	{

		for (int j = 0; j < Numblanks; j++)
		{
			cout << Blankspace;
		}
		for (int k = 0; k < Starlimit; k++)
		{
			cout << Stars;
		}
		Numblanks = Numblanks - 1;
		Starlimit = Starlimit + 1;
		cout << "\n";
	}
	return 0;
}
Last edited on
Your problem comes from the fact that you keep decrementing Numblanks for every row. At your halfway point, it becomes negative, so you never actually output any. The opposite happens for Starlimit.

I'd suggest just using two for loops. One for the first half of the image. One for the second half.
My instructor said 3 loops would be needed
Your examples look like triangles, not parallelogram.

What is the exact goal? One of these?
A:
*****
 *****
  *****
    *****
     *****

B:
  *
 ***
*****
 ***
  *

I want paralellogram in this form sorry my formatting was bad.
soo if i input 4 it gets pushed up to 5 which gives me 5 rows of what i want exactly like this.

1
2
3
4
5
6
7
[code]      
   *       
  **
 *** 
 **
 *
               
Last edited on
Think about these:
* How many characters you print on a line at most? Lets call that T.

* You calculate number of blanks, B. How much is T+B?

* This is legal:
1
2
3
int k = 0;
for ( ; k < a; ++k ) {}
for ( ; k < b; ++k ) {}

(But one could use while-loops instead.)

* std::min
The assignment told me not to use a while loop just 3 for loops, 1 for shape, one for row and one for stars.
I cannot use any setw manipulators.
Ok so u said figure out how many characters(stars) to print in each line, I know that in first line it always starts with one so what i have is. First line always start with "1".
 
starlimit = starlimit + 1;

this code happens AFTER the two for loops and then the follow up would be the for loop for the rows.
As for calculating the "Initial" number of blanks, that is the answer to this algorithimn.
After I find out the initial i can start incrementing it OR decrementing it depending on what shape I need.
Hint:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int Rows = // some odd number
int Numblanks = Rows / 2;
const int maxim = Numblanks + 1;
for ( int i = 0; i < Rows; ++i )
{
  const int total = // compute from maxim and Numblanks
  int j = 0;
  for ( ; j < Numblanks; ++j )
  {
    cout << ' ';
  }
  for ( ; j < total; ++j )
  {
    cout << '*';
  }
  cout << "\n";
  --Numblanks;
}
I want this BUT...
1
2
3
4
5
6
     *
    **
   ***
   **
   *
 

The number of stars being printed after the "midpoint" is correct but im trying to figure out how to move it starting from the left side as opposed to the right side.


code as of right now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	Numblanks = Rows / 2;
	const int maxim = Numblanks + 1;
	for (int i = 0; i < Rows; ++i)
	{
		const int total = maxim - Numblanks;    //compute from maxim and numblanks
		for (int j = 0; j < Numblanks; ++j)
		{
			cout << ' ';
		}
		for (int j = 0; j < total; ++j)
		{
			cout << '*';
		}
		cout << "\n";
		if ( i > Numblanks)
			++Numblanks;
		else
			--Numblanks;
	}
	return 0;
}
Last edited on
Another hint:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>

int main()
{
    int Rows = 13;
    int Numblanks = Rows / 2;
    const int maxim = Numblanks + 1;
    for ( int i = 0; i < Rows; ++i )
    {
        const int total = std::min( maxim, maxim + Numblanks );
        std::cout << std::setw(3) << ( 0 < Numblanks ? Numblanks : 0 );
        std::cout << std::setw(3) << total << '\n';
        --Numblanks;
    }
	return 0;
}
I cant use setw manipulators
Of course not. The output is there just to illustrate the actual hint. Rather than printing a "setw decorated" number you do want to print a number of blanks and stars ...
Ehh I just dont understand what u did, I see u did a min function to determine if maxim or maxim + Numblanks is bigger.
Second the tenary syntax is asking if 0 is less than Numblanks and if it is then Numblanks retains its value?.. Is this suppose to help me format the output?
std::min returns the smaller.

Lets look at the output with Rows=5:
 2 3
 1 3
 0 3
 0 2
 0 1

The first column shows how many blanks you should write at the start of the line and the second column shows blanks+stars.

More verbose:
A  B  C  D E F G
3  2  5  3 2 1 3
3  1  4  3 1 2 3
3  0  3  3 0 3 3
3 -1  2  2 0 2 2
3 -2  1  1 0 1 1

A == maxim
B == Numblanks
C == A+B
D == min( A, C )
E == blanks
F == stars
G == E+F

Do you see the pattern in the numbers?

Edit: ignore the ternary. Just like the setw, it is just a shortcut for printing the number.
1
2
3
4
5
6
7
8
// method 1
int foo = ( 0 < Numblanks ? Numblanks : 0 );
// method 2
int bar = 0;
for ( ; bar < Numblanks; ++ bar ) {
}

assert( foo == bar );
Last edited on
I'd start with a function to print a character n times:
1
2
3
4
5
void printNChars(int n, char ch) {
    while (n--) {
        cout << ch;
    }
}



Next, use two for loops to print the parallelogram: one prints the top half and the other prints the bottom half:
1
2
3
4
5
6
7
8
width = rows/2;
for (int i=1; i<width; ++i) {
    printNChars(width-i ' ');
    printNChars(i, '*');
}
for (int i=width; i; --i) {
    printNChars(i, '*');
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	maxim = (Rows - 1) / 2;
	for (int i = 0; i < Rows; ++i)
	{
		if ( i <= maxim)
		{
			blanks = maxim - i;
			total = i + 1;
		}
		else
		{
			total = (Rows + 1) - i;
		}
		for (int j = 0; j < blanks; ++j)
		{
			cout << ' ';
		}
		for (int j = 0; j < total; ++j)
		{
			cout << '*';
		}
		cout << "\n";
	}

Finished, the trick was that there was 2 main parts to the problem, first was top second was bottom.
@kesiverto thanks for the help, your first post helped a little, but to be honest I just got lost after you gave me all these clues.

@dhayden the assignment after my next one makes me incorporate this program to a function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// assert( 1 == Rows % 2 )
int Numblanks = Rows / 2;
const int maxim = Numblanks + 1;
for ( int i = 0; i < Rows; ++i )
{
  const int total = std::min( maxim, maxim + Numblanks );
  int j = 0;
  for ( ; j < Numblanks; ++j )
  {
    cout << ' ';
  }
  for ( ; j < total; ++j )
  {
    cout << '*';
  }
  cout << '\n';
  --Numblanks;
}
Topic archived. No new replies allowed.