Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5. double getAverage(int array[], int size);
  6. int getMedian(int array[], int size);
  7. void sortArray(int array[], int size);
  8.  
  9. int main()
  10. {
  11.     int numberSurveyed;
  12.  
  13.     cout << "How many students were surveyed?: " << endl;
  14.     cin >> numberSurveyed;
  15.  
  16.     int *listPtr;
  17.     listPtr = new int[numberSurveyed];
  18.  
  19.     cout << "Enter the number of movies each student saw. Seperate each number with a space: " << endl;
  20.     for (int i = 0; i < numberSurveyed; i++)
  21.     {
  22.         cin >> *(listPtr + i);
  23.     }
  24.  
  25.     ofstream outFile;
  26.     outFile.open("surveyReport.txt");
  27.  
  28.     outFile << "Statistics for Movie Viewing of College Students" << endl
  29.             << "October 2012" << endl << endl
  30.             << "Total Students Surveyed: " << numberSurveyed << endl << endl
  31.             << "Average: " << getAverage(listPtr, numberSurveyed) << endl
  32.             << "Median: " << getMedian(listPtr, numberSurveyed) << endl;
  33.  
  34.     outFile.close();
  35.     delete [] listPtr;
  36.  
  37.     return 0;
  38. }
  39.  
  40. double getAverage(int array[], int size)
  41. {
  42.     int avg;
  43.     int sum = 0;
  44.  
  45.     for(int j = 0; j < size; j++)
  46.     {
  47.         sum += array[j];
  48.     }
  49.  
  50.     avg = sum / size;
  51.     return avg;
  52. }
  53.  
  54. int getMedian(int array[], int size)
  55. {
  56.     sortArray(array, size);
  57.     int calcMedium = (array[0] + array[size]) / 2;
  58.     int median;
  59.  
  60.     if((size % 2) != 0)
  61.     {
  62.         median = array[calcMedium];
  63.     }
  64.     else
  65.     {
  66.         int medium1 = calcMedium;
  67.         int medium2 = calcMedium + 1;
  68.         median = (array[medium1] + array[medium2]) / 2;
  69.     }
  70.     return median;
  71. }
  72.  
  73. void sortArray(int array[], int size)
  74. {
  75.     int startScan, minIndex, minValue;
  76.  
  77.     for (startScan = 0; startScan < (size - 1); startScan++)
  78.     {
  79.         minIndex = startScan;
  80.         minValue = array[startScan];
  81.         for(int index = startScan +1; index < size; index++)
  82.         {
  83.             if (array[index] < minValue)
  84.             {
  85.                 minValue = array[index];
  86.                 minIndex = index;
  87.             }
  88.         }
  89.         array[minIndex] = array[startScan];
  90.         array[startScan] = minValue;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement