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
|
#include "stdafx.h"
#include <iostream>
using namespace std;
double calcmedian(int*, int);
int calcmode(int*, int);
int *makeArray(int);
void getMovieData(int*, int);
void selectionsort(int[], int);
double calcavg(int*, int);
int main()
{
int *nums;
int num_student = 0;
nums = &num_student;
getMovieData(nums, num_student);
*makeArray(num_student);
nums = nums;
selectionsort(nums, num_student);
// display the results.
cout << "\nthe mean is: ";
cout << calcavg(nums, num_student) << endl;
cout << "\nthe mode is: ";
cout << calcmode(nums, num_student) << endl;
cout << "\nthe median is: ";
cout << calcmedian(nums, num_student) << endl;
delete[] nums;
nums = 0;
system("pause");
return 0;
}
double calcmedian(int *nums, int num_student)
{
double median;
if (num_student % 2 == 0)
{
median = (nums[num_student / 2] + nums[(num_student / 2) + 1]) / 2;
}
else
median = nums[num_student / 2];
return median;
}
int calcmode(int *nums, int num_student)
{
int mode = 0;
int val = 0;
int index;
for (index = 0; index < num_student; index++)
{
if (*(nums + index) == *(nums + (index + 1)))
{
mode++;
val = *(nums + index);
}
}
if (val > 0)
return val;
else
return -1;
}
int * makeArray(int num_student)
{
int count;
int *nums;
nums = new int[num_student];
if (nums == NULL) {
cout << "Error allocating memory!\n";
}
cout << "Enter the how many movies each student saw: ";
for (count = 0; count < num_student; count++) {
cout << "Student " << (count + 1) << ": ";
cin >> nums[count];
}
return nums;
}
void getMovieData(int *array, int num_students)
{
cout << "How many students were surveyed? ";
cin >> num_students;
if (num_students < 0)
{
cout << "Invalid number of students!\n";
}
}
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 calcavg(int *array, int size)
{
int total = 0;
double avg = 0.0;
for (int i = 0; i < size; ++i) {
total += array[i];
}
avg = total / size;
return avg;
}
| |