#include #include #include using namespace std; void calcByAddr(double a[], int b, double *c, double *d, double *e, double *f, double *g); void calcByRef(double a[], int b, double& c, double& d, double& e, double& f, double& g); void sortAr (double a[]); int main () { double ar1 [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 ar2 [15] = {4.2, 5.6, 2.1, 7.0, 51.3, 9.3, 81.7, 20.1, 4.5, 11.1, 40.2, 8.7, 86.4, 3.1, 1.0}; double high, low, avg, median, stdDev; int num; sortAr (ar1); sortAr (ar2); cout << "Results using pointers"; calcByAddr(ar1, 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; cout << "Results using references"; 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; calcByRef(ar2, num, low, high, avg, median, stdDev); return 0; } /********************************************************************************** Function: calcByAddr Use: Uses the passed in array to calculate and pass back. Arguments: 1. ar1[]: the array that contains the first 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 calcByAddr(double ar1[], int num, double *low, double *high, double *avg, double *median, double *stdDev) { int i, a, b; double sum, ar[15]; *low = ar1[0]; *high = ar[14]; for(i=0; i<15; i++) { sum += ar1[i]; } *avg = sum / num; *median = ar1[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: 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 ar2[], int num, double& low, double& high, double& avg, double& median, double& stdDev) { int i, a, b; double sum; low = ar2[0]; high = ar2[14]; for(i=0; i<15; i++) { sum += ar2[i]; } avg = (double)sum / num; median = ar2[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++; } }