#include #include #include using namespace std; //Concepts: Top-down programming, functions, arrays. //This program reads in from a file whose first line states the number //of exams the student took, and the next line lists that many exam scores. //The program computes the average of these scores with the highest and lowest scores //dropped, and reports the grade and letter grade to an output file. //Read in the grades from the file, put them in the array. void readGradesIntoArray(ifstream &infile, double grades[], int n ) { for (int i = 0; i < n; i++) { infile >> grades[i]; } } //Return highest value in grades array from index start to end double findHighest(double grades[], int start, int end) { double highest = grades[start]; for (int i = start; i <= end; i++) { if (grades[i] > highest) { highest = grades[i]; } } return highest; } //Return the smallest value in grades from index start up to end double findLowest(double grades[], int start, int end ) { double lowest = grades[start]; for (int i = start; i <= end; i++) { if (grades[i] < lowest) { lowest = grades[i]; } } return lowest; } //Add up all numbers in grades from s to e, return sum double sumArray(double grades[], int start, int end) { int total = 0; for (int i = start; i <= end; i++) { total = total + grades[i]; } return total; } string figureOutLetterGrade(double grade) { string letter; if (grade >= 90) letter = "A"; else if (grade >= 80) letter = "B"; else if (grade >= 70) letter = "C"; else if (grade >= 60) letter = "D"; else letter = "F"; return letter; } int main() { //Open input file, and file for output ifstream infile("exams.txt"); ofstream outfile("finalgrade.txt"); //Get number of scores from file int numScores; infile >> numScores; //Get the grades from file, store in array? //double grades[numScores]; //This won't compile! The "static" array declartion method double* grades = new double[numScores]; //a dynamic way to create array. readGradesIntoArray(infile, grades, numScores); //Figure out the highest score double high; high = findHighest(grades, 0, numScores - 1); //Figure out the lowest score double low; low = findLowest(grades, 0, numScores - 1); //Sum up all the numbers.. substract out highest and lowest double sum; sum = sumArray(grades, 0, numScores - 1); //Divide sum by numItems-2 to get average (which is the grade value). double finalAvg; finalAvg = sum / (numScores - 1); //Figure out letter grade from grade value string letterGrade; letterGrade = figureOutLetterGrade(finalAvg); //write answers (grade and grade value) to file outfile << finalAvg << " " << letterGrade << endl; return 0; }