-1

i´d like to build a pascal´s triangle as a square matrix in c# like this.

1 0 0 0 0
1 1 0 0 0
1 2 1 0 0
1 3 3 1 0
1 4 6 4 1

But the following code didn´t perform, could you please help me?

Console.Write("Size of Matrix: ");
        int size = Convert.ToInt32(Console.ReadLine());

        int[,] pascal = new int[size, size];

        for (int i = 0; i < pascal.GetLength(0);i++)
        {
            for (int j = 0; j < pascal.GetLength(1); j++)
            {
                if (j > i )
                {
                    pascal[i, j] = 0;
                }
                if (i == j || j == 0)
                {
                    pascal[i, j] = 1;             
                }
                if (i !=j)
                {
                    pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
                }           
                Console.Write($"{pascal[i,j ],5 }");
            }
            Console.WriteLine();
        }

thx

Cid
  • 14,968
  • 4
  • 30
  • 45
danbe
  • 1
  • 6
    What does "didn't perform" mean? – Enigmativity Nov 26 '20 at 21:47
  • 1
    For what it's worth, I'd never heard of Pascal's triangle, but I do recognize that number pattern. It's `11` to various powers (11^0=1, 11^1=11, 11^2=121, 11^3=1331, etc). Perhaps you could write a simple digit extractor and use that info. – Flydog57 Nov 26 '20 at 21:58
  • 1
    @Enigmativity it means it wasn't very good at musical showboating.. after all, the only thing it could play was the triangle. Ba dum tish (ting?) – Caius Jard Nov 26 '20 at 21:59
  • @Flydog57 Unfortunately, that would fail for the very next row: `1, 5, 10, 10, 5, 1`. – Phylogenesis Nov 26 '20 at 22:24
  • @Flydog57 It's the coefficients of the terms of (x +1)^n. – juharr Nov 26 '20 at 23:03

4 Answers4

1

Your problem is your ifs, need to be else if otherwise you will be trying to reference negative indexes in the array with pascal[i - 1, j - 1] etc.

for (int i = 0; i < pascal.GetLength(0); i++)
{
   for (int j = 0; j < pascal.GetLength(1); j++)
   {
      if (j > i)
         pascal[i, j] = 0;
      else if (i == j || j == 0)
         pascal[i, j] = 1;
      else if (i != j)
         pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];

      Console.Write($"{pascal[i, j],5}");
   }

   Console.WriteLine();
}

Another way you could achieve this is with good old fashioned math

for (var i = 0; i < rows; i++)
for (var j = 0; j <= i; j++)
   if (j != 0 && i != 0)
      pascal[i, j] = val = val * (i - j + 1) / j;
   else
      pascal[i, j] = 1;

To Output

for (var i = 0; i < rows; i++)
{
   for (var j = 0; j < rows; j++)
      Console.Write($"{pascal[i, j]} ");
   Console.WriteLine();
}
halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
1

You are forgot else if and rewrite data if (i != j)

Console.Write("Size of Matrix: ");
int size = Convert.ToInt32(Console.ReadLine());

int[,] pascal = new int[size, size];

for (int i = 0; i < size; i++)
{
    for (int j = 0; j < size; j++)
    {
        if (i == j || j == 0)
        {
            pascal[i, j] = 1;
        }
        else if (j > i)
        {
            pascal[i, j] = 0;
        }
        else if (i != j)
        {
            pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
        }
        
        Console.Write($"{pascal[i, j],5 }");
    }
    Console.WriteLine();
}
Stanislav
  • 459
  • 3
  • 6
0

A new array always contain zeros. You could seed the 1's outside the loops, then only loop through the rest.

I would also separate the construction of the array, from the printing method. If you only wanted to print it, you'd only need one array.

int[,] pascal = new int[size, size];
pascal[0, 0] = 1;
for (int i = 1; i < size; i++)
{
    pascal[i, 0] = pascal[i, i] = 1;
    for (int j = 1; j <= i - 1; j++)
        pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
}
Jeremy Lakeman
  • 9,515
  • 25
  • 29
0

To help to grasp this dynamic programming concept the following is my recursive solution.

for(var i = 0; i < 5; ++i)
{
    for(var j = 0; j < i + 1; ++j)
    {
        Console.Write($"{p(i, j)} ");
    }
    Console.WriteLine();
}

static int p(int i, int j) 
    => (j == 0 || i == j) 
        ? 1 
        : p(i - 1, j) + p(i - 1, j - 1);