1. ## adding contents of an array

I have this program i'm working on and i'm suppose to input some grade averages and then calculate the avg what I'm trying to do first is just add all the GPAs but it won't work can someone tell me why heres the code
Code:
#include <stdio.h>
#include <stdlib.h>

void CalculateAvg(void);

int i;
float iGpa[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

int main()
{
int iChoice = 0;

while (iChoice != 3)
{
printf("\n\tGPA Calculator\n");
printf("1\tCalculate Average\n");
printf("2\tEnter new GPA\n");
printf("3\tQuit\n");

printf("\n\n\tEnter a choice: ");
scanf("%d",&iChoice);

if(iChoice == 1)
{
system("cls");
CalculateAvg();
}

if(iChoice == 2)
{
printf("\n\tGPA Calculator\n");
printf("\tEnter a GPA: ");
scanf("%f",&iGpa[i]);
}

}
}
void CalculateAvg()
{
float x;
int i;
for (i = 0; i <= 30; i++)
x += iGpa[i];
printf("The average is %f\n",x);
}

2. Probably because you didn't initialize x, so it contains garbage values.

3. Initialize x doesn't do the trick it comes out the same what it does is print the last record I entered

4. You need to initialize x, for sure, to 0.0

Then you need to divide x after you've added up the grades into it, by the number of grades in iGpa. After all, it's still calculating an average.

5. but it won't add up the grades the code is wrong or something it prints out the last GPA record I put in there not them all added up that's what I don't get

6. if I can't add them then I can't divide them

7. That's because you keep overwriting the previous values. The variable i (the global one) never changes from 0.

8. And here's the answer to that:

scanf("%f",&iGpa[i]);

You want i++ in there, not just i, and you need to initialize it to 0, also.

9. hey thanks guys I ran the code it works now here it is
Code:
#include <stdio.h>
#include <stdlib.h>

void CalculateAvg(void);

int iNum = 0;
float iGpa[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

int main()
{
int i = 0;
int iChoice = 0;

printf("\n\tGPA Calculator\n");
printf("\tHow many records to enter: ");
scanf("%d",&iNum);

while (iChoice != 3)
{
printf("\n\tGPA Calculator\n");
printf("1\tCalculate Average\n");
printf("2\tEnter new GPA\n");
printf("3\tQuit\n");

printf("\n\n\tEnter a choice: ");
scanf("%d",&iChoice);

if(iChoice == 1)
{
system("cls");
CalculateAvg();
}

if(iChoice == 2)
{
printf("\n\tGPA Calculator\n");
printf("\tEnter a GPA: ");
scanf("%f",&iGpa[i++]);
}

}
}
void CalculateAvg()
{
float x = 0;
int i;
for (i = 0; i <= iNum; i++)
x += iGpa[i];
printf("The average is %f\n",x/iNum);
}