I made a program but I can't figure out how to convert it using a class called Stats. I would like to do it for my own practice and knowledge but I can't figure it out.
stats.h
// Function prototype declaration
float Mean(constint *arr, size_t size);
float Median(int *arr, size_t size);
void Sort(int *arr, size_t size);
stats.cpp
#include "stats.h"
// Get the mean from the array
float Mean(constint *arr, size_t size) {
float sum = 0;
for(size_t i = 0; i < size; i++)
sum += arr[i];
return sum / size;
}
// Get the median from the array
float Median(int *arr, size_t size) {
Sort(arr, size);
float median;
if(size % 2==0) {
int middle = size / 2;
median = (float)arr[middle];
} else {
//there are two middle elements in array
int middle1 = size / 2;
int middle2 = (size / 2)-1;
// find average of the two middle elements
median = (float)((arr[middle1] + arr[middle2])/2.0);
}
return median;
}
// Sort the array in ascending order using insertion sort
void Sort(int *arr, size_t size) {
for(size_t i = 1; i < size; i++) {
int t = arr[i];
size_t j = i;
while(j > 0 && t < arr[j - 1]) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = t;
}
}
main.cpp
#include <iostream>
#include <sstream>
#include "stats.h"
usingnamespace std;
// Entry point of the program
int main() {
// I/O is handled by function main()
int arr[100];
size_t size = 0;
cout << "Enter a sequence of numbers separated by spaces (end it with a non-digit): ";
// Keep on entering numbers until a non-digit is detected
while(!cin.eof() && size < 100) {
string value;
cin >> value;
// Attempt to convert input to number
if(stringstream(value) >> arr[size]) {
size++;
continue;
}
break;
}
cout << "Mean: " << Mean(arr, size) << endl;
cout << "Median: " << Median(arr, size) << endl;
cout << "After sort: ";
for(int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
system("pause");
return 0;
}