### Totally confusing nested loop that uses blank/char patterns

Hey, so yeah, usually I have no problem with this if it was a static set variable. But the variable itself is declared in this instance, so that means any number of lines or characters can be used. I am just having a very difficult time incorporating these formulas in the while/if loop for some reason. I don't know, honestly I've never been good with math word problems, so that may be partially to blame, but here is the written instructions and what I have of the code below. Any pointers would be great.

 Title Topics Nested For loops. Description Write a program that displays a diamond pattern as shown below. It asks the user to enter the number of lines making up the pattern. Then it asks the user to enter the character used in the pattern. If the user enters an even number for the lines, the program adds one to the input value to change it to an odd value. Then it displays the diamond pattern such as shown using the character entered by the user and spanning the required odd number of lines. The diamond pattern below is shown for the character X and 7 lines. X XXX XXXXX XXXXXXX XXXXX XXX X The program does the above repeatedly so a user may ask for displaying of the pattern multiple times during one execution of the program. /* Java users */ The program ends if the user clicks cancel when prompted for pattern length. /* C++ users */ The program ends if the user enters -1 when prompted for pattern length. Testing (User input is in bold) Enter the number of lines making the pattern: 5 Enter the character making up the pattern: X X XXX XXXXX XXX X Enter the number of lines making the pattern: 7 Enter the character making up the pattern: X X XXX XXXXX XXXXXXX XXXXX XXX X Enter the number of lines making the pattern: “Click Cancel” (Java users) -1 (C++ users) Bye Formulae Formula for calculating Number 0f Front Spaces in Each Line Note that the number of the middle line can be computed from the number of total lines i.e. middle line number = (total lines + 1) / 2. In our case total number of lines is 7. So the number of the middle line is 4 i.e. (7 + 1) / 2. From knowing the middle line number and the current line number, we can calculate the number of front line spaces as below. Middle Line Number = (Total Lines + 1) / 2 Number of front spaces in a line = abs (Middle Line Number – Current Line Number) The table below shows the relationship among middle line number, current line number, and front spaces in a line for a pattern made up of 7 lines. Middle Line = (Total Lines + 1) / 2 Middle Line Current Line Front Spaces 4 1 3 4 2 2 4 3 1 4 4 0 4 5 1 4 6 2 4 7 3 For calculating absolute value, use the following function. /* Java Users */ Math.abs (number) /* C++ Users */ #include abs (number) Formula for calculating Number of Visible Printed Characters in Each Line Note that the total number of character in a line (i.e. the total number of spaces and the total number of printed characters) is always equal to the total number of lines. In our case, the total number of lines is 7 and the total number of characters (spaces and characters) per line is also 7. The total number of lines is fixed for a given pattern as it is provided by the user. So for calculating the number of printed characters in a given line, we just need to know the total number of spaces (front and back) in that line and subtract it from the total number of lines making up the pattern as below. Total Front Spaces in a line – Calculated by the formula earlier Total Spaces (front and back) in a line = 2 * Total Front Spaces in a line. Total Printed Characters in a Line = Total Lines – Total Spaces (front and back) in a line. Instructions Display the diamond pattern using a nested For loop (a For loop within a for loop). Use one outer For loop for counting the number of lines spanning the pattern. Use two inner For loops one after the other to print spaces and characters within the line. The first inner loop is used for counting the number of spaces in the front part of the line and the second inner loop is used for counting the number of characters following the spaces in the line. /* Java users */ Display the pattern on the console using System.out.print ( ). The pattern will appear aligned since System.out.print ( ) uses a monospace font. Do not use JOptionPane.showMessageDialog as it uses proportionate font as explained later below. /* C++ users */ Display the pattern on the console using cout. The pattern will appear aligned since cout uses a monospace font. Discussion Monospace Fonts In a monospace font, all characters occupy equal amount of space. For example, the letter “i” and the letter “w” occupy the same amount of space. This makes it easier to align characters in different lines one below the other. Proportionate Fonts In a proportionate font, different characters occupy different amount of space. For example, the character “i” occupy less space than the character “w”. This makes it more difficult to align characters in different lines displayed one below the other. Method System.out.print displays output on the console and uses a monospace font by default. This makes it easier to align characters on different lines one below the other. Method JOptionPane.showMessageDialog displays output in a proportionate font by default. This makes it difficult to align characters on different lines one below the other.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109`` ``````#include #include using namespace std; int main() { int totalLines; int middleLineNumber=4; char cha; char abs=''; cout <<"Enter the number of lines making the pattern" <> totalLines; cout <<"Enter the character making up the pattern" <> cha; while(totalLines>0) { for(Middle Line Number = (Total Lines + 1) / 2) { for(abs (Middle Line Number – Current Line Number)) // 1- row : repeat the process for "row" times { cout << " "; } for(abs (Total Spaces (front and back) in a line = 2 * Total Front Spaces in a line)) // 1- row : repeat the process for "row" times { cout << cha; } cout << endl; } for(Total Printed Characters in a Line = Total Lines – Total Spaces in a line) { // 1- row : repeat the process for "row" times { cout << " "; } for(col=1;col<=2*row -1;col++) // 1- row : repeat the process for "row" times { cout << cha; } cout << endl; } } cout << endl; /* for(row=1;row<=4;row++) { space = 4-row; // row + space = 4 for(col=1;col<=space;col++) // 1- row : repeat the process for "row" times cout << white; star = 2*row - 1; // star = 2*(row-1) + 1 for(col =1; col<= star; col++) cout << "*"; cout << endl; } */ return 0; } ``````

The hardest part here was to understand the question. If I interpret correctly, the output should look like this:
 ```Number of lines: 7 Which character: x x xxx xxxxx xxxxxxx xxxxx xxx x Number of lines: 4 Which character: Y Y YYY YYYYY YYY Y Number of lines: 2 Which character: W W WWW W Number of lines: -1```

The code posted has lines like this, which don't compile:
` for(Middle Line Number = (Total Lines + 1) / 2)`
It would be worth putting together come actual c++ code and trying to run it, then take it from there.
Topic archived. No new replies allowed.