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
|
// ptrtest1.cpp
// tests sum, maxPtr, minPtr and valueDiff functions
#include <iostream>
#include "ptrfuncs.h"
#include <iomanip> // to use setw
// IMPLEMENT ptrfuncs.h FUNCTIONS BELOW
double sum(double *values, int n) {
int addition = *x;
for (int i = 0; i < n - 1; i++) {
addition += *(x + i);
return addition;
}
}
double *maxPtr(double *values, int n) {
int x;
maxPtr = *x;
for (int i = 0; i < n; i++) {
if (*(x + i) > maxPtr)
maxPtr = *(x + i);
}
return *maxPtr;
}
double *minPtr(double *values, int n) {
int x;
minPtr = *x;
for (int i = 0; i < n; i++) {
if (*(x + i) > minPtr)
minPtr = *(x + i);
}
return *minPtr;
}
double valueDiff(double *left, double *right) {
double range = maxPtr - minPtr;
return range;
}
int main() {
using namespace std;
double x[] = {
406.94, 198.63, 961.99, 238.36, 910.21, 188.81, 467.37, 639.6,
125.1, 581.16, 144.5, 354.07, 812.45, 716.26, 150.35, 753.4,
387.7, 491.69, 788.64, 624.17, 263.02, 545.16, 908.81, 829.42,
287.31, 913.75, 811, 245.32, 997.68, 701.87, 806.56, 76.68,
368.48, 792.79, 645.42, 868.44, 87.5, 702.63, 403.33, 698.98,
772.66, 793.88, 678.93, 37.28, 328.28, 893.33, 80.62, 721.98,
213.74, 251.96, 375.7, 18.64, 541.57, 117.97, 315.98, 136.18,
743.54, 519.56, 863.66, 359.53, 340.57, 283.71, 908.8, 600.22,
620.95, 684.85, 479.14, 210.61, 685.58, 23.42, 466.19, 641.73,
985.22, 803.12, 139.89, 106.97, 463.64, 575.13, 461.76, 572.48
};
int nx = (sizeof x) / sizeof(double); // total number of values
int half = nx / 2;
double *first = x;
double *max, *min;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "results for whole set of values:\n";
cout << " sum = " << sum(first, nx) << endl;
max = maxPtr(first, nx);
cout << " max = " << *max
<< ", at position " << max - first << endl;
min = minPtr(first, nx);
cout << " min = " << *min
<< ", at position " << min - first << endl;
cout << " range = " << valueDiff(max, min) << endl;
cout << "results for first half of values:\n";
cout << " sum = " << sum(first, half) << endl;
max = maxPtr(first, half);
cout << " max = " << *max
<< ", at position " << max - first << endl;
min = minPtr(first, half);
cout << " min = " << *min
<< ", at position " << min - first << endl;
cout << " range = " << valueDiff(max, min) << endl;
cout << "results for second half of values:\n";
cout << " sum = " << sum(first + half, half) << endl;
max = maxPtr(first + half, half);
cout << " max = " << *max
<< ", at position " << max - first << endl;
min = minPtr(first + half, half);
cout << " min = " << *min
<< ", at position " << min - first << endl;
cout << " range = " << valueDiff(max, min) << endl;
return 0;
}
|