Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- double getAverage(int array[], int size);
- int getMedian(int array[], int size);
- void sortArray(int array[], int size);
- int main()
- {
- int numberSurveyed;
- cout << "How many students were surveyed?: " << endl;
- cin >> numberSurveyed;
- int *listPtr;
- listPtr = new int[numberSurveyed];
- cout << "Enter the number of movies each student saw. Seperate each number with a space: " << endl;
- for (int i = 0; i < numberSurveyed; i++)
- {
- cin >> *(listPtr + i);
- }
- ofstream outFile;
- outFile.open("surveyReport.txt");
- outFile << "Statistics for Movie Viewing of College Students" << endl
- << "October 2012" << endl << endl
- << "Total Students Surveyed: " << numberSurveyed << endl << endl
- << "Average: " << getAverage(listPtr, numberSurveyed) << endl
- << "Median: " << getMedian(listPtr, numberSurveyed) << endl;
- outFile.close();
- delete [] listPtr;
- return 0;
- }
- double getAverage(int array[], int size)
- {
- int avg;
- int sum = 0;
- for(int j = 0; j < size; j++)
- {
- sum += array[j];
- }
- avg = sum / size;
- return avg;
- }
- int getMedian(int array[], int size)
- {
- sortArray(array, size);
- int calcMedium = (array[0] + array[size]) / 2;
- int median;
- if((size % 2) != 0)
- {
- median = array[calcMedium];
- }
- else
- {
- int medium1 = calcMedium;
- int medium2 = calcMedium + 1;
- median = (array[medium1] + array[medium2]) / 2;
- }
- return median;
- }
- void sortArray(int array[], int size)
- {
- int startScan, minIndex, minValue;
- for (startScan = 0; startScan < (size - 1); startScan++)
- {
- minIndex = startScan;
- minValue = array[startScan];
- for(int index = startScan +1; index < size; index++)
- {
- if (array[index] < minValue)
- {
- minValue = array[index];
- minIndex = index;
- }
- }
- array[minIndex] = array[startScan];
- array[startScan] = minValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement