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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
void header (ofstream &);
void readData (ifstream &, int [], float [], int &);
void printData (ofstream &, int [], float [], int &);
void findLow (ofstream &, int [], float [], int &);
void findHigh (ofstream &, int [], float [], int &);
void findAverage (ofstream &, int [], float [], int &);
void sortLow (ofstream &, int [], float[], int &);
void sortHigh (ofstream &, int [], float[], int &);
void footer (ofstream &);
int main()
{
int studID[50]; // declare program variables and arrays
float testScore[50];
int count = 0;
ifstream inFile; // open files for input and output
inFile.open("data.txt");
ofstream outFile;
outFile.open("outFile.txt");
header(outFile); //displays header on outfile
readData(inFile, studID, testScore, count); // get student IDs and test scores
outFile << "The original list of test scores is:" << endl << endl;
outFile << "Student ID #" << " " << "Test Score" << endl;
printData(outFile, studID, testScore, count); // print out the students' information
outFile << endl;
outFile << "The test scores sorted high to low are:" << endl << endl;
sortLow(outFile, studID, testScore, count);
printData(outFile, studID, testScore, count); // print out the students' information
outFile << endl;
outFile << "The student ID numbers sorted low to high are:" << endl << endl;
sortHigh(outFile, studID, testScore, count);
printData(outFile, studID, testScore, count); // print out the students' information
outFile << endl;
findLow(outFile, studID, testScore, count); // find the student with the lowest test score
findHigh(outFile, studID, testScore, count); // find the student with the highest test score
findAverage(outFile, studID, testScore, count); // find the average of the students' scores
footer(outFile); // displays footer on outfile
system("pause");
return 0;
} //end main function
//***************************** FUNCTION HEADER *****************************
void header (ofstream &outFile)
{
return;
}
// ************************* END OF FUNCTION HEADER *************************
//*******************************************************************************
void readData (ifstream &inFile, int studID[], float testScore[], int &count)
{
// GIVEN: input file, empty studID array, empty testScore array
// TASK: read the students ID and their test score
// RETURNS: the student ID and test score from the file
int id;
inFile >> id;
while (id > 0)
{
studID[count] = id;
inFile >> testScore[count];
count++;
inFile >> id;
}
return;
}
//*******************************************************************************
void printData (ofstream &outFile, int studID[], float testScore[], int &count)
{
// GIVEN: output file, empty studID array, empty testScore array
// TASK: print out the students' IDs and test scores
// RETURNS: nothing
int size;
for (count = 0; count < 50; count++)
{
if (studID[count] > 0)
outFile << setw(8) << studID[count] << setw(17) << setprecision(4) << testScore[count] << endl; // writes to outfile
}
return;
}
//*******************************************************************************
void sortLow (ofstream &outFile, int studID[], float testScore[], int &count)
{
// GIVEN:
// TASK:
// RETURNS:
int m;
int n;
int tempScore;
int tempID;
for (m = 0; m < 50; m++)
{
for (n = 50; n > m; n--)
if (studID[n] > 0 && testScore[n] > testScore[n-1])
{
tempScore = testScore[n];
tempID = studID[n];
testScore[n] = testScore[n-1];
studID[n] = studID[n-1];
testScore[n-1] = tempScore;
studID[n-1] = tempID;
}
}
return;
}
//*******************************************************************************
void sortHigh (ofstream &outFile, int studID[], float testScore[], int &count)
{
// GIVEN:
// TASK:
// RETURNS:
int m;
int n;
int min;
int tempScore;
int tempID;
for ( m = 0; m < 50; m++)
{
min = m;
for ( n = 50; n > m; n--)
if (studID[n] > 0 && studID[n] < studID[min])
{
min = n;
tempID = studID[m];
tempScore = testScore[m];
studID[m] = studID[min];
testScore[m] = testScore[min];
studID[min] = tempID;
testScore[min] = tempScore;
}
}
return;
}
//*******************************************************************************
void findLow (ofstream &outFile, int studID[], float testScore[], int &count)
{
// GIVEN: empty studID array, empty testScore array
// TASK: find the student with the lowest test score
// RETURNS: the student ID with the lowest score
int min = 0;
for (count = 0; count < 50; count++)
{
if (studID[count] > 0 && testScore[count] < testScore[min])
min = count;
}
outFile << "The lowest test score was " << testScore[min] << "% achieved by student " << studID[min] << endl;
return;
}
//*******************************************************************************
void findHigh (ofstream &outFile, int studID[], float testScore[], int &count)
{
// GIVEN: empty testScore array
// TASK: find the student with the highest test score
// RETURNS: the student ID with the highest score
int max = 0;
for (count = 0; count < 50; count++)
{
if (testScore[count] > testScore[max])
max = count;
}
outFile << "The highest test score was " << testScore[max] << "% achieved by student " << studID[max] << endl;
return;
}
//*******************************************************************************
void findAverage (ofstream &outFile, int studID[], float testScore[], int &count)
{
double total = 0;
double average = 0;
count = 0;
do // add to total while the student ID is positive
{
total += testScore[count];
count++; // increments count
}
while (studID[count] > 0);
average = total / count;
outFile << "The average test score for the group is " << setprecision(4) << average << "%" << endl; // writes to outfile
return;
}
//***************************** FUNCTION FOOTER *****************************
void footer (ofstream &outFile)
{
outFile << endl;
outFile << setw(35) << "-------------------------" << endl;
outFile << setw(35) << " END OF PROGRAM OUTPUT " << endl;
outFile << setw(35) << "-------------------------" << endl;
return;
}
// ************************* END OF FUNCTION FOOTER *************************
|