### Array question

I need to write a code that calculates the average score based on the number of scores the user inputs and it has to be between 1 and 20. What am i doing wrong?
Thanks!

 ``1234567891011121314151617181920212223242526272829`` ``````#include using namespace std; int main() { int size,count,sum; double average; cout << "Enter class size <1-20>" << endl; cin >> size; int numbers [size]; if (size>=1 && size<=20) { for (count=0;count> size; sum=sum+size; average = sum / size; } cout << "Original Scores"<< endl; cout << size; cout << "Average Score"<
Well, your array numbers has a problem- you are defining its size using a variable. While that may be allowed in some compilers, it isn't in others. Also... you're altering size in your for loop. Size is also a parameter in the for loop for when it exits... you see the issue with that, right? Also, you aren't putting anything back into the array you just defined. Your average variable is also going to be wrong...

I recommend you go back through the documentation, up to arrays. It should help you see what's wrong in your code.
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include using namespace std; int main() { const size_t N = 20; size_t size; while ( true ) { size = 0; cout << "Enter class size <1-" << N << ">: "; cin >> size; if ( !cin || ( size > 0 && size <= N ) ) break; cout << "Class size is NOT within required range. The required range is 1 to " << N << endl; } if ( size ) { int sum = 0; for ( size_t i = 0; i < size; i++) { int score; cout << "Enter Score: "; cin >> score; sum += score; } double average = static_cast( sum ) / size; cout << "Sum of scores: " << sum << std::endl; cout << "Average Score: " << average << endl; } return 0; }``````
Last edited on
Topic archived. No new replies allowed.