# Thread: How can I calculate average per user ID

1. ## 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
0006; 15-10-2022; Marcha; 35, 3km```

2. 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.

3. Hi, yes I think so. I will put here what I have.

4. Dont use images... Put your code inside 'code' markdown: [ code ] and [ /code ] (without spaces inside [ and ]).

5. 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() {
char line[150];
activitiesDone data[7];
int i = 0;

// 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");

return 0;
}```

6. Looked OK, apart from

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

It should be <, not <=

7. 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. > 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 )
}
}```

9. 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?