Results 1 to 3 of 3
  1. Default Simple question concerning arrays (C-like language)

    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:

    Help me please. Help will be appreciated.
    Last edited by Eos; 2012-03-01 at 12:07 AM. Reason: Added CODE tags to make readable.

  2. Default Re: Simple question concerning arrays (C-like language)

    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.

    for(int i = 0; i < arraymax; i++)
        array_distance[i] = distance;
    for(i = 0; i < arraymax; i++)
        NumOut(i * 4, LCD_LINE1, array_distance[i]); //printing array on LCD screen, parameters NumOut(x,y,value).

  3. Default Re: Simple question concerning arrays (C-like language)

    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?



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts