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
|
#include <iostream>
using namespace std;
//prototyypes
float Mean (int* array, size_t size);
float Median (int* array, size_t size);
void Swap (int& x, int& y);
void Sort (int* array, size_t);
//main function
main()
{
const size_t MAX_SIZE = 100;
int array[MAX_SIZE];
cout << "Enter integers('x' to stop)\n";
size_t size = 0;
while ((size < 100) && (cin >> array[size]))
{
size++;
}
cout << "Median: " << Median(array, size);
cout << "Mean: " << Mean(array, size);
return 0;
}
//function implementations
float Mean(int numbers[],size_t sizeOfArray)
{
float avg = 0;
float sum = 0;
for(int i = 0; i < sizeOfArray; i++)
{
sum +=(numbers[i]);
}
avg = sum/sizeOfArray;
return avg;
}
float Median(int numbers[]),size_t sizeOfArray)
{
sort(numbers, sizeOfArray);
float med = 0;
if (sizeOfArray % 2 == 0)
{
med = (numbers[sizeOfArray/2] + numbers[sizeOfArray/2-1]) / 2;
}
else
{
med = numbers[sizeOfArray/2];
}
return med;
}
void Swap(int& x, int& y)
{
int temp = 0;
temp = x;
x = y;
y = temp;
}
void Sort(int array, size_t sizeOfArray)
{
int i, j, min;
for (i = 0; i < sizeOfArray - 1; i++ )
{
min = i;
for(j = i + 1; j < sizeOfArray; j++)
if(array[j] < array[min])
min = j;
if (min != i)
Swap(array[i], array[min]);
}
}
|