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

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

Children