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
|
#include <iostream>
#include <cmath>
using namespace std;
float meanFunction(float *arr, int count);
float medianTotal(float *arr, int count);
float modeFunction(float *arr, int count);
void stdFunction(float mean, int count);
void sortFunction(float *arr, int count);
int main() {
int count = 0, go_on = 0;
float num[100], mean, median, *arr = num, mode;
tryAgain: // Statement Label
for (int i = 0; i < 40; i++) {
cout << "**";
}
cout << "This program will calculate the mean, median, mode, and standard deviation. " << endl;
cout << "Enter the amount of values that you will select (must be 100 or less)." << endl;
for (int i = 0; i < 40; i++) {
cout << "**";
}
cin >> go_on;
if (go_on > 100) { // validating array size
cout << "Please follow the directions!" << endl;;
goto tryAgain;
}
cout << "You have entered " << go_on << " values." << endl;
do {
cout << "Enter a number: ";
cin >> num[count];
count++;
}
while (count < go_on);
sortFunction(arr,count); // call for sort function
median = medianTotal(arr, count); // call for median function
mean = meanFunction(arr,count); // call for mean function
mode = modeFunction(arr, count);
cout << "The Mode is: " << mode << endl;
cout << "The Mean is: " << mean << endl;
cout << "The Median is: " << median << endl;
stdFunction(mean, count);
cout << "GOOD BYE!" << endl;
return 0;
}
void stdFunction(float mean, int count) {
float stdAnswer1, stdAnswer2;
stdAnswer1 = (mean * mean);
stdAnswer1 = (stdAnswer1 / (count - 1));
stdAnswer1 = sqrt(stdAnswer1);
stdAnswer2 = stdAnswer1;
stdAnswer1 = (mean - stdAnswer1);
stdAnswer2 = (mean + stdAnswer2);
cout << "The Standard Deviation is: " << stdAnswer1 << " & " << stdAnswer2 << endl;
}
void sortFunction(float *arr, int count) {
int i;
float mode, swapHolder;
for (int counter = (count - 1); counter > 0; counter--) {
for(i = 0; i < count; i++) {
if (arr[i] > arr[i + 1]) {
swapHolder = arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = swapHolder;
}
}
}
for (int l = 0; l < count; l++) {
cout << arr[l] << endl;
}
}
float modeFunction(float *arr, int count) {
int currentNumCount = 0, mostFoundCount = 0, i = 0;
float currentNum = arr[i], mostFound = arr[i];
for (i = 0; i < count; i++) {
if (arr[i] == currentNum) {
currentNumCount++;
}
else {
if (currentNumCount > mostFoundCount) {
mostFound = currentNum;
mostFoundCount = currentNumCount;
}
currentNum = arr[i];
currentNumCount = i;
}
}
return mostFound;
}
float meanFunction(float *arr, int count) {
float sum = 0, meanAnswer;
for (int i = 0; i < count; i++) {
sum += arr[i];
}
meanAnswer = (sum / count);
return meanAnswer;
}
float medianTotal(float *arr, int count){
int middle = (count / 2);
float average;
if ((count % 2) == 0){
average = ((arr[middle - 1] + arr[middle]) / 2);
}
else {
average = (arr[middle]);
}
return average;
}
|