C
C#2y ago
scxft

❔ How do I get around the out of bounds error when I submit this code?

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
25 Replies
Pobiega
Pobiega2y ago
$code
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/
scxft
scxftOP2y ago
BlazeBin - otkgbdkihzuq
A tool for sharing your source code with the world!
Pobiega
Pobiega2y ago
can you see what input it is thats giving out of bounds?
scxft
scxftOP2y ago
I think it's back2 = s[i - 2];
Pobiega
Pobiega2y ago
its giving the wrong result for VII btw its saying 6, not 7
scxft
scxftOP2y ago
yh ik ill fix it once this stupid out of bounds error has gone
Pobiega
Pobiega2y ago
I've never seen this approach to a roman numeral converter before, can you walk me through the idea of the bounds check?
scxft
scxftOP2y ago
So essentially I wanted the algorithm to check what the digit before/ in front is. However with the constraints of length I needed it to not check for a value when there is no value there - e.g. checking for s[-1] as it doesn't exist. That's why I attempted to use the "bound check" too solve the problem.
Pobiega
Pobiega2y ago
it seems to get a bit confused on the very first and last ones, since for VII on the first iteration, back is V (which seems confusing) ah okay so its just to prevent going OOB.. I see.
scxft
scxftOP2y ago
I just realised I think I copied the wrong code
scxft
scxftOP2y ago
BlazeBin - ljhqfykuacxo
A tool for sharing your source code with the world!
Pobiega
Pobiega2y ago
that if statement is... quite something try
if (i > 2)
{
back2 = s[i - 2];
}
if (i > 2)
{
back2 = s[i - 2];
}
instead also, instead of default to your current "digit", maybe let it default to something that wont ever be true like (char)0
scxft
scxftOP2y ago
I know Im a noob - I already tried to do that but it said that it couldnt implicitly convert it to an integer
Pobiega
Pobiega2y ago
char temp = s[i];
char forward = (char)0;
char back = (char)0;
char back2 = (char)0;
char temp = s[i];
char forward = (char)0;
char back = (char)0;
char back2 = (char)0;
then do it explicitly 🙂
scxft
scxftOP2y ago
Im sorry but how?
Pobiega
Pobiega2y ago
By reading the code sample I literally just sent?
scxft
scxftOP2y ago
Thanks - I still get the OOB error tho
Pobiega
Pobiega2y ago
did you fix the if statement?
scxft
scxftOP2y ago
yes
Pobiega
Pobiega2y ago
I rewrote your start to this:
for (int i = 0; i < s.Length; i++)
{
char temp = s[i];
char forward = (char)0;
char back = (char)0;
char back2 = (char)0;

if (i < s.Length - 1)
{
forward = s[i + 1];
}
else if (i != 0)
{
back = s[i - 1];
}

if (i > 2)
{
back2 = s[i - 2];
}
for (int i = 0; i < s.Length; i++)
{
char temp = s[i];
char forward = (char)0;
char back = (char)0;
char back2 = (char)0;

if (i < s.Length - 1)
{
forward = s[i + 1];
}
else if (i != 0)
{
back = s[i - 1];
}

if (i > 2)
{
back2 = s[i - 2];
}
and I'm not getting any OOBs
scxft
scxftOP2y ago
ah okay thanks, no OOB now
Pobiega
Pobiega2y ago
I still think this is a very... unique approach to roman numerals. Let me know if you want some tips.
scxft
scxftOP2y ago
okay, thanks
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