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
*
**
***
****
*****
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
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.
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.
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.
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.
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?
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 );
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.