C
C#2y ago
Str1ke

❔ I keep getting an error " Index was outside the bounds of the array"

I've written a method that gets 3 arrays, in which the third one has to have all the numbers merged from the first two arrays. Then when I try to check if that works correctly it gives me that error. $code
22 Replies
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Str1ke
Str1keOP2y ago
public static bool IsMerged(int[] arr, int[] brr, int[] crr)
{
int[] zrr = new int[999];
int[] trr = new int[999];
for (int i = 0; i < 1000; i++)
{
zrr[crr[i]]++;
trr[arr[i]]++;
trr[brr[i]]++;
}

for(int x = 0; x < 1000; x++)
{
if (trr[x] != zrr[x])
return false;
}
return true;
}
public static bool IsMerged(int[] arr, int[] brr, int[] crr)
{
int[] zrr = new int[999];
int[] trr = new int[999];
for (int i = 0; i < 1000; i++)
{
zrr[crr[i]]++;
trr[arr[i]]++;
trr[brr[i]]++;
}

for(int x = 0; x < 1000; x++)
{
if (trr[x] != zrr[x])
return false;
}
return true;
}
( The method )
int[] arr = new int[4];
arr[0] = 2;
arr[1] = 4;
arr[2] = 7;
arr[3] = 5;
int[] brr = new int[3];
brr[0] = 99;
brr[1] = 4;
brr[2] = 5;
int[] crr = new int[7];
crr[0] = 7;
crr[1] = 4;
crr[2] = 5;
crr[3] = 5;
crr[4] = 4;
crr[5] = 2;
crr[6] = 99;
if (IsMerged(arr, brr, crr) == true)
Console.WriteLine("IsMerged");
else
Console.WriteLine("IsNotMerged");
int[] arr = new int[4];
arr[0] = 2;
arr[1] = 4;
arr[2] = 7;
arr[3] = 5;
int[] brr = new int[3];
brr[0] = 99;
brr[1] = 4;
brr[2] = 5;
int[] crr = new int[7];
crr[0] = 7;
crr[1] = 4;
crr[2] = 5;
crr[3] = 5;
crr[4] = 4;
crr[5] = 2;
crr[6] = 99;
if (IsMerged(arr, brr, crr) == true)
Console.WriteLine("IsMerged");
else
Console.WriteLine("IsNotMerged");
( The calling code ) ( It is known that there is up to numbers with three digits)
phaseshift
phaseshift2y ago
Easiest to $debug and check the values yourself
MODiX
MODiX2y ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
phaseshift
phaseshift2y ago
But your loop is just too big
Str1ke
Str1keOP2y ago
oh What are the limits to the loop? And is there a way to bypass that somehow?
phaseshift
phaseshift2y ago
Use the size of the arrays
Str1ke
Str1keOP2y ago
and if let's say I make an array of a 1000 it will let me?
phaseshift
phaseshift2y ago
Sure
Str1ke
Str1keOP2y ago
I see, thank you! @phaseshift
public static bool IsMerged(int[] arr, int[] brr, int[] crr)
{
if (crr.Length != arr.Length + brr.Length)
return false;
int[] zrr = new int[999];
int[] trr = new int[999];
for (int i = 0; i < crr.Length; i++)
{
zrr[crr[i]]++;
trr[arr[i]]++;
trr[brr[i]]++;
}

for(int x = 0; x < crr.Length; x++)
{
if (trr[x] != zrr[x])
return false;
}
return true;
}
public static bool IsMerged(int[] arr, int[] brr, int[] crr)
{
if (crr.Length != arr.Length + brr.Length)
return false;
int[] zrr = new int[999];
int[] trr = new int[999];
for (int i = 0; i < crr.Length; i++)
{
zrr[crr[i]]++;
trr[arr[i]]++;
trr[brr[i]]++;
}

for(int x = 0; x < crr.Length; x++)
{
if (trr[x] != zrr[x])
return false;
}
return true;
}
I changed the code still the same error
phaseshift
phaseshift2y ago
Time to engage brain a bit. Are crr, arr, brr all the same size?
Str1ke
Str1keOP2y ago
arr + brr length must be equal to crr length Else it's false oh wait yeah nvm
phaseshift
phaseshift2y ago
So you're looping over the size of crr
Str1ke
Str1keOP2y ago
yes
phaseshift
phaseshift2y ago
Doing crr[i]
Str1ke
Str1keOP2y ago
Wait, if like let's say the int[] size is 5
phaseshift
phaseshift2y ago
And brr[i]
Str1ke
Str1keOP2y ago
and I loop till like 8 it will show error?
phaseshift
phaseshift2y ago
Yes, of course. That's the point of the error
Str1ke
Str1keOP2y ago
Oh omg Thank you!
phaseshift
phaseshift2y ago
👍🏻
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server