C
Join ServerC#
help
❔ I keep getting an error " Index was outside the bounds of the array"
SStr1ke1/29/2023
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
$code
MMODiX1/29/2023
To post C# code type the following:
```cs
// code here
```
Get an example by typing
If your code is too long, post it to: https://paste.mod.gg/
```cs
// code here
```
Get an example by typing
$codegif
in chatIf your code is too long, post it to: https://paste.mod.gg/
SStr1ke1/29/2023
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;
}
SStr1ke1/29/2023
( The method )
SStr1ke1/29/2023
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");
SStr1ke1/29/2023
( The calling code )
SStr1ke1/29/2023
( It is known that there is up to numbers with three digits)
Pphaseshift1/29/2023
Easiest to $debug and check the values yourself
Pphaseshift1/29/2023
But your loop is just too big
SStr1ke1/29/2023
oh
SStr1ke1/29/2023
What are the limits to the loop? And is there a way to bypass that somehow?
Pphaseshift1/29/2023
Use the size of the arrays
SStr1ke1/29/2023
and if let's say I make an array of a 1000
SStr1ke1/29/2023
it will let me?
Pphaseshift1/29/2023
Sure
SStr1ke1/29/2023
I see, thank you!
SStr1ke1/29/2023
@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;
}
SStr1ke1/29/2023
I changed the code
SStr1ke1/29/2023
still the same error
Pphaseshift1/29/2023
Time to engage brain a bit.
Pphaseshift1/29/2023
Are crr, arr, brr all the same size?
SStr1ke1/29/2023
arr + brr length must be equal to crr length
SStr1ke1/29/2023
Else it's false
SStr1ke1/29/2023
oh wait
SStr1ke1/29/2023
yeah
SStr1ke1/29/2023
nvm
Pphaseshift1/29/2023
So you're looping over the size of crr
SStr1ke1/29/2023
yes
Pphaseshift1/29/2023
Doing crr[i]
SStr1ke1/29/2023
Wait, if like let's say the int[] size is 5
Pphaseshift1/29/2023
And brr[i]
SStr1ke1/29/2023
and I loop till like 8
SStr1ke1/29/2023
it will show error?
Pphaseshift1/29/2023
Yes, of course. That's the point of the error
SStr1ke1/29/2023
Oh
SStr1ke1/29/2023
omg
SStr1ke1/29/2023
Thank you!
Pphaseshift1/29/2023
👍🏻
AAccord1/30/2023
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.