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

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    1

    Question 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[1000];// 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
    Last edited by SKai; 11-26-2019 at 09:14 PM.

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    239
    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),
    - Input NewVoltageReading
    - Sum += NewVoltageReading

    Then find the average at the end. It would be stupid to store all the voltage values until you need to use them elsewhere.
    Last edited by Zeus_; 11-27-2019 at 12:49 AM.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    639
    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.
    Last edited by flp1969; 11-27-2019 at 04:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find average?
    By Danny.Hoshi in forum C Programming
    Replies: 17
    Last Post: 02-24-2015, 03:39 PM
  2. read n numbers and find their total, min, max, average
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 04-23-2011, 04:04 PM
  3. function to find average
    By nynicue in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 05:30 PM
  4. Read a txt and find data
    By BianConiglio in forum C Programming
    Replies: 8
    Last Post: 04-25-2004, 03:14 AM
  5. How can I find the average in the ARRAYS?
    By Nate2430 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2001, 11:21 AM

Tags for this Thread