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];}

  • 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!

  • 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

  • It's hard to figure things out since you didn't specify any variable types.

    i++;

    How is the variable i declared. I think you probably want it to be static and unsigned char. If it's not static you can be certain that it will not retain its value between invocations.

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

    I think you mean i = 0 instead of i == 0. Also, what happens if i = 11?

    Why not use if (i>=10) i=0;? This avoids problems if i is outside the range of valid array indexes. This comparison assumes that i is an unsigned type (which never goes negative).

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

    I understand what you're doing here, but adding up all of the array contents each time takes a long time (imagine if your array was 100 elements long). Also, since you accumulate the array contents into data, data's type should probably be larger than the array (the array unsigned char and data unsigned int) to avoid overflow problems.

    Instead of accumulating the entire array each time, you could just subtract array[i] (since it's the oldest value in the array), put the new data into array [i], and add the new data to your total.

    I would probably do something like this:

    unsigned int func (unsigned char data)
    {
    #define ARRAY_SIZE 10
    
    static unsigned char ndx = 0;
    static unsigned char array [ARRAY_SIZE] = { 0, };
    static unsigned int array_ttl;
    
    if (++i >= ARRAY_SIZE) i = 0;
    
    array_ttl -= array[ndx]; // remove oldest data
    array_ttl += data;       // add newest data
    array[ndx] = data;       // add newest data to array
    
    return (array_ttl);      // return total
    or
    return (array_ttl/ARRAY_SIZE);  // return average
    }

    This routine always takes the same amount of time to execute no matter how big the array is. It also has the advantage of being able to change the array size in a single place (the #define).

    Jon

  • for (a = 0; a < 10 ; a++)
    	{data = data + array[i];}
    "I understand what you're doing here, but adding up all of the array contents..."

    That might be what he's trying to do, but it's not what that piece of code does, is it?

    The loop uses 'a' to repeat the statement
    data = data + array[i];
    ten times.
    'i' does not change during the loop!
    Therefore, each time around the loop, it adds the same element from the array!

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

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

  • Why would you use

    unsigned char array [ARRAY_SIZE]
    to store integers in the array?