Functions and arrays

May 17, 2017 at 10:14pm
Hello. I am trying to write a program that averages judges scores that the user inputs. The user will be able to enter the number of scores as well. I have to use two different functions. One that gets the number of scores, gets the scores, and then stores it in an array. The other function calculates the average of the scores. I am having trouble with the array part. It is giving me an error and saying "expression must have a constant value". I'm new to programming so mind my ignorance. Thanks for any help.

#include<iostream>

using namespace std;

int getScore();
double calcAverage(int sumScores, int numScores);


int main()
{
getScore();
double calcAverage(int, int);

getScore();

system("pause");
}


int getScore()
{
int numScores;

cout << "How many scores would you like to enter?" << endl;
cin >> numScores;

int judgeScores[numScores];
int x;

for (x = 0; x < numScores; x++)
{
cout << "Enter your score" << endl;
cin >> judgeScores[x];
}

int sumScores = sumScores + judgeScores[x];

return numScores;
return sumScores;
return judgeScores[numScores];
}

double calcAverage(int judgeScores[numScores], int sumScores, int numScores)
{
double averageScore = sumScores / numScores;

return averageScore;
}
May 17, 2017 at 10:34pm
1
2
3
4
5
6
int numScores; // Must be constant and specify the size for static array (i.e. const int numScores{10})

cout << "How many scores would you like to enter?" << endl;
cin >> numScores;

int judgeScores[numScores];


For static arrays, you must specify the number of elements in the array.

If you must ask the user how many scores to enter, then you need to look at dynamic array memory. This link http://www.cplusplus.com/doc/tutorial/dynamic/

Last edited on May 17, 2017 at 10:35pm
May 17, 2017 at 10:42pm
Yes but the user must enter the number of elements in the array and im not sure how to format that. Once again thanks for the help.
May 17, 2017 at 10:55pm
Either use a const value for the size:
1
2
const int size = 10;
int judgeScores[size];


or use a dynamic array
1
2
3
4
cout << "How many scores would you like to enter?" << endl;
cin >> numScores;

int *array =  new int [numScores];
May 17, 2017 at 11:05pm
As mentioned before your array must have a constant size that cannot change unless dynamically allocated.
Topic archived. No new replies allowed.