December 2nd, 2011, 01:14 AM
Need help with pointers homework
Hey guys! I have programming homework due tomorrow and really need help!
Here are the instructions:
"A file contains number of movies that students watched last month. The format of the
file is as follows: the 1st number in the file indicates the number of students, followed by the
numbers of movies watched by each student in the class. The numbers in the file are separated by space. Following is a snap shot an example file
10 1 2 3 4 5 6 7 8 9 10
The 1st number indicates that there are 8 students. The 1st student has watched 4 movies, the 2nd student has watched 5 movies, the 3rd student has watched 6 movies and so on. For this assignment, you have to write a program to compute statistical data about number of movies these students watched last month. In your program you must implement the following
readFile: This function should have two parameters: name of the movie file and an integer reference parameter for holding the number of students. The function should open the
designated file and read the number of students (the 1st number in the file) from the file and store it in the reference parameter. If the file doesn’t exist or there is error opening the file, the
function should display an appropriate error message. The function should dynamically create an
array of integers with as many elements as the number of students. The function then should
read the numbers of movies watched by the students and store them in the newly created array. The function should return a pointer to the newly created array.
computeAverage: This function should have two parameters: an integer pointer (pointer of the
1st element of an integers array) and number of elements in the array. The function should
compute the average of the integers in the array pointed by the pointer, and return the average. The return type of this function should be double.
The main function of your program should ask the user to enter the name of the movie data file,
call the readFile function to read the data from the file (Note: you can download the movie data
file named movie.txt from ilearn). The main function should compute and display the following
Number of students:
Total number of movies watched by the students:
Maximum number of movies watched by a student:
Minimum number of movies watched by a student:
Average number of movies watched by the students:
You must use the computeAverage function to computer the average number of movies watched
by the students."
Here is what I have so far:
Please help and Thanks guys!!!!
using namespace std;
int *readFile(char, int &);
double computeAverage(int *, int);
const int SIZE = 8;
// MAY BE DELETED
cout << "Please enter the name of the movie data file: ";
cout << endl << "Number of students: " << *numStudents;
cout << endl << "Total number of movies watched by the students: "; // NOT COMPLETED
cout << endl << "Maximum number of movies watched by a student: "; // NOT COMPLETED
cout << endl << "Minimum number of movies watched by a student: "; // NOT COMPLETED
cout << endl << "Average number of movies watched by the students: " << computeAverage(&firstElement, numElements);
int *readFile(char movieFile, int &numStudents)
cout << "File has not yet been created.\n\n";
//inFile >> &numStudents;
double computeAverage(int *firstElement, int numElements)
double average = 0.0;
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.