Last active
December 26, 2015 06:49
-
-
Save EricTendian/7110554 to your computer and use it in GitHub Desktop.
Reads a file called grades.txt with a list of grades, one on each line, and outputs the average of all the numbers, as well as the lowest and highest score. Uses vectors to account for an unlimited number of grades.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
using namespace std; | |
double findAvg(vector<double>); | |
double min_max(vector<double>, bool); | |
int main() { | |
vector<double> grades; | |
ifstream in; | |
in.open("grades.txt"); | |
int currLine; | |
while (in >> currLine) grades.push_back(currLine); | |
in.close(); | |
cout << "Average of grades: " << findAvg(grades) << endl; | |
cout << "Lowest score: " << min_max(grades, false) << endl; | |
cout << "Highest score: " << min_max(grades, true) << endl; | |
} | |
double findAvg(vector<double> grades) { | |
double sum = 0; | |
double avg = 0; | |
for (int i = 0; i < grades.size(); i++) sum+=grades.at(i); | |
avg = double(sum) / double(grades.size()); | |
return avg; | |
} | |
double min_max(vector<double> grades, bool max = false) { | |
double val = grades.at(0); | |
for (int i = 0; i < grades.size(); i++) if ((max && (val < grades.at(i))) || (val > grades.at(i))) val = grades.at(i); | |
return val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment