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
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();
}
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();
}
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];
}
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);
I'm trying to write a gold prospecting program that takes an initial data map in the form of a 2D array which then produces a map with all the likely places for gold marked on it.
However, when calculating the average to determine whether or not to mark the point for prospecting, I get a "System.IndexOutOfRangeException" exception thrown at me and the program breaks. How would I go about fixing this? Thank you for any help in advance.
for (int i = 1; i < nRows; i++)
{
for (int j = 1; j < nCols - 1; j++)
{
//it is at the line below where the program breaks
double average = (data[i - 1, j] + data[i + 1, j] + data[i, j - 1] + data[i, j + 1]) / 4;
if (data[i, j] > average)
{
map[i, j] = "*";
}
}
}
You go out of borders of your 2-D array. So change this part of your code:
for (int i = 1; i < nRows; i++)
{
for (int j = 1; j < nCols - 1; j++)
to
for (int i = 1; i < nRows - 2; i++) // NOT from 0 to nRows - 1
{
for (int j = 1; j < nCols - 2; j++) // NOT from 0 to nCols - 1
so you omit the borders.
I need three columns from the datagridview to export to columns a, c, and i in excel. All the data in the dataGridView needs to be visible there, but only the first three rows need to be exported.
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
objexcelapp.Cells[1, i] = dataGridView1.Columns.Count - 1; i++ ;
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
{
objexcelapp.Cells[i + 11, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
}
If you only need the first 3 rows, then count to 3 instead of all rows. I don't understand how you ended up with all rows
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
objexcelapp.Cells[1, i] = dataGridView1.Columns.Count - 1; i++;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
{
objexcelapp.Cells[i + 11, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
}
Update
To write to excel check this post
I have an enum with six distinct values:
One
Two
Three
Four
Five
Six
which is filled from a config file (i.e. string)
Let's say someone writes into the config file any of the values
On
one
onbe
or other common misspellings/typos, I want to set the most similar value from the enum (in this case, "One") instead of throwing up.
Does C# have something like that builtin or will I have to adapt an existing Edit Distance algorithm for C# and hook that into the enum?
You can use Levinshtein distance, this tells us the number of edits needed to turn one string into another:
so just go through all values in your enum and calculate Levinshtein distance:
private static int CalcLevenshteinDistance(string a, string b)
{
if (String.IsNullOrEmpty(a) || String.IsNullOrEmpty(b)) return 0;
int lengthA = a.Length;
int lengthB = b.Length;
var distances = new int[lengthA + 1, lengthB + 1];
for (int i = 0; i <= lengthA; distances[i, 0] = i++) ;
for (int j = 0; j <= lengthB; distances[0, j] = j++) ;
for (int i = 1; i <= lengthA; i++)
for (int j = 1; j <= lengthB; j++)
{
int cost = b[j - 1] == a[i - 1] ? 0 : 1;
distances[i, j] = Math.Min
(
Math.Min(distances[i - 1, j] + 1, distances[i, j - 1] + 1),
distances[i - 1, j - 1] + cost
);
}
return distances[lengthA, lengthB];
}
I have been trying to separate two numbers using "." but in Excel it sets ",". How can I fix it?
int col= 2;
for (int i = 0; i < proc; i++)
{
for (int j = 0; j < mac; j++)
{
wse.Cells[1, col] = (i + 1).ToString()+"." + (j + 1).ToString();
col++;
}
}