Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
So i have a problem but im not sure why.
public static int Suurin(int[,] luku)
{
int max = luku[0, 0];
for (int i = 0; i < luku.Length; i++)
{
for (int j = 0; j < luku.Length; i++)
{
if (max < luku[i, j]) // ERROR LINE
max = luku[i, j];
}
so i just dunno how to figure this out can anyone help me?
replace
for (int j = 0; j < luku.Length; i++)
with
for (int j = 0; j < luku.Length; j++)
I've made that mistake more times than I'd like to admit.
EDIT:
While what I posted above is still correct, you should be using GetUpperBound() if you're trying to get the maximum value of any item in any dimension.
int max = luku[0, 0];
for (int i = 0; i <= luku.GetUpperBound(0); i++)
{
for (int j = 0; j <= luku.GetUpperBound(1); j++)
{
if (max < luku[i, j])
max = luku[i, j];
}
}
The problem is
For two-dimensional array, its Length is multiplication of row and column (equal to total number of elements)
For example,
int[,] luku = {{1,2,3}, {2,3,4}, {2,3,4}};
luku.Length = row * column = 3*3 = 9
Another example,
int[,] luku = {{1,2,3,4}, {2,3,4,5}, {2,3,4,6}};
luku.Length = row * column = 3*4 = 12
Therefore you cannot use luku.Length in nested For loop to iterate all elements in two dimensional array.
Because you will have IndexOutofRange exception with out of range row and column index.
By using Jodrell methods,
luku.OfType<int>().Max();
You can get the maximum value of the two dimensional array with
public static int Suurin(int[,] luku)
{
int max = luku.OfType<int>().Max();
Rather than writing your own loops, you could just do
luku.OfType<int>().Max();
In general, avoid multi-dimensional arrays and use jagged arrays or some other collection. Multi-dimensional arrays suck on many levels.
Related
This question already has answers here:
Passing Arrays by Value and by Reference
(4 answers)
Copy array with `ToArray` Method
(2 answers)
Any faster way of copying arrays in C#?
(5 answers)
Closed 1 year ago.
Here is my code below-
double[][] geometricReturnsArray = new double[returnsArray.Count][];;
double[] tempGeometricReturns = new double[returnsArray[0].Count];
double return_1 = 0;
double return_2 = 0;
for (int i = 0; i < returnsArray.Count; i++)
{
for (int j = 0; j < returnsArray[i].Count - 1; j++)
{
return_1 = returnsArray[i][j + 1];
return_2 = returnsArray[i][j];
tempGeometricReturns[j] = ((return_1 - return_2) / return_2) * 100;
}
geometricReturnsArray[i] = tempGeometricReturns;
}
The issue I'm facing in the code is that tempGeometricReturns keeps getting reassigned as i increases. So in the end, all three arrays of geometricReturnsArray have exactly the same values since when tempGeometricReturns gets reassigned, so do the previous values in geometricReturnsArray.
I tried to use lists as well but then I just get a long list of 267 values whereas I'd prefer three arrays of length 90. What else can I try to do?
In your example you will end up with multiple references to the same array in the geometricReturnsArray. Arrays are reference types, so assignments only copy the reference, not the entire array.
The fix is to create a new array for each outer loop, for example by copying the existing array when doing the assignment
}
geometricReturnsArray[i] = tempGeometricReturns.ToArray();
}
or
for (int i = 0; i < returnsArray.Count; i++)
{
double[] tempGeometricReturns = new double[returnsArray[0].Count];
for (int j = 0; j < returnsArray[i].Count - 1; j++)
{
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
Below is a very simple code which sorts array of 0s, 1s and 2s. I believe it's time complexity is O(N), right? How can this algo be further improved to bring down time complexity to O(logN) or so?
//Input: 0 2 1 2 0
//Output: 0 0 1 2 2
public static int[] SortArray012(int[] array)
{
Dictionary<int, int> result = new Dictionary<int, int>(3);
int[] sortedResult = new int[array.Length];
int i = 0;
foreach(int no in array)
{
if (result.ContainsKey(no))
result[no]++;
else
result.Add(no, 1);
}
for (; i < result[0]; i++)
sortedResult[i] = 0;
for (; i < result[0] + result[1]; i++)
sortedResult[i] = 1;
for (; i < result[0] + result[1] + result[2]; i++)
sortedResult[i] = 2;
return sortedResult;
}
This is an example of counting sort. While there is no way to my knowledge to lower the asymptotic complexity you can focus on reducing the constants. For example there is no need to construct a dictionary, an array will do. If we are guaranteed that we will only see 1,2 and 0 then there is no need for the if statement. We can also generate our result with two for loops instead of three
int[] test = {1,1,0,2,1,0};
int[] count = {0,0,0};
int[] result = new int[test.Length];
foreach(int no in test){
count[no]++;
}
int i = 0;
int k = 0;
foreach(int c in count){
for(int j = 0; j < c; j++){
result[k++] = i;
}
i++;
}
This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 5 years ago.
the noob, again. todays question is:
int[] forArray = new int[10];
for (int k = 1; k <= 10; k++)
{
forArray[k] = k * 2;
Console.WriteLine(k); // test
}
for (int k = 0; k < 10; k++)
{
Console.WriteLine(forArray[k]);
}
it gives an "out of bounds" error. I would like my program to output natural numbers from 2 to 20. Instead gives off an error. when I change the first for loops condition to k <= 9 it runs but gives me 0 instead of 20. its like it returns the last value as 0 and "re-positions" it to the "front". sorry for the really simple question.
Arrays are zero-based, when it comes to referencing the elements. So, for 10 items in an array (which is what you allocated), it's forArray[0] through forArray[9]. Your code tries to loop from forArray[1] through forArray[10], and there is no index position 10 (which is when you end up going out of bounds).
Your second for-loop is fine, as it goes from 0 to 9.
Note: Since your loop needs to be zero-based, you'll need to adjust how you calculate the number you stuff into the index positions, if you want it starting with 2.
Debug your code and step through, as you see below you are trying to assign to index [10], which doesn't exist.
{
int[] forArray = new int[10];
for (int k = 1; k < 10; k++) // k < 10 instead of k <= 10
{
forArray[k] = k * 2;
Console.WriteLine(k); // test
//forArray[0] = SKIPPED
//forArray[1] = 2
//forArray[2] = 4
//forArray[3] = 6
//forArray[4] = 8
//forArray[5] = 10
//forArray[6] = 12
//forArray[7] = 14
//forArray[8] = 16
//forArray[9] = 18
//forArray[10] INVALID
}
for (int k = 0; k < 10; k++)
{
Console.WriteLine(forArray[k]);
}
}
Tested here
Change your condition to k
forArray[k - 1] = k * 2;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Does anyone know how i can have a 2d Array and add one every time he crosses the for loop?
int[,] matrix = new int[10,15];
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
// Here I am stuck to add one each time the loop passes
// for example: [0,0] = 0; [0,1]= 1; [0,2] = This should be 2
// and so it has to go on
}
}
You define a variable before the first loop and increment it every time:
int value = 0;
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
matrix[i, j] = value;
++value;
}
}
Note: But please, the next time before you ask a question do some own research as the comment by Peter Duniho suggests.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
In c# I've got four 10x10 int square matrices and I need to create a 20x20 square matrix by merging the four smaller matrices.
What is the best way to accomplish this?
EDIT: This is my code
int[] first = myArray.Take(myArray.Length / 2).ToArray();
int[] second = myArray.Skip(myArray.Length / 2).ToArray();
int[,] matrice0 = MatrixCalc(first, first);
int[,] matrice1 = MatrixCalc(first, second);
int[,] matrice2 = MatrixCalc(second, first);
int[,] matrice3 = MatrixCalc(second, second);
// Need to join these four matrices here like this: [[0 1][2 3]]
Quickly put together a simple non-scalable solution (only for 4 matrices, if you need a scalable solution you can look at having the matrix as a list of lists and concatenate them) that supposes the matrix lenght is the same. Haven't compiled it so sorry for any bugs
int[,] joinedMatrice = new int[matrice0.GetLength(0) + matrice1.GetLength(0), matrice0.GetLength(1) + matrice2.GetLength(1)];
for (int i = 0; i < matrice0.GetLength(0) + matrice1.GetLength(0); i++)
{
for (int j = 0; j < matrice0.GetLength(1) + matrice2.GetLength(1); j++)
{
int value = 0;
if (i < matrice0.GetLength(0) && j < matrice0.GetLength(1))
{
value = matrice0[i, j];
}
else if (i >= matrice0.GetLength(0) && j < matrice0.GetLength(1))
{
value = matrice1[i - matrice0.GetLength(0), j];
}
else if (i < matrice0.GetLength(0) && j >= matrice0.GetLength(1))
{
value = matrice2[i, j - matrice0.GetLength(1)];
}
else if (i >= matrice0.GetLength(0) && j >= matrice0.GetLength(1))
{
value = matrice3[i - matrice0.GetLength(0), j - matrice0.GetLength(1)];
}
joinedMatrice[i, j] = value;
}
}
You can do this:
// pre-arrange them in the form you want
List<List<int[,]>> sources = new List<List<int[,]>>() {
new List<int[,]>() {matrice0, matrice1},
new List<int[,]>() {matrice2, matrice3}
};
int[,] joint = new int[20, 20];
for (int i = 0; i < joint.GetLength(0); i++) {
for (int j = 0; j < joint.GetLength(1); j++) {
// select the matrix corresponding to value (i,j)
int[,] source = sources[i / matrice0.GetLength(0)][j / matrice0.GetLength(1)];
// and copy the value
joint[i, j] = source[i % matrice0.GetLength(0), j % matrice0.GetLength(1)];
}
}