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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
struct StudentData
{
string first_name;
string last_name;
int exam1;
int exam2;
int exam3;
int total;
char ch;
};
const int SIZE = 9;
const int INFO = 4;
// Function prototypes
void openInputFile(ifstream &, string);
void getTotal(StudentData[]);
void getGrade(StudentData[]);
void calcLowest(StudentData[], int &, int &, int &, int &, int[]);
void calcHighest(StudentData[], int &, int &, int &, int &, int[]);
void getAverage(StudentData[], int, double &, double &, double &, double &, double[]);
void getStd(StudentData[], double &, double &, double &, double &, double &, double &, double &, double &, double[]);
void print(StudentData[], int[], int[], double[], double[]);
void sort(StudentData[]);
int main()
{
// Variables
StudentData arr[SIZE];
int lowest1, lowest2, lowest3, lowest4; // Stores lowest exam scores
int highest1, highest2, highest3, highest4; // Holds highest exam scores
double average1 = 0, average2 = 0, average3 = 0, average4 = 0; // Represents average of each exam
double std1 = 0, std2 = 0, std3 = 0, std4 = 0; // Holds standard deviation for Exams 1-3 and Total
int lowest[INFO] = {};
int highest[INFO] = {};
double average[INFO] = {};
double standardDeviation[INFO] = {};
ifstream inFile;
string inFileName = "C:\\Users\\Christian\\Desktop\\ClassGrades.txt";
// Call function to read data in file
openInputFile(inFile, inFileName);
// Read data into an array of structs
for (int count = 0; count < SIZE; count++)
{
inFile >> arr[count].first_name >> arr[count].last_name >> arr[count].exam1 >> arr[count].exam2 >> arr[count].exam3;
}
// Close input file
inFile.close();
// Get score total for each student
getTotal(arr);
// Determine grade for each student
getGrade(arr);
// Calculate lowest scores in each exam and total scores
calcLowest(arr, lowest1, lowest2, lowest3, lowest4, lowest);
// Calculate highest scores in each exam and total scores
calcHighest(arr, highest1, highest2, highest3, highest4, highest);
// Calculate average of each exam and the average of the total scores
getAverage(arr, SIZE, average1, average2, average3, average4, average);
// Calculate standard deviation of each category
getStd(arr, std1, std2, std3, std4, average1, average2, average3, average4, standardDeviation);
cout << "\n";
// Print unsorted data
print(arr, lowest, highest, average, standardDeviation);
cout << "\n";
// Sort data
sort(arr);
// Print sorted data
print(arr, lowest, highest, average, standardDeviation);
system("PAUSE");
return 0;
}
/**
* Pre-condition:
* Post-condition:
*/
void openInputFile(ifstream &inFile, string inFileName)
{
//Open the file
inFile.open(inFileName);
//Input validation
if (!inFile)
{
cout << "Error to open file." << endl;
cout << endl;
return;
}
}
/**
* Pre-condition:
* Post-condition:
*/
void getTotal(StudentData arr[])
{
for (int i = 0; i < SIZE; i++)
{
arr[i].total = arr[i].exam1 + arr[i].exam2 + arr[i].exam3;
}
}
/**
* Pre-condition:
* Post-condition:
*/
void getGrade(StudentData arr[])
{
for (int i = 0; i < SIZE; i++)
{
if (arr[i].total >= 90)
arr[i].ch = 'A';
else if (arr[i].total >= 80)
arr[i].ch = 'B';
else if (arr[i].total >= 70)
arr[i].ch = 'C';
else if (arr[i].total >= 60)
arr[i].ch = 'D';
else
arr[i].ch = 'F';
}
}
/**
* Pre-condition:
* Post-condition:
*/
void calcLowest(StudentData arr[], int &lowest1, int &lowest2, int &lowest3, int &lowest4, int lowest[])
{
int smallest = 0;
lowest1 = arr[0].exam1;
lowest2 = arr[0].exam2;
lowest3 = arr[0].exam3;
lowest4 = arr[0].total;
// Loop to determine lowest score from Exam1, 2, 3, and Total
for (int i = 0; i < SIZE; i++)
{
if (lowest1 > arr[i].exam1)
{
lowest1 = arr[i].exam1;
smallest = i;
}
if (lowest2 > arr[i].exam2)
{
lowest2 = arr[i].exam2;
smallest = i;
}
if (lowest3 > arr[i].exam3)
{
lowest3 = arr[i].exam3;
smallest = i;
}
if (lowest4 > arr[i].total)
{
lowest4 = arr[i].total;
smallest = i;
}
}
// Loop lowest values into an array of size 4
for (int index = 0; index < INFO; index++)
{
if (index == 0)
lowest[0] = lowest1;
else if (index == 1)
lowest[1] = lowest2;
else if (index == 2)
lowest[2] = lowest3;
else if (index == 3)
lowest[3] = lowest4;
else
cout << "Fail!" << endl;
}
}
/**
* Pre-condition:
* Post-condition:
*/
void calcHighest(StudentData arr[], int &highest1, int &highest2, int &highest3, int &highest4, int highest[])
{
int biggest = 0;
highest1 = arr[0].exam1;
highest2 = arr[0].exam2;
highest3 = arr[0].exam3;
highest4 = arr[0].total;
// Loop to determine highest score from Exam1, 2, 3, and Total
for (int i = 0; i < SIZE; i++)
{
if (highest1 < arr[i].exam1)
{
highest1 = arr[i].exam1;
biggest = i;
}
if (highest2 < arr[i].exam2)
{
highest2 = arr[i].exam2;
biggest = i;
}
if (highest3 < arr[i].exam3)
{
highest3 = arr[i].exam3;
biggest = i;
}
if (highest4 < arr[i].total)
{
highest4 = arr[i].total;
biggest = i;
}
}
// Loop highest values into an array of size 4
for (int index = 0; index < INFO; index++)
{
if (index == 0)
highest[0] = highest1;
else if (index == 1)
highest[1] = highest2;
else if (index == 2)
highest[2] = highest3;
else if (index == 3)
highest[3] = highest4;
else
cout << "Fail!" << endl;
}
}
/**
* Pre-condition:
* Post-condition:
*/
void getAverage(StudentData arr[], int size, double &average1, double &average2, double &average3, double &average4, double average[])
{
int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
// Get sum of each category (Exam1, 2, 3, and Total)
for (int i = 0; i < SIZE; i++)
{
sum1 += arr[i].exam1;
sum2 += arr[i].exam2;
sum3 += arr[i].exam3;
sum4 += arr[i].total;
}
// Calculate average for each category
average1 += static_cast<double>(sum1) / size;
average2 += static_cast<double>(sum2) / size;
average3 += static_cast<double>(sum3) / size;
average4 += static_cast<double>(sum4) / size;
// Loop average values into an array of size 4
for (int index = 0; index < INFO; index++)
{
if (index == 0)
average[0] = average1;
else if (index == 1)
average[1] = average2;
else if (index == 2)
average[2] = average3;
else if (index == 3)
average[3] = average4;
else
cout << "Fail!" << endl;
}
}
/**
* Pre-condition:
* Post-condition:
*/
void sort(StudentData arr[])
{
StudentData temp;
for (int i = 0; i < (SIZE - 1); i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (arr[i].last_name > arr[j].last_name)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
|