CODE IN C++: DO NOT USE STRUCTS, ONLY CLASSES The University of Southern North D
ID: 3838553 • Letter: C
Question
CODE IN C++:
DO NOT USE STRUCTS, ONLY CLASSES
The University of Southern North Dakota Testing department needs to write a grading program for multiple choice test. It should be capable of grading tests with up to 50 questions and for any number of students. The program should grade individual exams and calculate the grade for the student based on the number of questions, each being the same value out of 100. For example, if there are 25 questions each is worth 4.
The program must also determine how many times each score was earned on the test. This is a count or frequency. The program must should show the number of students graded and the class average.
The program should have
a single function (called with different parameters) to read the answer key and the student responses. The arrays are the same size and data type so only one function is needed.
a function that take the answer key and student response as parameters and then computes the score for that student. The score is returned as int by the function.
an input file. The program asks the user for the name of the file. The file name is entered at the keyboard. All subsequent data is read from the file.
a score frequency count. Every time a score is computed increment a counter for that value. Any score from 0 to 100 is possible (integer only), so the program needs 101 counters!
Behavior not assigned to functions may be done in main.
The program will accept the input file name from the keyboard after prompting the user for the file name.
The first line in the input files contains the number of questions on the exam, the second the answer key, and all following lines contain the student ids and responses to the questions.
The input shown here
20
2 1 1 3 4 4 5 3 2 1 2 4 5 2 1 1 3 4 4 2
12345 2 1 1 1 4 4 3 3 2 1 2 4 5 2 2 1 4 4 4 3
23456 2 1 1 2 3 4 5 3 2 1 2 4 5 2 1 2 3 4 4 1
14567 1 2 1 3 4 4 3 3 2 1 2 4 5 2 2 1 4 4 4 2
15678 2 1 2 3 3 4 5 3 1 1 3 4 5 2 1 1 3 4 4 2
16789 2 1 1 3 4 4 5 3 2 1 2 4 5 2 1 1 3 4 4 2
17890 2 1 1 3 4 4 5 3 2 1 2 4 5 2 2 1 4 4 4 3
12245 1 2 1 3 4 4 3 3 2 1 4 4 5 2 2 1 4 4 4 2
12256 2 1 2 3 3 4 5 3 1 1 2 4 5 2 1 1 3 4 4 2
22345 2 1 1 2 3 4 5 3 2 1 2 4 5 2 1 2 3 4 4 1
22456 1 2 1 3 4 4 3 4 2 1 2 4 5 2 2 1 4 4 4 2
13244 2 1 1 3 3 4 5 3 1 1 3 4 5 2 1 1 3 4 4 2
22458 2 2 1 3 4 4 5 3 2 1 2 4 5 2 1 1 3 4 4 2
23678 2 1 3 3 4 4 5 3 2 1 2 4 5 2 2 4 4 4 4 3
24567 2 1 2 3 3 4 5 3 2 1 2 4 5 1 1 2 3 3 4 1
11412 2 1 2 3 3 4 5 3 1 2 2 4 4 2 1 1 3 4 4 2
produces this output:
Enter file name:scantron.txt
Student ID Score
===================
12345 75
23456 80
14567 75
15678 80
16789 100
17890 85
12245 70
12256 85
22345 80
22456 70
13244 85
22458 95
23678 75
24567 70
11412 75
===================
Tests graded = 15
===================
Score Frequency
===================
100 1
95 1
85 3
80 3
75 4
70 3
===================
Class Average = 80
the solution to this is:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int MAX_SCORE = 100;
bool readarray(fstream &file, int array[], int size){
for(int i=0; i< size; i++)
file >> array[i];
return file.eof();
}
int gradeOneTest(int key[], int answers[], int size){
int misses = 0;
for(int i=0; i< size; i++)
if(key[i] != answers[i])
misses++;
return MAX_SCORE - (MAX_SCORE/size) * misses;
}
int main(){
fstream infile;
int key[50], answers[50], num_questions;
int frequency[101] = {0};
int studentID, score, numTests=0;
double average=0;
string filename;
cout << "Enter file name:";
cin >> filename;
infile.open(filename, ios::in);
infile >> num_questions;
readarray(infile, key, num_questions);
cout << "Student ID Score" << endl;
cout << "===================" << endl;
while(!infile.eof()){
infile >> studentID;
readarray(infile, answers, num_questions);
score = gradeOneTest(key, answers,num_questions) ;
frequency[score]++;
average += score;
cout << studentID << setw(12) << score << endl;
numTests++;
}
cout << "===================" << endl;
cout << "Tests graded = " << numTests << endl;
cout << "===================" << endl;
cout << "Score" << setw(14) <<"Frequency" << endl;
cout << "===================" << endl;
for(int i=100;i>0;i--)
if(frequency[i]!=0)
cout << setw(3) << i << setw(12)<< frequency[i] << endl;
cout << "===================" << endl;
cout << "Class Average = " << average / numTests << endl;
return 0;
}
Rewrite the Scantron program so that it uses a class called Student to hold the student ID and grade.
Make a second class called Class that contains an array of the Student class and an integer that stores how many Student class objects are in the array.
As each student is ID is read from the file add them to a student class in the array of student class.
As the grade for each student is calculated add it to the student’s class.
Adjust the rest of the program to use the array of classes in place is single integer values.
Sort the class of student structs in descending order by grade.
Do not output anything until all students have been processed and sorted.
Do not use anything from the STL. Do not use any sort routine other than one you code from scratch , you can use a bubble sort
Explanation / Answer
Here is the modified code to use classes. Please don't forget to rate the answer if it helped. Thank you very much.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
class Student
{
private:
int id;
int grade;
public:
Student()
{
id=0;
grade =0;
}
Student(int id1)
{
id = id1;
}
void setGrade(int grade1)
{
grade = grade1;
}
int getGrade()
{
return grade;
}
int getId()
{
return id;
}
};
class Class
{
private:
Student students[100];
int size;
public:
Class()
{
size = 0;
}
//adds a student object passed by refernce to the class. max number of students allowed is 100. if student was added, true
//is returned otherwise false.
bool addStudent(Student s)
{
if(size < 100)
{
students[size] = s;
size++;
return true;
}
else
return false;
}
//sorts the array of students in descending order of grades using bubble sort
void sort()
{
for(int i =0 ;i <size-1; i++)
{
for(int j=0; j<size-i-1; j++)
{
if(students[j].getGrade() < students[j+1].getGrade())
{
Student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
}
int getSize()
{
return size;
}
Student getStudent(int i)
{
return students[i];
}
};
const int MAX_SCORE = 100;
bool readarray(fstream &file, int array[], int size){
for(int i=0; i< size; i++)
file >> array[i];
return file.eof();
}
int gradeOneTest(int key[], int answers[], int size){
int misses = 0;
for(int i=0; i< size; i++)
if(key[i] != answers[i])
misses++;
return MAX_SCORE - (MAX_SCORE/size) * misses;
}
int main(){
fstream infile;
int key[50], answers[50], num_questions;
int frequency[101] = {0};
int studentID, score;
double average=0;
string filename;
Class myclass;
cout << "Enter file name:";
cin >> filename;
infile.open(filename, ios::in);
infile >> num_questions;
readarray(infile, key, num_questions);
Student s;
while(infile >> studentID){
Student s(studentID);
readarray(infile, answers, num_questions);
score = gradeOneTest(key, answers,num_questions) ;
s.setGrade(score);
myclass.addStudent(s);
}
//sort the student objects based on descending order of grades
myclass.sort();
cout<<"List of students sorted in descending order of grades"<<endl<<endl;
cout << "Student ID Score" << endl;
cout << "===================" << endl;
for(int i=0; i<myclass.getSize(); i++)
{
s = myclass.getStudent(i);
cout<<s.getId() << " " << s.getGrade() <<endl;
frequency[s.getGrade()]++;
average += s.getGrade();
}
cout << "===================" << endl;
cout << "Tests graded = " << myclass.getSize() << endl;
cout << "===================" << endl;
cout << "Score" << setw(14) <<"Frequency" << endl;
cout << "===================" << endl;
for(int i=100;i>0;i--)
if(frequency[i]!=0)
cout << setw(3) << i << setw(12)<< frequency[i] << endl;
cout << "===================" << endl;
cout << "Class Average = " << average / myclass.getSize() << endl;
return 0;
}
output
Enter file name:ans.txt
List of students sorted in descending order of grades
Student ID Score
===================
16789 100
22458 95
17890 85
12256 85
13244 85
23456 80
15678 80
22345 80
12345 75
14567 75
23678 75
11412 75
12245 70
22456 70
24567 70
===================
Tests graded = 15
===================
Score Frequency
===================
100 1
95 1
85 3
80 3
75 4
70 3
===================
Class Average = 80
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.