Thread: Data type problems, float, int, etc with averages

1. Data type problems, float, int, etc with averages

Hi guys, i'm having a little trouble getting my GPA average calculator to work as one of the challenges from the book. The sample data is:

GPA = 3.5, 2.8, 3.0, 2.5, 4.0, 3.7
avg GPA = 3.25

i can't seem to get the sample data above to work. For me i think there something wrong with the data types. Shouldn't i be using float for decimal type values? Correct me if i'm wrong.

Code:
```#include <stdio.h>

float iGPA[29];

int main(void) {

int x;
float countGPA = 0;
int iSelection;
float eGPA;
float gpaTOTAL;
float avgGPA = 0;;

printf("\n\tGPA Average Calculator\n");

for (x = 0; x < 29; x++) {

printf("\nEnter gpa: ");
scanf("%f", &eGPA);

iGPA[x] = eGPA;

countGPA++;

printf("2\tCalculate average GPA: ");
printf("\nEnter selection: ");
scanf("%d", &iSelection);

if (iSelection == 2) {
break;
}

}

int y;

for (y = 0; y < countGPA; y++) {

gpaTOTAL = iGPA[y];
gpaTOTAL += gpaTOTAL;

}
avgGPA = gpaTOTAL / countGPA;
printf("\nAverage GPA is: %f", avgGPA);

}```

2. Look at the following snippet:
Code:
```for (y = 0; y < countGPA; y++) {
gpaTOTAL = iGPA[y];
gpaTOTAL += gpaTOTAL;
}```
Each time through your loop you reset gpaTOTAL to iGPA[y], erasing anything in the total. The following should get you closer.
Code:
```gpaTOTAL = 0.0;
for (y = 0; y < countGPA; y++) {
gpaTOTAL += iGPA[y];
}```
Jim

3. Each time through the loop, this will overwrite the previous value of gpaTOTAL:
Code:
`gpaTOTAL = iGPA[y];`

4. Yep - the problem is in the summing loop, as described in the last post.

You data types are mostly ok. Everything that needs to be a float is a float. I wouldn't use a float for countGPA though. There's no need for it, it's just a counter, it can be an int.

5. Thanks

Hi guys thanks,

I originally had that, but i think i forgot to change all the floats that were originally ints, which kept giving me zero average. I understand now. I appreciate it guys.

Sincerely,
Jomoka