Division not working properly

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
aash_1317466
Level 2
Level 2
    I'm trying to calculate the average of all values in an array. The array values get loaded from a Measure() function (not shown) and array values are constantly updated to create a running average.   
   
        
   
    There is a for loop that sums the array values. Then I'm dividing the sum by the number of array values and printing result on LCD. My problem is when i place the division directly after the summing "for" loop, the division never takes place and I end up with FlightTimeAvg = FlightTimeSum. When i moved the division line after a couple of LCD instructions I get the proper answer.   
   
        
   
    Does anyone have any ideas what could be going on?    
   
        
   
    Thanks   
   
    Aaron   
   
        
   
        
   
        
   
        
   
        
   
        
   
    while(1)        
   
     {   
   
     // Clear LCD screen   
   
     LCD_Position(0,0);          
   
     LCD_PrCString ("                ");   
   
     LCD_Position(1,0);   
   
     LCD_PrCString ("                ");   
   
        
   
     // Print "Calculating" on row 1   
   
     LCD_Position(0,0);     
   
     LCD_PrCString ("Calculating...");   
   
               
   
     // Reset Tick counter   
   
     Tick = 0;        
   
        
   
     // Delay   
   
     while(Tick < 40);   
   
        
   
     // Call Measure function   
   
     FlightTime = Measure();   
   
        
   
        
   
     // Calculate sum of FlightTime array   
   
     FlightTimeSum = 0;   
   
        
   
     for (j = 0; j < Samples; j++)   
   
     {   
   
     FlightTimeSum += FlightTime;   
   
     }   
   
        
   
     //Calculate average of array, when placed immediatelyhere   
   
     //after summing routine, the division does not   
   
     //take place and I end up with FlightTimeAvg = FlightTimeSum   
   
     //FlightTimeAvg = FlightTimeSum / Samples;   
   
        
   
     // Clear LCD row 1   
   
     LCD_Position(0,0);          
   
     LCD_PrCString ("                ");   
   
        
   
     // Print "Done" on row 1   
   
     LCD_Position(0,0);   
   
     LCD_PrCString ("Done");        
   
        
   
     //Calculate average of array, when placed here division takes    
   
     //place and get expected result   
   
     FlightTimeAvg = FlightTimeSum / Samples;   
   
        
   
        
   
     // Print FlightTime on row 2   
   
     LCD_Position(1,0);                  
   
     csprintf(LCDBuffer, "%u    ", FlightTimeAvg);   
   
     LCD_PrString(LCDBuffer);   
   
        
   
        
   
        
   
     // Reset Tick counter   
   
     Tick = 0;   
   
        
   
     // Delay   
   
     while(Tick < 40);   
   
        
   
     i++;   
   
     if (i == Samples)   
   
     {   
   
     i = 0;   
   
     }   
   
        }   
   
    }   
0 Likes
10 Replies