This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 1 year ago.
I've been reading the Introduction to Algorithms and tried to implement the pseudocode, but I can't seem to pinpoint what is causing IndexOutOfRangeException.
Merge
The error happens at left[i] < right[j]
static void Merge(int[] array, int start, int middle, int end)
{
i = 1;
j = 1;
for (k = start; k < end; k++)
{
if (left[i] < right[j])
{
array[k] = left[i];
i = i + 1; // -> i++;
}
else
{
array[k] = right[j];
j = j + 1; // -> j++;
}
}
}
Solved
I solved it by setting all i and j variables to 0 and k to k = start - 1
You are getting error in this line:
if (left[i] < right[j])
since i exceeds left array length when it becomes 6 and array left has only 6 elements.
It is very easy to figure it out when you use debugging.
This question already has answers here:
Swap two items in List<T>
(6 answers)
Closed 2 years ago.
So iam new to c# and trying to making a bubble sort algorithm and was wondering is there a way to swap 2 values in a list in the simplest way?
static void BubbleSort(List<int> list)
{
for (int i = 0; i == list.Count-1; i++)
{
for (int j = 0; i == list.Count - i; j++)
{
if (list[j] > list[j+1])
{
//Need a way to swap values in position j and j+1
}
}
}
Keep it simple, use a temporary variable to momentanly store one of two values:
if (list[j] > list[j+1])
{
int temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
if (list[j] > list[j + 1])
(list[j], list[j + 1]) = (list[j + 1], list[j]);
This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 3 years ago.
I am learning C#. I know how to do this in C++ but i get an exception to this line of code :
v[i] = int.Parse(Console.ReadLine());
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
int[] v = new int[n];
for (int i = 1; i <= n; i++)
{
v[i] = int.Parse(Console.ReadLine());
}
I want to translate this from C++ to C#:
int v[1001], n;
cin>>n;
for (int i = 1; i <= n; i++)
{
cin>>v[i];
}
There is more code but i think it's irelevant for this problem.
Just to add the code which will work, alongside Christos' explanation.
Solution: Now you're also starting the loop at 0 which will not result in an exception and you won't skip the first element.
Note that here you have < not <=. That's because the last element in the array has an index of n-1.
int[] v = new int[n];
for (int i = 0; i < n; i++)
{
v[i] = int.Parse(Console.ReadLine());
}
As already mentionend, for a detailed explanation/comparison(between c++ and c#) on this see Christos' answer.
This question already has answers here:
How do I check if my array has repeated values inside it?
(8 answers)
Find duplicates between arrays
(16 answers)
Closed 8 years ago.
I need to find duplicate numbers in this method. Ive tried this, but i think im missing something. anyone can see what I'm doing wrong.
public bool FindDublicate(int[] a)
{
bool svar = true;
for (int i = 0; i < a.Length; i++)
{
for (int j = i; j < a.Length - 1; j++)
{
if (a[j] == a[j + 1])
{
svar = true;
}
else return false;
}
}
return svar;
}
If you are only interested in knowing whether your array contains duplicate or not you can do:
return a.Distinct().Count() != a.Length;
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.