#include <iostream>
usingnamespace std;
int studentsSurveyed();
int * studentArray(int);
int studentResults();
int numMoviesViewed(int *);
float averageViewedMovies(int*);
void displayReports();
void Reports(int, double);
int main()
{
int *movieArray;
int numStudents;
double averageMoviesViewed;
double avgMoviesViewed = 0;
numStudents = studentsSurveyed();
movieArray = studentArray(numStudents);
numMoviesViewed(movieArray);
avgMoviesViewed = averageViewedMovies(movieArray);
cout << "The average number of movies viewed by the group of students is " << avgMoviesViewed << endl;
Reports(numStudents, avgMoviesViewed);
return 0;
}
int studentsSurveyed()
{
int numStudents;
cout << "How many total students were surveyed?\n";
cin >> numStudents;
return numStudents;
}
int * studentArray(int numStudent)
{
int * array;
array = newint[numStudent];
for (int count = 0; count < numStudent;count++)
{
array[count];
}
return array;
}
int numMoviesViewed(int *array)
{
int movies = 0;
int totMovies = 0;
if(sizeof(*array) <= 0)
return -1;
for(int student = 0; student < (sizeof(*array) - 1); student++)
{
cout << "How many movies has student " << (student + 1) <<" seen this month?\n";
cin >> movies;
// array[student] = movies;
totMovies += (int)movies;
}
return 1;
}
float averageViewedMovies(int *array)
{
float average = 0;
int totMovies = 0;
for(int i=0; i < sizeof(*array)-1; i++)
{
totMovies += array[i];
//cout << "debug" << array[i] << endl;
}
average = totMovies / sizeof(*array) -1;
return average;
}
void Reports(int numStudents, double avgMoviesViewed)
{
cout << "The total number of students surveyed was " << numStudents << " and the average movies veiwed was "
<< avgMoviesViewed << "." << endl;
return;
}
sizeof(*array)
This is not the right way to get the array length.
If the size of the array is declared in compile time then use can use sizeof(array) / sizeof(int) to get the length of the array. But in your case "array" is a dynamic array, so you'll have to include numStudents in every function that include int *array, such as:
int numMoviesViewed(int *, int);
float averageViewedMovies(int*, int);
and
int numMoviesViewed(int *array, int numStudents)
float averageViewedMovies(int *array, int numStudents)
and then change "sizeof(*array)" to "numStudents"
and one more thing, why (minus 1), shouldn't you include the last student in
for(int student = 0; student < (sizeof(*array) - 1); student++)
sizeof(*array) will return sizeof(int) because array is an integer array, that is *array is int and sizeof(*array) will return sizeof(int) which is 4. That explains why you need -1 to get to 3 =.= Your number of students could be 4 5 6 or 2 because it is dynamic. If you were only asked for 3 then make an array of 3 int instead of using "new". Oh and you'll have to delete movieArray to free memory that you allocated...
int main()
{
....
delete [] movieArray;
return 0;
}