### Array Help

These arrays are kicking my butt. Can someone help me with what i have so far?

 ``1234567891011121314151617181920212223242526272829303132333435`` `````` int getGrades(float grades[], int num) { for(int i = 0; i <= num; i++) { cout << "Grade " << i + 1 << ": "; cin >> grades[num]; } } float averageGrades(float grades[], int num) { float sum; float avg; for(int i = 0; i <= num; i++) { grades[i] +=sum; } sum / 10 == avg; return avg; } int main() { // declare array float grades[9]; //fill them getGrades (grades, 9/*numGrades*/); float avg = averageGrades(grades, 9/*numGrades*/); //display results cout << "Average Grade: " << avg << "%" << endl; return 0; }``````

When I run the program, it runs through nicely, until i get to the average.It always comes out as 0%.

Can someone tell me what I'm doing wrong? Thank you for all of the help!
Last edited on
Use [code][/code] tags around your code to format it nicely.

On the line after your for loop, you have `sum / 10 == avg;`. This is incorrect for two reasons: one, that is a comparison, not assignment, and thus doesn't do anything (I presume you meant to do `avg = sum / 10;`, two, the sum should be divided by the num of elements in the array, not by 10.

Otherwise, it seems you have the idea correct.
Oh my goodness, that is EXACTLY what I meant. I kept getting an error when I just did = avg. Stupid mistake. I took you advice, and it still comes out as 0%. Any ideas?
What exactly did you change that line to?

Edit: And you seem to have flipped the operands to += inside your loop as well; you probably want `sum += grades[i]`, and you also need to initialize sum to 0 before trying to use it.
Last edited on
I changed it to this:
` avg = sum / num; `
Please check my edit in my previous post.
Thank you! it is now working. I need to stop flipping the operands. Thanks you again for all the help, this helped me understand arrays that much more.
Topic archived. No new replies allowed.