Thread: How can I calculate average per user ID

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    8

    How can I calculate average per user ID

    I am new to C programming and I have to print content from a file with an average already calculated. I will set the file here to show you how it is displayed. AtividadesPraticadas.txt
    I want to calculate the average per line of the second last values (23, 45, 300, ....). I know that there are not many users to get a proper value but what can I do to let the program know that I only want to divide that value/values for the correspondent ID ?

    Code:
    0001; 12-07-2022; Marcha; 23; 2km
    0003; 14-02-2022; Corrida; 45; 4km
    0002; 23-05-2022; Btt; 300; 40km
    0001; 10-09-2022; Natacao; 30; 250m
    0004; 08-03-2022; Marcha; 45; 5km
    0005; 29-06-2022; Escalada; 50; 2km
    0006; 15-10-2022; Marcha; 35, 3km
    Last edited by Salem; 1 Week Ago at 11:07 PM. Reason: Inlined the text

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,322
    Are you able to read each line into a buffer using say fgets?

    So which of these functions have you come across?
    - sscanf
    - strchr
    - strtok

    Any of these can be used to split the line into fields.

    As a first step, read the file and print the field of interest.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Hi, yes I think so. I will put here what I have.
    How can I calculate average per user ID-1-pngHow can I calculate average per user ID-2-pngHow can I calculate average per user ID-3-png

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,017
    Dont use images... Put your code inside 'code' markdown: [ code ] and [ /code ] (without spaces inside [ and ]).

  5. #5
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Oh sorry.

    Code:
    double getAv (activitiesDone participants[500], int max)
    {
    
    
        int i;
        double total, average;
        total = 0;
        for (i=0; i<=max; i++)
        {
            total += participants[i].time;
        }
    
    
        average = total / max;
        return average;
    }
    
    
    avgParticipantsTime() {
        FILE *fRead;
        char line[150];
        activitiesDone data[7];
        int i = 0;
    
    
        fRead = (fopen("AtividadesPraticadas.txt", "r")); // read only
    
    
        // while (true)
        while (1)
        {
            // STEP 2
            if (fgets(line, 150, fRead) == NULL) break;
    
    
            // STEP 3-4 - strtok() breaks str into a serie of tokens using a delimiter ";"
            char * pch;
            pch = strtok (line,";");
            data[i].nPraticante = atoi(pch);  // item part
            pch = strtok (NULL, ";");
            strcpy(data[i].date, pch); // unit price part
            pch = strtok (NULL, ";");
            strcpy(data[i].activity, pch); // purchase date part
            pch = strtok (NULL, ";");
            data[i].time = atoi(pch);
            pch = strtok (NULL, ";");
            strcpy(data[i].distance, pch);
            
    
    
            i++;
        }
    
    
        setlocale(LC_ALL, "Portuguese");
    
    
        
    
    
        // double averageAge;
        // averageAge = getAv (&participants[50], i);
        // printf ("%.1lf\n", averageAge);
        double avarageTime;
        avarageTime = getAv(data, 7);
        printf("\n##### Participants average time #####\n");
        for (int k = 0; k < 7; k++)
        {
            printf("\n%04d; %.1lf\n", data[k].nPraticante, avarageTime);
        }
    
    
        printf ("\n");
            
    
    
        fclose(fRead);
        return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,322
    Looked OK, apart from

    > for (i=0; i<=max; i++)

    It should be <, not <=
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    It works but I want to apply that function to calculate the average in each ID and not the total value. For example average for ID 0001, 0002, 0003 and so on

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,322
    > It works but
    Try again.
    Your array has 7 elements, but you loop 8 times.

    > but I want to apply that function to calculate the average in each ID and not the total value.
    So perhaps your function should be
    Code:
    double getAvForID (int id, activitiesDone participants[], int max) {
      for ( int i = 0 ; i < max ; i++ ) {
        if  ( data[i].nPraticante == id )
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Thank you, now it outputs the value per ID!
    But now my biggest problem is that the total amount of time calculated is being divided by the value of its ID and not by the number of times that ID appears in the file. What can I do now? Count the occurrences?

  10. #10
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Thanks for your help!! Already solved the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to calculate a running average
    By knik653 in forum C Programming
    Replies: 18
    Last Post: 11-11-2018, 11:57 AM
  2. Replies: 2
    Last Post: 10-05-2016, 06:37 PM
  3. How to calculate the average of scores?
    By david.jones in forum C Programming
    Replies: 4
    Last Post: 05-02-2011, 06:33 AM
  4. calculate average
    By archriku in forum C Programming
    Replies: 23
    Last Post: 04-10-2009, 04:27 AM
  5. calculate average from a file
    By mrsirpoopsalot in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2009, 02:25 PM

Tags for this Thread