1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <iostream>
#include <iomanip>
using namespace std;
double median(int *, int);
int mode(int *, int);
void getMovieData(int *, int);
void selectionSort(int [], int);
double average(int *, int);
int main()
{
double average1, median1;
int *movies, students, mode1;
cout << "How many students were surveyed? ";
cin >> students;
movies = new int[students];
getMovieData(movies, students);
selectionSort(movies, students);
average1 = average(movies, students);
median1 = median(movies, students);
mode1 = mode(movies, students);
cout << "Here are the results for the number of movies college students watch in a month.\n";
cout << fixed << showpoint << setprecision(2);
cout << "The average is " << average1 << endl;
cout << "The median is " << median1 << endl;
cout << "The mode is " << mode1 << endl;
delete [] movies;
movies = 0;
return 0;
}
void getMovieData (int *array, int size)
{
cout << "How many movies did each student see \n";
for (int index = 0; index < size; index++)
{
cout << "Student " << (index + 1) << ": ";
cin >> *(array + 1);
}
}
void selectionSort(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;
}
}
double average(int *array, int size)
{
double sum = 0;
for (int index = 0; index < size; index++)
{
sum += *(array +index);
}
return sum / size;
}
double median(int *array, int size)
{
int middle = (size - 1) / 2;
double median;
if(size % 2 == 0)
{
median = (*(array + middle) + *(array + (middle + 1))) / 2;
}
else
{
median = *(array + middle);
}
return median;
}
int mode(int *array, int size)
{
int mode, max, count;
count = max = 0;
for (int index = 0; index < size; index++)
{
count++;
if (*(array + index) < *(array + index + 1))
{
if (count > max)
{
mode = *(array + index);
max = count;
}
count = 0;
}
}
}
|