C
C#

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
MMODiX1/29/2023
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/
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;
}
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)
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 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 it will let me?
Pphaseshift1/29/2023
Sure
SStr1ke1/29/2023
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
Pphaseshift1/29/2023
Time to engage brain a bit. Are crr, arr, brr all the same size?
SStr1ke1/29/2023
arr + brr length must be equal to crr length Else it's false oh wait yeah 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 it will show error?
Pphaseshift1/29/2023
Yes, of course. That's the point of the error
SStr1ke1/29/2023
Oh omg 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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ Having trouble with a player swap function```cs public void swap() { Vector3 LastPlayerPos = transform.position; ❔ Pannel not showingHello, I have a problem with my winform app, there is a pannel that won't show although I have this ❔ WPF game in C# having trouble attaching the "fireball" (image) to the player(ImagePlayer)https://gyazo.com/d7f93807a3085469eea5e5ac0e7a63da❔ How to adapt text to fit large items in Xamarin.Forms XAML UIHi all, slight problem here: I've got this todo app, it enters and removes todo items just fine, bu❔ LINQ build an array with an arbitrary value at the startSo, I want to make a palette that's an array of `int`s where index 0 is equal to 0 and the rest repe❔ Xamarin Android NFC App Open User Input after Button Clickwe have installed an Git Repository with an NFC Reader Write Applikation and it hase The bUtton Read❔ FluentValidation on startupWhat's not okay so that it fails with: > Unhandled exception. System.InvalidOperationException: No ❔ ✅ node in asp.netHello, I'm building asp.net MVC app and in on part I need to run some server side js code Now I tri❔ WPF Border TemplateBindingHi! I've created such style for most of my controls to have rounded corners however i would like for❔ Testing: Is there a library that can automatically populate all properties in an object?For integration tests I would love a mechanism that would grab an object, automatically populate all✅ How do i fix this?im stuck on this problem ive surfed everywhere and tried everything i could but i just cant seem to ❔ fluentvalidation custom validation inherit existing validation `MinimumLengthValidator`Hello i trying to create custom validation by inherit fluentvalidator build in validator `MinimumLen❔ Bootstrap modal in razor page closing for one page but not anotherWhen I click the plus button on my application a modal pops up. (good) But when I click any button i✅ Design pattern for a method feels incorrectI have made a small CLI application that does a few things related to my job. The CLI downloads the ❔ GraphicsObjective. Write a program to draw the drawing, shown in Fig. 3 - equilateral triangle located in th❔ This platform does not allow the automatic selection of an algorithm.hey guys when i run my project to a game i get this error This platform does not allow the automatichow do i know what level my treeviewitem is currently at?im using wpf❔ Blazor server run in browser/clientHi! I have some code that *needs* to run in the browser and not in the server. (Constantly updating ❔ MySQL publishing questionI'm working on a habit tracker using c# and mysql and i was about to publish it and send it off to gHelp understanding "get" and "set"i used to learn c++, now that get and set is really confusing... can it be explained in a way i unde