Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iomanip>
- #include <iostream>
- #include <string>
- using namespace std;
- // INCLUDE PROTOTYPES
- int* create_array(int);
- void enter_data(int*, int);
- float find_average(int*, int);
- void show_array(int*, int);
- int main()
- {
- int *dyn_array;
- int students;
- float avrg;
- do
- {
- cout << "How many students will you enter? ";
- cin >> students;
- }while ( students <= 0 );
- dyn_array = create_array( students );
- //this function creates a dynamic array
- //and returns its pointer
- enter_data( dyn_array, students );
- //use 'pointer' notation in this function to
- //access array elements
- //accept only numbers 0-100 for movie seen
- avrg = find_average( dyn_array, students );
- //use 'pointer' notation in this function to
- //access array elements
- cout << "The array is:" << endl << endl;
- show_array( dyn_array, students);
- cout << endl;
- cout << "The average is " << avrg << ".\n";
- delete [] dyn_array;
- return 0;
- }
- int* create_array( f_students )
- {
- int* f_array = new int[ f_students ];
- return f_array;
- }
- void enter_data(int* f_dyn_array, int f_students)
- {
- for(int i = 0; i < f_students; i++)
- {
- cout << "How many movies did student" << i + 1 << "watch?: ";
- cin >> f_dyn_array[i];
- }
- while(f_dyn_array[i] < 0 || f_dyn_array[i] > 100)
- {
- cout << "Please enter a number between 0 and 100!";
- cin >> f_dyn_array[i];
- }
- }
- float find_average(int* f_dyn_array, int f_students)
- {
- int sum = 0;
- float avrg;
- for(int i = 0; i < f_students; i++)
- {
- sum = sum + f_dyn_array[i];
- }
- avrg = sum/f_students;
- return avrg;
- }
- void show_array(int* f_dyn_array, int f_students)
- {
- cout << "Student Movies" << "\n";
- for(int i = 0; i < f_students; i++)
- {
- cout << i + 1 << " " << f_dyn_array[i] << "/n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement