Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
using namespace std;
void calcByRef(double [], int , double& , double& , double& , double& , double& );
void sortAr (double []);
int main ()
{
double ar [15] = {2.4, 6.5, 1.2, 0.7, 15.3, 3.9, 78.1, 12.0, 5.4, 10.1, 24.0,
7.8, 46.8, 1.3, 0.1};
double high, low, avg, median, stdDev;
int num;
sortAr (ar);
cout << "Results using references";
calcByRef(ar, num, low, high, avg, median, stdDev);
cout << "Minimum: \t\t\t" << low << endl;
cout << "Maximum: \t\t\t" << high << endl;
cout << "Mean: \t\t\t" << avg << endl;
cout << "Median: \t\t\t" << median << endl;
cout << "Standard Deviation: \t\t\t" << stdDev << endl;
return 0;
}
/**********************************************************************************
Function: calcByRef
Use: Uses the passed in array to calculate and pass back.
Arguments: 1. ar2[]: the array that contains the second set of numbers.
2. num: Number of elements in the array.
3. low: The smallest number in the array.
4. high: The largest number in the array.
5. avg: The mean of all the elements.
6. median: The median number of the array.
7. stdDev: The calcualated standard deviation of the elements.
Returns: nothing
**********************************************************************************/
void calcByRef(double ar[], int num, double& low, double& high, double& avg, double& median, double& stdDev)
{
int i, a, b;
double sum;
num = 15;
low = ar[0];
high = ar[14];
for(i=0; i<15; i++)
{
sum += ar[i];
}
avg = (double)sum / num;
median = ar[num/2];
for(i=0; i<15; i++)
{
sum += i*i;
a = sum;
}
for(i=0; i<15; i++)
{
sum += i;
b = sum*sum;
}
stdDev = sqrt ((a - (double)(b/num))/(num -1));
}
/**********************************************************************************
Function: sortAr
Use: Sorts out the elements in the arrays and puts them in order.
Arguments: 1. array: The inputted array to be sorted.
Returns: nothing
**********************************************************************************/
void sortAr( double ar[])
{
int top = 0, last = 14, ptr, ssf;
double temp, array[14];
while ( top < last)
{
ptr = top;
ssf = top;
while ( ptr <= last)
{
if ( array[ptr] < array[ssf])
{
ssf = ptr;
}
else
{
}
ptr++;
}
temp = array[top];
array[top] = array[ssf];
array[ssf] = temp;
top++;
} }