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
|
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
const int MAX_ARRAY = 500;
void getData(ifstream& inFile, int inValues[], int& size);
double getAverage(int inValues[], int dataSize);
void compute_max_min [theData, numElems, max, min];
int main()
{
ifstream fileIn;
int max, min;
int theData[MAX_ARRAY]; // Array for data storage
int numElems; // Actual number of values in array
// (1 more than largest index)
fileIn.open("pgm5data.txt"); // Open file
if (fileIn.fail() ) // Test for file existence
{
cout << "Problem opening file";
exit(-1);
}
// Read file and count values in array
getData(fileIn,theData,numElems);
// Determine and write average; test first for non-empty data file
if (numElems > 0)
cout << "Average: " << getAverage(theData,numElems) << endl << endl;
else
cout << "ERROR: No data processed" << endl << endl;
compute_max_min[theData, numElems, max, min];
// Close file
fileIn.close();
return 0;
}
// This function reads integers from a file and stores the values in
// an array. It returns the loaded array and the number of elements
// in the array
void getData(ifstream& inFile, int inValues[], int& numVals)
{
int i = 0;
inFile >> inValues[i];
while (!inFile.eof() && i < MAX_ARRAY) // Test for end of file and array
{
i++;
inFile >> inValues[i];
}
numVals = i;
}
// This function receives an array of integers, calculates the average
// of the array values, and returns it.
double getAverage(int inValues[], int dataSize)
{
double sum = 0.0;
for (int i = 0; i < dataSize; i++)
sum = sum + inValues[i];
return sum / dataSize;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void compute_max_min (int theData[], int& dataSize, int& iCurrMin, int& iCurrMax)
{
int iCurrMax = 0;
for (int i = 1; i < dataSize; ++i) {
if (theData[iCurrMax] > theData[i])
iCurrMax = i;
int iCurrMin = 0;
for (int i = 1; i < dataSize; ++i) {
if (theData[iCurrMin] < theData[i])
iCurrMin = i;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double computer_stdDev (int theData[], int dataSize);
{
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void print_quartile ();
|