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
|
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <math.h>
using namespace std;
const int SIZE = 100;
void readData (double data[], int& numbers);
void calcData (double data[], int numbers, double& arithMean, double& rootMean, double& maxHeight);
void printReport (double data[], int numbers, double arithMean, double rootMean, double maxHeight);
int main()
{
double maxHeight, data[SIZE], arithMean, rootMean;
int numbers;
readData (data, numbers);
calcData (data, numbers, arithMean, rootMean, maxHeight);
printReport (data, numbers, arithMean, rootMean, maxHeight);
return 0;
}
void readData (double data[], int& numbers)
{
ifstream inputFile;
inputFile.open("surface.txt");
if (inputFile.fail())
{
cout <<"Error opening input file: " <<"surface.txt, file may not be in the correct location.";
exit(1);
}
inputFile >> numbers;
for (int i = 0; i < numbers; i++)
{
inputFile >> data[i];
}
inputFile.close();
return;
}
void calcData (double data[], int numbers, double& arithMean, double& rootMean, double& maxHeight)
{
double sum = 0;
maxHeight = data[0];
for (int i=1; i<numbers; i++)
{
if (data[i] > maxHeight)
maxHeight = data[i];
}
for (int i = 0; i< numbers; i++)
{
sum += fabs(data[i]);
}
arithMean = sum/numbers;
for (int i = 0; i< numbers; i++)
{
rootMean = sqrt (pow (data[i], 2)/numbers);
}
return;
}
void printReport (double data[], int numbers, double arithMean, double rootMean, double maxHeight)
{
cout.setf(ios::showpoint | ios::fixed);
cout.precision(2);
ofstream outputFile;
outputFile.open("Roughness.txt");
outputFile <<"Surface Roughness Data" <<endl;
for (int i = 0; i< numbers; i++)
{
outputFile << data[i]<<"µm"<<endl;
cout << data[i]<<"µm"<<endl;
}
outputFile <<"Arithmetic Mean = " << arithMean <<" µm"<<endl;
cout <<"Arithmetic Mean = " << arithMean <<" µm"<<endl;
outputFile <<"Root-Mean Square = " <<rootMean <<" µm"<<endl;
cout <<"Root-Mean Square = " <<rootMean <<" µm"<<endl;
outputFile <<"Maximum Roughness = " <<maxHeight <<" µm"<<endl;
cout <<"Maximum Roughness = " <<maxHeight <<" µm"<<endl;
outputFile.close();
return;
}
|