# Thread: read data every hour and find average value in C

1. ## read data every hour and find average value in C

I have difficulties in writting a code for adding counter -for this reading voltage every hour and find average voltage value based on the number of hour that user key in.
Let's say, user key-in "10" in the number of hour then, we will read voltage for 10 hours and find the average voltage value for 10 hours.
Let's say I use "time"=3600 in counting (such as if (time==3600)). When "time" reach to 3600secs, then it will read voltage. So we get the voltage reading for one hour.
If for "100" hours or "90hours" or whatever more than 10hours, then I have to use 3600*100 = 360,000 secs.
If for "1000" hours, I have to use 3600*1000= 3,600,000 secs, which is very large integer count.
To avoid the large integer count, is there any other ways to add "counter" for reading data for large number of hours (such as read data for 1000 hours).
Can I use two variables for counter? Please correct me if I am wrong.

I have another concern, on how to equal "n" with the number of hours that user will key in, if user keyin, 30 hours then, "n" must be 30,

[/CODE]
Code:
``` int tmp,time, n;
//declare time for counting, //
//n to equal with number of hours that user keyin, //
//tmp for adding values in Volt array //

typedefstructVolt
{
doubleVoltageA;// to store Voltage-A reading for 1000 hours//
}Volt;

VoltVolt_sum,Volt_avg;// create two variable for finding average and total sum value for all the voltage-A reading keep in the array "Volt"

time++;//time will increase //
if(time==3600&& n==1000)// this is the part where i find difficulties, when user enter 1000 hours then, time count will be 3,600,000 secs, which is large integer count. //
{
Volt_sum[i]+=tmp;// summation of Voltage A value-reading will store inside using "tmp" variable
}
Volt_avg[i]=Volt_sum[i]/1000;// find average Voltage value for 1000 hours``` 2. Use unsigned long ints

Also, instead of having a stack allocated memory for storing 1000 different voltages and calculating the average, you could calculate the average by using just 16 bytes of memory (long int : 8) for storage instead of 8000 that you statically allocate for 1000 long ints.

Use 2 variables, Sum and NewVoltageReading. Let Sum = 0, NewVoltageReading = 0
Now, each time you run your loop (i.e. every hour),

Then find the average at the end. It would be stupid to store all the voltage values until you need to use them elsewhere. 3. Well... Using 'int' to hold the number of seconds is not that bad. Timestamps, for example, can hold the number of seconds since January 1st 1970 to 2038 using a 32 bits integer (time_t is traditionally an int - nowadays, a long type):

Code:
```\$ gcc -dM -E -include time.h - < /dev/null | grep -i time_t
#define __TIME_T_TYPE __SYSCALL_SLONG_TYPE
#define __time_t_defined 1```
Or:

Code:
```\$ gcc -xc -include time.h -include stdio.h - <<< 'int main(void){printf("%zu\n",sizeof(time_t));}'
\$ ./a.out
8```
@Zeus_ tip to use long ints is nice to avoid this upper limit. Popular pages Recent additions array, average value, hourly, hours, read data 