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 118 119 120 121
|
//Matthew Young HW9 Array;
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
const int MAXVALUE=100;//Maximum amount of values we can pull from.
double i=0;
double loadArray(ifstream& infile, double data[], int MAXVALUE);
double findMax(double data[], int numValue);
double findMin (double data[], int numValue);
double findCalcMean (double data[], int numValue);
double findGeoMean (double data[], int numValue);
double findRmsAvg(double data[], int numValue);
int main(){
double i=0;
double numValue=0;
double resist[MAXVALUE]={0};
ifstream infile ("HW9resist.txt");
if (!infile) {
cout << "Error: can't open file.";
infile.close();
return 1;
}
numValue=loadArray(infile, resist, MAXVALUE);
cout << "The maximum value is " << findMax(resist, numValue) << endl;
cout << "The minimum value is " << findMin(resist, numValue) << endl;
cout << "The calculated mean is " << findCalcMean(resist, numValue) << endl;
cout << "The Geometric Mean is " << findGeoMean(resist, numValue) << endl;
cout << "The Root Mean Average is " << findRmsAvg(resist, numValue) << endl;
for(int i=0; i<numValue; i++){
cout << resist [i] << endl;
}
return 0;
}
double loadArray(ifstream& infile, double data[], int MAXVALUE){
int i=0;
while(i<MAXVALUE && infile >> data[i]){
i++;
}
if(i>=MAXVALUE){
cout << "Maximum size of array met." << endl;
}
if(!infile.eof()){
cout << "Bad daa found." << endl;
}
return i;
}
double findMax(double data[], int numValue){
int index=0;
double max=data[index];
for (int i=1; i<numValue; i++){
if (data[i]>max){
index=i;
max=data[i];
}
}
return max;
}
double findMin (double data[], int numValue){
int index=0;
double min=data[index];
for (int i=1; i<numValue; i++){
if (data[i]<min){
index=i;
min=data[i];
}
}
return min;
}
double findCalcMean (double data[], int numValue){
int index=0;
double CalcMean=data[index];
for (int i=1; i<numValue; i++){
double sum=0;
sum=data[i]*numValue;
CalcMean=sum/numValue;
}
return CalcMean;
}
double findGeoMean (double data[], int numValue){
int index=0;
double prod=1;
double GeoMean=data[index];
for (int i=1; i<numValue; i++){
prod=prod*data[i];
GeoMean=pow(prod, (1.0/i));
}
return GeoMean;
}
double findRmsAvg(double data[], int numValue){
int index=0;
double sumsq=1;
double RmsAvg=data[index];
for (int i=1; i<numValue; i++){
sumsq=sumsq+pow(data[i],2);
RmsAvg=sqrt(sumsq*(1.0/i));
}
return RmsAvg;
}
| |