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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
//Steve Osborne, My First Array
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
const int MAX=51;
void findMax(double resist[], int numValues, ofstream& outfile);
void findMin(double resist[], int numValues, ofstream& outfile);
void calcMean(double resist[], int numValues, ofstream& outfile);
void geomMean(double resist[], int numValues, ofstream& outfile);
void rmsAvg(double resist[], int numValues, ofstream& outfile);
void harmMean(double resist[], int numValues, ofstream& outfile);
void sortValues(double resist[],int n);
int main(){
int t;
int T;
int i;
int numValues=0;
double resist[MAX];
ifstream infile("C:\\Users\\Inara\\Desktop\\resist.txt");
if (!infile){
cout<<"Unable to read input file.\n";
return 0;
}
ofstream outfile("results.txt");
if (!outfile){
cout << "Unable to create output file.\n";
return 0;
}
for (i=1;i<MAX && !infile.eof();i++) {
numValues++;
infile>>resist[i];
}
cout<<"Steve Osborne, My First Array\n\n"
<<"The output file created with the results can be found"
<<"\nwhere your project folder is located.";
outfile<<"Steve Osborne, Program 10\n"
<<"Sample Resistance Study\n";
findMax(resist, numValues, outfile);
findMin(resist, numValues, outfile);
calcMean(resist, numValues, outfile);
geomMean(resist, numValues, outfile);
rmsAvg(resist, numValues, outfile);
harmMean(resist, numValues, outfile);
sortValues(resist,numValues);
cout<<"\n";
for(T=0;T<numValues;T++){
cout<<"\n"<<fixed<<setprecision(2)<<resist[MAX];
}
cout<<"\n\n";
infile.close();
outfile.close();
return 0;
}
void findMax(double resist[], int numValues, ofstream& outfile){
int i=1;
double value=resist[i];
double max=resist[i];
while(i<=numValues){
if(value>max) max=value;
value=resist[i++];
}
outfile<<"The maximum value is"
<<fixed<<setprecision(2)<<setw(18)<<max<<"\n";
}
void findMin(double resist[], int numValues, ofstream& outfile){
int i=1;
double value=resist[i];
double min=resist[i];
while(i<=numValues){
if(value<min) min=value;
value=resist[i++];
}
outfile<<"The minimum value is"
<<fixed<<setprecision(2)<<setw(18)<<min<<"\n";
}
void calcMean(double resist[], int numValues, ofstream& outfile){
int i=1;
double sum=0;
while(i<=numValues){
sum+=resist[i];
i++;
}
outfile<<"The mean is"
<<fixed <<setprecision(6)<<setw(31)
<<sum/numValues<< endl;
}
void geomMean(double resist[], int numValues, ofstream& outfile){
int i=1;
double geo=pow(resist[i],1./numValues);
i++;
while(i<=numValues){
geo*=pow(resist[i],1./numValues);
i++;
}
outfile<<"The geometric mean is"
<<fixed<<setprecision(6)<<setw(21)
<<geo<<endl;
}
void rmsAvg(double resist[], int numValues, ofstream& outfile){
int i=1;
double sum=0;
while(i<=numValues){
sum+=resist[i]*resist[i];
i++;
}
outfile<<"The harmonic mean is"
<<fixed<<setprecision(6)<<setw(22)
<<sqrt((1./numValues)*sum)<<"\n";
}
void harmMean(double resist[], int numValues, ofstream& outfile){
int i=1;
double sum=0;
while(i<=numValues){
sum+=1./resist[i];
i++;
}
outfile<<"The rms average is"
<<fixed<<setprecision(6)<<setw(24)
<<numValues/sum<<"\n";
}
void sortValues(double resist[],int numValues){
for(int I=0;I<numValues;I++){
for(int II=0;II<numValues-1;II++){
if(resist[II]>resist[II+1]){
double temp=resist[II+1];
resist[II+1]=resist[II];
resist[II]=temp;
}
}
}
}
| |