This is an assignment I wasn't able to fully implement. I'm reading in students' scores and have to rank them according to their score and name. Originally we were told to use a class of students but I want to use a structure instead. What I am having trouble with is ordering them by their rank and name. This is what the output should look like:
Enter an input file: C:\\Temp\\scores.txt
--------------------------------------------------
Course Report: Numerical Average Order
--------------------------------------------------
Alice 2000 - 100.00 (A) (rank: 1)
Tom 1000 - 88.05 (B) (rank: 2)
Alice 1500 - 80.00 (B) (rank: 3)
John 3000 - 80.00 (B) (rank: 3)
Jason 2500 - 50.00 (F) (rank: 5)
---------------------------------------------------
--------------------------------------------------
Course Report: First Name Order
--------------------------------------------------
Alice 1500 - 80.00 (B) (rank: 3)
Alice 2000 - 100.00 (A) (rank: 1)
Jason 2500 - 50.00 (F) (rank: 5)
John 3000 - 80.00 (B) (rank: 3)
Tom 1000 - 88.05 (B) (rank: 2)
---------------------------------------------------
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
|
//libraries
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//structure
struct Student{
string name;
int id;
//variables I read all score values in from file I/O
double quiz_scores[5], midterm_score1, midterm_score2, final_score;
};
//gets called from "main" after scores array are streamed in
double quiz_grade(double passed_array[], int size){//size is five; number of students
double weighted_quiz_avgs = 0;// variable to calculate running sum
for (int j = 1; j < size; j++){
weighted_quiz_avgs += passed_array[j];
//cout << "WQA's: "<<weighted_quiz_avgs<<" " <<endl;
}
return 20.0 * (weighted_quiz_avgs / 40.0);//calculates weighted percentage of total grade
}
//similar to "quiz_grade" in that it finds weighted chunk of overall grade
double mid_term_grade(double grade1, double grade2){
double midterm_grade;
midterm_grade = (grade1 + grade2) / 200.00;
return 40.0 * midterm_grade;
}
//same as two previous functions
double final_grade(double score){
double final_grade;
final_grade = score / 100.00;
return 40.0 * final_grade;
}
char letter_grade(double grade){//called in main with variable assigned with sum of all functions' values
if (grade <= 100 && grade >= 90){
grade = 'A';
return grade;
}
if (grade <= 89 && grade >= 80){
grade = 'B';
return grade;
}
if (grade <= 79 && grade >= 70){
grade = 'C';
return grade;
}
if (grade <= 69 && grade >= 60){
grade = 'D';
return grade;
}
if (grade < 60 && grade > 0){
grade = 'F';
return grade;
}
}
void s_sort(double x[], int n) {//sorts five quiz scores
int m; // keep the index of current smallest value
double hold;
for (int k=0; k<=n - 1; k++) {
m = k;
for (int j=k+1; j <= n-1; j++) {
if (x[j] < x[m])
m = j;
}
hold = x[m];
x[m] = x[k];
x[k] = hold;
//cout << "x[" << k <<"]:" <<x[k] <<endl;
}
}
int main()
{
Student a;
int i, j;
double numeric_average;//used to assign sum of a returned function values
const int SIZE = 5;
string file;//for I/O
ifstream in_file;//I/O variable used to open file
in_file.open("C:\\Temp\\cst231.txt");
if(in_file.fail()){
cout <<"Error: file open failed.\n";
}
cout <<"--------------------------------------------------\n";
cout <<" Course Report: Numerical Average Order\n";
cout <<"--------------------------------------------------\n";
for(i = 0; i < SIZE; i++){//
in_file >> a.name >>a.id;
for(j = 0; j < 5; j++){
in_file >>a.quiz_scores[j];
//cout <<a.quiz_scores[j] <<endl;
}
//call sort
s_sort(a.quiz_scores, SIZE);//call to sort quiz scores
in_file >>a.midterm_score1 >>a.midterm_score2 >>a.final_score;
numeric_average = quiz_grade(a.quiz_scores, SIZE) + mid_term_grade(a.midterm_score1, a.midterm_score2) + final_grade(a.final_score);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << a.name <<" " <<a.id <<" - " <<numeric_average <<" " <<"(" <<letter_grade(numeric_average) <<") " <<endl;
}
in_file.close();
return 0;
}
|