7. Pascal's triangle

The following method generates the indicated number of rows in Pascal's triangle:

// Make a Pascal's triangle with the desired number of rows.
private List<List<int>> MakePascalsTriangle(int numRows)
{
// Make the result list.
List<List<int>> triangle = new List<List<int>>();

// Make the first row.
List<int> prevRow = new List<int>();
prevRow.Add(1);
triangle.Add(prevRow);

// Make the other rows.
for (int rowNum = 2; rowNum <= numRows; rowNum++)
{
// Make the next row.
List<int> newRow = new List<int>();
newRow.Add(1);
for (int colNum = 2; colNum < rowNum; colNum++)
{
newRow.Add(
prevRow[colNum - 2] +
prevRow[colNum - 1]);
}
newRow.Add(1);

// Prepare for the next row.
triangle.Add(newRow);
prevRow = newRow;
}
return triangle;
}

This method creates a result list and then adds the top row of the triangle containing the single value 1.

It then enters a loop to calculate each of the remaining rows from the rows above them in the result list.

Alternatively, you could use one of the methods in Solution 6. Binomial Coefficients, to calculate each of the triangle's binomial coefficients directly.

The following method converts a triangle's entries from a List<List<int>> into a multiline string:

// Convert a Pascal's triangle into a string
private string TriangleToString(List<List<int>> triangle
{
StringBuilder sb = new StringBuilder();
foreach (List<int> row in triangle)
{
sb.AppendLine(
string.Join(" ",
row.ConvertAll(i => i.ToString())));
}
return sb.ToString();
}

This method creates a StringBuilder and then loops through the triangle's rows. It uses the ConvertAll LINQ extension method to convert the row's values into strings, uses string.Join to concatenate those strings into a single space-delimited string, and adds the row's space-delimited values to the StringBuilder.

After it processes all of the rows, the method returns the text in the StringBuilder.

The following screenshot shows the PascalsTriangle example solution, displaying the first 10 rows of Pascal's triangle:

Displaying Pascal's triangle graphically is a bit more challenging. The PascalsTriangleGraphical example solution uses the following code to draw Pascal's triangle:

// The Pascal's triangle.
private List<List<int>> PascalsTriangle = null;
// Draw the triangle on the PictureBox.
private void DrawTriangle(Graphics gr, int cx)
{
gr.Clear(Color.White);
if (PascalsTriangle == null) return;
gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
gr.SmoothingMode = SmoothingMode.AntiAlias;

// The size of an item.
const int wid = 30;
const int hgt = 30;
const int margin = 2;
// Make a StringFormat to center text.
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;

// Make a font to use.
using (Font font = new Font("Segoe", 9))
{
int y = 4;

// Draw each row.
int numRows = PascalsTriangle.Count;
for (int rowNum = 1; rowNum <= numRows; rowNum++)
{
// Start on the left so the row is centered.
int x = cx - rowNum * wid / 2;

// Draw the items in this row.
List<int> row = PascalsTriangle[rowNum - 1];
for (int colNum = 1; colNum <= rowNum; colNum++)
{
Rectangle rect = new Rectangle(
x + margin, y + margin,
wid - 2 * margin, hgt - 2 * margin);
gr.DrawString(row[colNum - 1].ToString(),
font, Brushes.Blue, rect, sf);
gr.DrawEllipse(Pens.Black, rect);
x += wid;
}
y += hgt;
}
}
}
}

The PascalsTriangle variable holds the solution found by the previous MakePascalsTriangle method.

The DrawTriangle method first performs some setup chores. It makes a StringFormat object that it can use to center text in a rectangle and creates a font. It then initializes the variable y to indicate the vertical position of the first row.

The method then loops through the triangle's rows. For each row, the method calculates the row's width, divides that by 2, and subtracts the result from the X coordinate of the center of the PictureBox. That gives the X coordinate where the program must start drawing the row in order to center it horizontally.

Next, the code loops through the row to draw its values, updating the variable x after each.

To draw a value, the code makes a rectangle at position (x, y), and with the width and height given by the constants wid and hgt. It offsets the position and dimensions to make a margin between values.

This method draws the value centered inside the rectangle and then uses the same rectangle to draw an ellipse around the value.

The following screenshot shows the PascalsTriangleGraphical program in action:

Note that the program doesn't try to adjust the size of the ellipses depending on the length of the values.  For example, the 20th row of the triangle contains the value 92,378, which won't fit in the ellipses drawn by the preceding code. Modifying the program to handle larger values would make an interesting exercise, so I encourage you to give it a try.

Download the PascalsTriangle and PascalsTriangleGraphical example solutions to see additional details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset