This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

array adding int.

I'm trying to fill an array with a rotating counts then add the all up each time the sub is called. When I simulate this code I never see the i++ index. I guess keil supports i++ by itself.

i++;

if(i == 10){i == 0;}

array[i] = data;

data = 0;

for (a = 0; a < 10 ; a++)
	{data = data + array[i];}

Parents
  • i++; // What's this doing here?
         // You're incrementing i, but haven't initialised it
    
    if(i == 10){i == 0;} // Should be i=0 ?
    
    array[i] = data;  // you're putting an uninitialised value from data into one element of array
                      // i was uninitialised, so it could be anything now
                      // (because of the preceding error, i could even be 10!)
    
    data = 0;         // having just used data, you now initialise it!
    
    for (a = 0; a < 10 ; a++)
    	{data = data + array[i];} // you repeat this assignment 10 times
                                      // still using the undefined value of 'i' each time
    
    Note that this won't even compile, as data is a reserved keyword in C51!

Reply
  • i++; // What's this doing here?
         // You're incrementing i, but haven't initialised it
    
    if(i == 10){i == 0;} // Should be i=0 ?
    
    array[i] = data;  // you're putting an uninitialised value from data into one element of array
                      // i was uninitialised, so it could be anything now
                      // (because of the preceding error, i could even be 10!)
    
    data = 0;         // having just used data, you now initialise it!
    
    for (a = 0; a < 10 ; a++)
    	{data = data + array[i];} // you repeat this assignment 10 times
                                      // still using the undefined value of 'i' each time
    
    Note that this won't even compile, as data is a reserved keyword in C51!

Children
  • I did post an error when I posted the

    {i == 0;}
    your right it should be
    {i=0;}
    . This was only part of my sub and I did initialize my variables.I used the word data in my posting I actually used the word refreq in my program. Next time I post I'll stick with my code and not change it trying to simplify it for understanding. By racking my brains out I did notice the code was working and the simulator just wasn't showing me the results. The simulator was only updating the results when I minimized the maximized my window.

    Thank you for your fee back

  • "The simulator was only updating the results when I minimized the maximized my window."

    Have you checked 'Periodic Window Update' on the 'View' menu?