Hello everyone. Having issues figuring out how to use this dynamic array. The program is just supposed to gather some info into a dynamically allocated array, and then find the average, mean, median, and mode. I can't seem to figure out how to use the array in my function definitions because it's not declared before main. When I define the array before main, I get fatal error LNK1120: 1 unresolved externals. If I take it out I get undeclared identifier. Any help would be GREAT.
#include <iostream>
usingnamespace std;
int numStudents[];
void average(int);
int main()
{
int *numStudents = NULL; int x, howmany;
cin >> x;
numStudents = newint[x];
for (int i = 0; i < x; i++) //initialize all array elements to 0
numStudents[x] = 0;
for (int q = 0; q < x; q++)
{
cout << "\nEnter the number of movies watched for student " << q + 1 << endl;
cin >> howmany;
numStudents[q] = howmany;
}
cout << endl << endl;
return 0;
}
void average(int z)
{
int sum = 0;
double avg;
for (int x = 0; x < z; x++) //find average
sum = sum + *(numStudents + x);
avg = sum / z;
cout << "The average is " << avg << endl;
}
I added line 7 because when it wasn't there, I was getting "undeclared identifier" as the error in my average() function definition. So it seems like youi are saying I should just replace line 7 with line 14.
when you calculate the number , the operation will use their own pattern , so the divided will return Integer always.
you should write like
avg = (double)sum / z; // we 'll convert sum to double , and the calculate will use double pattern for calculation . (because double's priority is bigger than integer)