# Help with code for calculating grade average

I need to make a code for calculating grade average and I have come up with

int mygrades = [98, 89, 94, 85, 76, 94, 68, 97, 87, 88, 91, 94];
int i = 0;
int average = 0;
i = i + 1;
end while
return average
I need to know what my mistake is in this code thanks

I need to know what my mistake is in this code thanks

Italics

``````while i ? length(mygrades) do
``````

Non-C syntax

Hi bigmac92

Your approach in that pseudo-code (add up the grades, then divide by the number of grades) is ok, but a few things to consider when you turn it into valid C …

To get the number of elements in the array, use:

``````int length = sizeof(mygrades) / sizeof(mygrades);
``````

This finds the total size of the array in bytes, then divides by the number of bytes used to hold one element, so gives the number of elements.

Instead of the `while` loop, you could use a `for` loop:

``````int sum = 0;
for (i = 0; i < length; i++)
{
}
``````

And when you calculate the average, remember that your code will work out an integer result, so will lose the fractional part of the answer.

``````average = sum / length;
``````

Regards

Ray

THANK YOU I APPRECIATE IT GUYS :)!!

``````int  mygrades [] = {98, 89, 94, 85, 76, 94, 68, 97, 87, 88, 91, 94};
``````

note: it is often interesting to see what the running average does, because that gives less weight to historical grades.

you might check - http://playground.arduino.cc/Main/Statistics - to get beyond average.