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 <stdlib.h> 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. |
|
|
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 |
for(Middle Line Number = (Total Lines + 1) / 2)