Thursday, October 9, 2008

Matrix output with C#

namespace OutPut
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("input row number...");
int input = int.Parse(Console.ReadLine());
ShowNumber(input);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~");
}

public static void ShowNumber(int RowNumber)
{
int index = RowNumber - 1;
int[,] Arr = new int[RowNumber,RowNumber];

if (RowNumber <= 0)
{
Console.WriteLine("the row number must be bigger than 0");
}
else if (RowNumber == 1)
{
Arr[0, 0] = 1;
}
else if (RowNumber == 2)
{
Arr[0, 0] = 1;
Arr[1, 0] = 1;
Arr[1, 1] = 1;
}
else
{
Arr[0, 0] = 1;
Arr[1, 0] = 1;
Arr[1, 1] = 1;
for (int row = 0; row < RowNumber; row++)
{
for (int col = 0; col <= row; col++)
{
if (col == 0)
{
Arr[row, 0] = 1;
}
else if (col == row)
{
Arr[row, col] = 1;
}
else
{
Arr[row, col] = Arr[row - 1, col] + Arr[row - 1, col - 1];
}
}
}
}

for (int i = 0; i < RowNumber; i++)
{
for (int j = 0; j <= i; j++)
{
if (j == i)
{
Console.WriteLine(Arr[i, j]);
}
else
{
Console.Write(Arr[i,j]+" ");
}
}
}
}
}
}

// belowing is the output with 6 input

No comments: