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 123 124 125 126 127 128 129 130 131 132 133
|
#include <iostream>
#include <iomanip>
using namespace std;
double median(int *, int);
int mode(int *, int);
int *makeArray(int);
void getMovieData(int *, int);
void selectionSort(int [], int);
double average(int *, int);
int main()
{
double average1, median1;
int *movies, students, mode1;
*makeArray(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.";
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;
}
int *makeArray(int students)
{
int *movies;
cout << "How many students were surveyed? ";
cin >> students;
movies = new int[students];
}
void getMovieData (int *array, int size)
{
cout << "How many movies did each student see";
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;
double median;
middle = size / 2;
if(size % 2)
{
median = array[middle + 1];
}
else
{
median = (array[middle] + array[middle + 1]) / 2.0;
}
return median;
}
int mode(int *array, int size)
{
int num = array[0];
int mode = num;
int count = 1;
int countMode = 1;
for (int index = 1; index < size; index++)
{
if (array[index] == num)
{
count++;
}
else
{
if (count > countMode)
{
countMode = count;
mode = num;
}
count = 1;
num = array [index];
}
}
return mode;
}
|