Grading program using structs; ordering by rank and by name

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;
}
What you need is an array of Student not an array of quiz_scores. Further more you need to sort the Student array according to the entry in question.
Cool thanks. I think I know what you're saying.
What I don't think I'm fully understanding is how an array of Student will account for five quiz scores.
Hi,

There is a problem with your letter_grade function. You are using the double parameter grade , but then assign a char to to it, before returning it as a char. IMO, that is not not good, have 2 variables - each with the correct type.

Also, there are holes in the values you are comparing: If grade was 89.5 say (or any other value > 89.0 and < 90.0) then the function won't actually return anything with the logic you have - the same goes for other values such as 79->80, 69->70.

If you were to cast the grade to an int type then you could use the logic you have.

What I don't think I'm fully understanding is how an array of Student will account for five quiz scores.


Your struct could contain a member which is an array of 5 doubles, if each student has five scores.

Hope all goes well :+)
Thnx for the input TheIdeasMan ;)

I didn't know that I couldn't use a different type for a function than the type of its parameter. I used double because as you can see the variable that is passed into letter_grade is the sum of all double type functions so I guess that's why I did that. As for the logic in letter_grade I did come across an issue with different values but I'll get to that after I can figure out the array of structs concept. It doesn't seem I can get around creating an array member for my struct without creating a variable for each quiz score within Student struct. I'm working this now. Same idea the only difference is that I'm providing the input instead of file I/O. I can't figure out how to code the struct array member in such a way that I can sort it however I want and get the same output I put in my opening post.
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
 
#include <iostream>
#include <string>
using namespace std;

struct Student{
	string name;
	double quiz_scores[5];
	double midterm1, midterm2, final_grade;
	int id;
};

void s_sort(double x[], int n) {
	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;
	}
}

double quiz_grade(double passed_array[], int size){
	double weighted_quiz_avgs = 0;
	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);
}

int main(){
	Student student[5];
	const int SIZE = 5; 	
	int i, j;
	for(i = 0; i < 5; i++){
		cout <<"Enter name: ";
		cin >>student[i].name;
		cout <<"Enter id: ";
		cin >>student[i].id;
		for(j = 0; j < 5; j++){
			cout <<"Enter Quiz Score(5): ";
			cin>> student[i].quiz_scores[j];
		}
		s_sort(student[i].quiz_scores, SIZE);//call to sort quiz scores
		cout <<"Enter Midterm 1: ";
		cin >>student[i].midterm1;
		cout <<"Enter Midterm 2: ";
		cin >>student[i].midterm2;
		cout <<"Enter Final Exam Score: ";
		cin >>student[i].final_grade;
		
		cout <<quiz_grade(student[i].quiz_scores, SIZE) <<endl;
	}
	
	/*for(i = 0; i < 5; i++){
		cout <<"Name: " <<student[i].name <<" ID:"<<student[i].id <<endl;
		for(j = 0; j < 5; j++){
			cout <<"Quiz Score " <<j + 1 <<":" <<student[i].quiz_scores[j] <<endl;
		}
		cout <<"Midterm 1: " <<student[i].midterm1 <<" Midterm 2: " <<student[i].midterm2 <<" Final: " <<student[i].final_grade <<endl;
	}*/
	
	
}
Last edited on
Topic archived. No new replies allowed.