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 <string>
#include <iomanip>
using namespace std;
//function prototype and variables
const int numRows=5;
const int numColumns = 7;
void openFile(ifstream& , ofstream& );
void initialize(ifstream&, ofstream&, double[][numColumns], double[], string[]);
void milesAvg(ifstream& , ofstream&, double , double, string[]);
void print(ifstream& inFile, ofstream& outFile , double results[][numColumns],double average[], string names[]);
int main()
{
//declaring variables
ifstream inFile;
ofstream outFile;
string names[numRows];
double results[numRows][numColumns];
double average[numRows];
//call function
openFile(inFile,outFile);
initialize(inFile,outFile,results,average,names);
return 0;
}
//open file
void openFile(ifstream& inFile , ofstream& outFile)
{
inFile.open("run.txt");
outFile.open("newrun.txt");
}
void initialize(ifstream& inFile , ofstream& outFile, double results[][numColumns], double average[numRows], string names[numRows])
{
int a;
int i;
for(i=0; i<numRows; i++)
{
inFile >> names[i];
for(a=0; a< numColumns; a++)
{
inFile >> results[i][a];
cout << endl;
}
}
}
void milesAvg(ifstream& inFile , ofstream& outFile, double results[][numColumns],double average[numRows],string names[numRows])
{
//declaring variables
int i;
int j;
int sum;
//find the averages
for(i=0; i<numRows; i++)
{
sum=0;
for(j=0; j<numColumns; j++)
average[i] = average[i]+(results[i][j]/7);
cout << i << endl;
}
print(inFile, outFile,results,average,names);
}
void print(ifstream& inFile, ofstream& outFile, double results[numRows][numColumns], double average[numRows], string names[numRows])
{
//declaring variables
int k;
int l;
//print name of runners
for(k=0; l < numRows; l++ )
{
//write the names to outfile
outFile << names[k] << endl;
}
//print results
for(k=0; k < numRows; k++)
{
//write the results to outfile
for(l=0; l<numColumns; k++)
outFile << results[k][l];
outFile << endl;
}
//print average
for(k=0; k < numRows; k++)
//write the average to outfile
outFile << average[k];
inFile.close();
outFile.close();
}
|