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.
Related
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);
Can anyone help me I want my C# code to start loop and print at row number 5 in my Excel file.
Here is my current code:
DataColumnCollection dataColumnCollection = dataTable.Columns;
for (int i = 1; i <= dataTable.Rows.Count + 1; i++)
{
for (int j = 1; j <= dataTable.Columns.Count; j++)
{
if (i == 1)
excelApplication.Cells[i, j] = dataColumnCollection[j - 1].ToString();
else
excelApplication.Cells[i, j] = dataTable.Rows[i - 2][j - 1].ToString();
}
Here is my sample data:
sample data
And here is what I want the file to look like:
sample result
first of all arrays and lists the like start at 0 in most cases.
In your case if you want to start at row 5, simply increment the start with 5.
const int start = 5;
int end = datadataTable.Rows.Count;
for (int i = 0 + start; i < end; i++) // loop from 5..end
{
for (int j = 1; j <= dataTable.Columns.Count; j++)
{
if (i == 1)
excelApplication.Cells[i, j] = dataColumnCollection[j - 1].ToString();
else
excelApplication.Cells[i, j] = dataTable.Rows[i - 2][j - 1].ToString();
}
I need to add a value which would be either p1(payoff one) or p2 (payoff two) to the surrounding four neighbours of a value in a matrix that'll then be printed into a new array matrix. If it's 1 then p1 will need to be added to it's neighbours or if its 0 then p2 will be added to its neighbours. I've tried to do this approach with a nested for loop but my 'if' statement in my for loop is giving me errors and Im not sure where to go next with it.
class MainClass
{
static void Main(string[] args)
{
int m, n, i, j, p1, p2;
// rows and columns of the matrix+
m = 3;
n = 3;
//Payoff matrix
p1 = 10; //cheat payoff matrix
p2 = 5; //co-op payoff matrix
int[,] arr = new int[3, 3];
Console.Write("To enter 1 it means to co-operate" );
Console.Write("To enter 0 it means to cheat");
Console.Write("Enter elements of the Matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
arr[i, j] = Convert.ToInt16(Console.ReadLine());
}
}
Console.WriteLine("Printing Matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine();
}
// how to change the values of the matrix
int[] payoffMatrix = new int[4];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if(arr[i,j] == 1)
{
arr[i, j] = arr[i - 1, j] , arr[i + 1, j] , arr[i, j - 1] , arr[i, j + 1];
}
}
Console.WriteLine();
}
The result of the neighbouring values need to be printed into the payoff matrix aswell
If I understood correctly you need to make a copy of your array first. Because otherwise you would read e.g. "1" from a array position (i + 1) from the current iteration. That's probably not what you want.
Then you just set the desired values in your for-loop. You need some bound checking, because e.g. arrNew[i - 1] will only be accessible if i > 0
this gives you something like:
int[,] arrNew = arr.Clone() as int[,]; //creates a copy of arr
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (arr[i, j] == 1)
{
if (i > 0) //bounds checking
{
arrNew[i - 1, j] = 1;
}
if (i < m - 1) //bounds checking
{
arrNew[i + 1, j] = 1;
}
if (j > 0) //bounds checking
{
arrNew[i, j - 1] = 1;
}
if (j < n - 1) //bounds checking
{
arrNew[i, j + 1] = 1;
}
}
}
}
For a matrix:
0 0 0
0 1 0
0 0 0
the result would be
0 1 0
1 1 1
0 1 0
I have this code and I work finding squares of "0" in 2d arrays.
How can I make find rectangles and not only squares?
I'm having some difficulty doing this.
// Set first column of S[,]
for (i = 0; i < R; i++)
{
S[i, 0] = M[i, 0];
}
// Set first row of S[][]
for (j = 0; j < C; j++)
{
S[0, j] = M[0, j];
}
// Construct other entries of S[,]
for (i = 1; i < R; i++)
{
for (j = 1; j < C; j++)
{
if (M[i, j] == 0)
S[i, j] = Math.Min(S[i, j - 1], Math.Min(S[i - 1, j], S[i - 1, j - 1])) + 1;
}
}
// Find the maximum entry, and indexes of maximum entry in S[,]
max_of_s = S[0, 0];
max_i = 0;
max_j = 0;
for (i = 0; i < R; i++)
{
for (j = 0; j < C; j++)
{
if (max_of_s < S[i, j])
{
max_of_s = S[i, j];
max_i = i;
max_j = j;
}
}
}
Console.WriteLine("Maximum size sub-matrix is: ");
for (i = max_i; i > max_i - max_of_s; i--)
{
for (j = max_j; j > max_j - max_of_s; j--)
{
M[i, j] = 2;
Console.Write(M[i, j] + " ");
}
Console.WriteLine();
}
Find any size rectangles and not only squares.
I have a function to calculate the costMatrix like this code below.
public double[,] makeCostMatrixClassic(List<PointD> firstSeq, List<PointD> secondSeq)
{
double[,] costMatrix = new double[firstSeq.Count, secondSeq.Count];
costMatrix[0, 0] = Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[0]), 3);
// For D(n,1)
for (int i = 0; i < firstSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[i, 0] += Math.Round(this.getEuclideanDistance(firstSeq[j], secondSeq[0]), 3);
}
}
// For D(1,m)
for (int i = 0; i < secondSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[0, i] += Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[j]), 3);
}
}
// For D(n,m)
for (int i = 1; i < firstSeq.Count; i++)
{
for (int j = 1; j < secondSeq.Count; j++)
{
double min = this.findMin(costMatrix[i - 1, j - 1], costMatrix[i - 1, j], costMatrix[i, j - 1]);
costMatrix[i, j] = min + Math.Round(this.getEuclideanDistance(firstSeq[i], secondSeq[j]), 3);
}
}
return costMatrix;
}
For the 3rd loop (n,m), how could i improve its performance if the "count" of each Sequence is 1 million points.