MergeSort Algorithm "IndexOutOfRangeException" [duplicate] - c#

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.

Related

How to swap 2 values in a List in C#? [duplicate]

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]);

How can i read variables to an array? [duplicate]

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.

C# array wont accept "size" [duplicate]

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;

Adding array to a list how to stop it being the last iteration data? [duplicate]

This question already has answers here:
C# dictionary value clearing out when I clear list previously assigned to it....why?
(6 answers)
Closed 6 years ago.
I am trying to create a list of an array of 2 items. The problem I have is the final iteration in my loop sets all the list's arrays to the same (my guess is due to a reference?). I was wondering how I can prevent this from happening.
This is what I am trying to do:
PathTiles[] links = new PathTiles[2];
int j;
for(int i = 0; i < path.Count; i++)
{
if(i+1 < path.Count)
{
links[0] = path[i];
links[1] = path[i];
for(j = i+1; j < path.Count; j++)
{
if(path[j].x == links[1].x && Mathf.Abs(path[j].y-links[1].y) == 1)
{
links[1] = path[j];
i = j;
}
else
{
break;
}
}
validPath.Add(links);
for(int k = 0; k < validPath.Count;k++)
{
Debug.Log(validPath[k].x+", "+validPath[k].y);
}
Debug.Log("=====");
}
}
So the console shows this:
0, 0
=====
1, 1 <-- why did this become 1, 1 and not stay as 0, 0
1, 1
=====
Does any one know how I can retain the first data value and not have it overwritten? I am currently under the impression it is because it stores a reference and so I changed the values, but I don't know how to stop it doing that.
Try adding the PathTiles[] links = new PathTiles[2]; within the 1st for loop statement. As you said this is a problem of references. As you were addiing the same modified links to the list. the 1st data value is overwritten.
int j;
for (int i = 0; i < path.Count; i++)
{
PathTiles[] links = new PathTiles[2]; //<-- here
if (i + 1 < path.Count)
{
links[0] = path[i];
links[1] = path[i];
...

Cannot find duplicates in array [duplicate]

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;

Categories