Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. //James Waldman
  2. //4/20/18
  3. //Homework 4
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. void getData(int *, int);
  9. double getAvg(int *, int);
  10. double getTotal(int *, int);
  11.  
  12. int main()
  13. {
  14.     int *Movies = NULL;
  15.     int SIZE;
  16.  
  17.     cout << "How many students were surveyed? " << endl;
  18.     cin >> SIZE;
  19.     while (SIZE < 0) {
  20.         cout << "Error. Try again." << endl;
  21.         cin >> SIZE;
  22.     }
  23.     Movies = new int[SIZE];
  24.     getData(Movies, SIZE);
  25.     cout << "The number of Movies seen: " << endl;
  26.     cout << getTotal(Movies, SIZE) << endl;
  27.     cout << "The average number of Movies seen: " << endl;
  28.     cout << getAvg(Movies, SIZE) << endl;
  29.  
  30.     delete Movies;
  31.     Movies = NULL;
  32.  
  33.     system("pause");
  34.     return 0;
  35. }
  36.  
  37. void getData(int *array, int size)
  38. {
  39.     cout << "How many movies did they watch?" << endl;
  40.     for (int i = 0; i < size; i++)
  41.     {
  42.         cout << "Student " << (i + 1) << ": ";
  43.         cin >> *(array + i);
  44.         while (*array + i < 0) {
  45.             cout << "Error. Try again." << endl;
  46.             cin >> *(array + i);
  47.         }
  48.     }
  49. }
  50.  
  51. double getAvg(int *array, int size)
  52. {
  53.     double total = 0;
  54.     double avg;
  55.     for (int i = 0; i < size; i++)
  56.     {
  57.         total += *(array + i);
  58.     }
  59.     avg = total / size;
  60.     return avg;
  61. }
  62.  
  63. double getTotal(int *array, int size)
  64. {
  65.     double total = 0;
  66.     double *total1 = NULL;
  67.     for (int i = 0; i < size; i++)
  68.     {
  69.         total += *(array + i);
  70.  
  71.     }
  72.     return total;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement