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

average of an array

In my project i am incrementing my array 1 and want to find the average of it.

But unfortunately my code is correct when i debugged but it gives an erroneous output when print the output.

I will put my code below can anybody say what is the problem in my function?

float Arr[81][11] = 0;
float Ave[81] = 0;

This is my global declaration.

void Monitor_Slot4(void)
  {
     if(Slot_4 == 0)
      {
        Dev_Curr = fNewVREF_4 / Res;

        DevCurr_MinLimit = Dev_Curr - PassBand;

        DevCurr_MaxLimit = Dev_Curr + PassBand;

        //ROUTINE TO CHECK THE DEVICE VOLTAGE

          for(r=61;r<=80;r++)                            {
                fMonDevVol = 0.0;

               r = r + 12;

                fMonDevVol = fReadVoltage(r);

               r = r - 12;

                Arr[r][s] += 1;

            }

           if(s >= 10)
             {
                flt_Average();
                s = 0;
            }
  return;
    }

The 's' is incrmented in another function where the control returns after executing the above function.

In the above routine when i watch the value of Arr[r][s] in debug mode the value is correct. But if i try to print the value it is showing a wrong value.

Is this due to memory problem.

void flt_Average(void)
  {
     unsigned int u_min,u_max;
     unsigned int u,v;

     average = 0.0;
     Ave[0]  = 0.0;

     if(Slot_1 == 0) {u_min = 1;  u_max = 20;}
     if(Slot_2 == 0) {u_min = 21; u_max = 40;}
     if(Slot_3 == 0) {u_min = 41; u_max = 60;}
     if(Slot_4 == 0) {u_min = 61; u_max = 80;}

  //consider slot_4 is 0

     for(u=u_min;u<=u_max;u++)
        {
           for(v=1;v<=10;v++)
             {
               average += Arr[u][v];
              }
          average /= 10;

          Ave[u] = average;

          if((u >= 61) && (u <= 80))
             {
               SBUF= ' ';
               printf("\n aver is %f",Ave[u]);
               TI  = 0;

               Delay(10000);

               SBUF=' ';
               printf("\n u is %u",u);
               TI = 0;

               Delay(10000);
            }

          average = 0.0;
    }
    return;
  }

In the above function if i try to print the Ave[u], the value is not '1' since in the previous function the value is always '1'.

I do not know where is the problem is?

Can anybody suggest me where the problem is ?

0