Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple question concerning arrays (C-like language)
#1
My problem is probably too simple for words, but I can't figure out what I am doing wrong. I am currently programming in NXC (Not Exactly C) for a LEGO mindstorm robot, and my code is supposed to have the robot decide autonomously wether or not it is standing upright. This by means of comparing sensor values stored in an array. Sadly, it hasn't even come close to that point, with errors all over the place and now a runtime error ('file error' when running the program, probably caused by indexing values in nonexistant arrays - that's what googling told me).

My code:
 Spoiler

Help me please. Help will be appreciated.
Reply
#2
Your second while loop goes out of bounds.

b <= arraymax lets b grow to 10, and accessing array_distance[10] causes a problem.

Make your loops like this:

while (i < arraymax)

For all loops, that way you never run into indexing issues.

Code:
for(int i = 0; i < arraymax; i++)
{
    array_distance[i] = distance;
    Wait(50);
}

for(i = 0; i < arraymax; i++)
{
    NumOut(i * 4, LCD_LINE1, array_distance[i]); //printing array on LCD screen, parameters NumOut(x,y,value).
}
Reply
#3
Thanks, that certainly helped me out. As I said, too simple for words. Now to figure out why my sensor isn't providing the values I'd expect it to give, could it perhaps be that the ArrayInit overrules my loop?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)