Please follow me here!

Thursday, June 26, 2014

The Ampersand Pyramid (Java Program)

Good morning, y'all!

I hope everyone is having a fine morning! (This was written on the morning of June 26, 2014)


It has come to my attention that some newcomer programmers would be asked to print certain characters in a certain format or shape. For example, a pyramid, where you would need to use a piece of paper to see the logic behind creating a program that prints characters in the shape of a pyramid directly on the console. You may be asked to write programs to print out characters in other formats such as a four-sided diamond called a rhombus, a circle, or something where you want to get used to the logical processes you can implement in any program.

Here's what I got here. I took simply several minutes to write this 32-line Java console-based program to print an upward pyramid, depending on the max number of columns, with ampersands. The pyramids are oriented at center based on the number of characters to be printed for each line of the program. As such, you would need to use a piece of paper, or if you're smart enough, your thinking, to see how you would print your first one or two characters directly at center of the first row.

At the second row, you would add two more ampersands on either side of the first row to form the output's triangular shape. Third row, you would do the same thing, except that you'll have either five or six ampersands. The choice will always depend on whether the max number of columns is either odd or even.

Let's say you want the program to handle maximum 25 columns. This is what the pyramid looks like, with the console font set to Consolas, 10 pixels:



Here's what a 30-column pyramid looks like:



And this is the output of a pyramid with 7^2, or 49, columns:



What do you say I share with you the source code of this program, and then I'll let you know on how this works?


package consolebaseddisplayprograms;

public class TheAmpersandPyramid
{
public static void main(String[] args)
{
int squareSize = 49, rowLimit;

if (squareSize % 2 == 0)
rowLimit = squareSize / 2;
else
rowLimit = squareSize / 2 + 1;


for (int row = 0; row < rowLimit; row++)
{
for (int col = 0; col < squareSize; col++)
if (squareSize % 2 == 0)
if (col <= (squareSize / 2 + row) && col >= (squareSize / 2 - 1 - row))
System.out.print("&");
else
System.out.print(" ");
else
if (col <= (squareSize / 2 + row) && col >= (squareSize / 2 - row))
System.out.print("&");
else
System.out.print(" ");

System.out.println();
}
}
}

The way on how this program works is, assuming you changed the value assigned to the variable squareSize, the first if-else clause determines how many maximum rows are to be there in this set pyramid. In all the rows covered by this ampersand pyramid, we need to make sure that by the time we reach the last row, all the columns are covered by the ampersand. This is also to control the addition of ampersands on either side of the identical columns for every row gone through.

If squareSize is even, this means that there are two ampersands displayed on the first row. Because we add two ampersands on either side, this means that the maximum number of rows we have in printing this pyramid is half the number of maximum columns.

If squareSize is odd, though, there is a catch when it is divided by two and thus assigned to rowLimit. Dividing an odd number by two causes the decimal to be truncated, which means that we ignore the decimal, regardless of how high or low it is, and thus we only consider the whole number. If we have a max column count of 11, that means we can only print five rows, and thus by adding two ampersands on either side of the corresponding row based on the first row with one ampersand, we only end up with nine ampersands on the last row. To circumvent this problem, we simply add the truncated value with one. Thus, with six rows and starting from one ampersand, we will have 11 ampersands at the last row.

We now come to the for loops. The outer loop is to advance the rows, while the inner loop is to print the characters accordingly. We print the ampersands inside the boundaries defined by the conditional terms.

If squareSize is even, we determine whether the column is either less than or equal to half the maximum number of max columns plus the corresponding row index, or more than or equal to half the maximum number of max columns minus the sum of 1 and the corresponding row index. If it is, we print an ampersand character. If not, we simply print a space.

If squareSize is odd, we check the same, except that for the left side, we only check whether the column is greater than or equal to half the number of maximum columns minus the current row index. Printing the characters is the same process as well.

Once each row is printed, we go to the next line to print the characters there also. We keep on going until we reach the last row. Then, the program terminates.

Well, that does it for this morning blog. Have a good morning, and I'll talk to you soon!


Please follow my blogger by going to the top of this page, and click on "Join this site," big blue button with the Google logo!

Oh, and please make a donation to this campaign: http://www.gofundme.com/af3kwc
I would also appreciate if you could please spread the word; that would be awesome!
The campaign can also be found here: https://fundrazr.com/campaigns/end33

I would really appreciate if you could please make a donation, primarily for me to pay the costs for my crowdfunding promotion!
 Click here to follow me on Facebook: https://www.facebook.com/TheAspergerComputerGregDes
 Please follow me on Twitter: https://twitter.com/GregoryDes
You can also follow me on Instagram! @gregpdesig

Google+ Followers

Popular Posts by Gregory