In: Computer Science
Question 2 : Aggregate Scores: Write a program that opens 2
files, quiz.txt, and hw.txt. The program will
read each file and aggregate these values into the associated class
variables. Using a list of Students, the
program keeps track of each student's scores. The program will then
write out to the scores.txt file, the
students score in homework, quizzes, and their overall score.
Related input files.
Each student is supposed to have 3 quiz scores and 3 homework
scores. If a score is not present in the file,
then it defaults to 0 points.
Files will be formatted as a Name followed by a tab followed by a
score.
Note: Refer to the Pre-Lab for the student class, and how to work
with classes.
How to Calculate Grades:
Homework Percent can thus be calculated as:
Homework_Points/3
Quiz Percent can be calculated in the same way:
Quiz_Points/3
Student’s overall score will be computed using the following
formula
HW_Score_Percent * .5 + Quiz_Score_Percent * .5
These scores work as each assignment is out of 100. Thus the
homework percent is calculated as
hw_percent = student.HW/300 * 100, which equals hw_percent =
student.HW/3
Required Design Schematic:
You must use the Student class. You must use a list of students.
You must not use global variables.
● Write a function find_student(student_list, name)
o Either returns a student or an index to the student in the
list
o Should return None if a student with name is not found
● Write a function get_HW_Scores(file_name, student_list)
o Opens the file
o Reads the file
▪ Divide the line into a name and score
▪ Finds the student
▪ Adds the score to the students homework score
o Closes the file
● Write a function get_Quiz_Scores (file_name, student_list)
o Opens the file
o Reads the file
▪ Divide the line into a name and score
▪ Finds the student
▪ Adds the score to the students quiz score
o Closes the file
● Write a function assign_grade(score)
o Returns a string containing a letter grade matching the percent,
see the week 3 lab for scoring
brackets
o Feel free to copy your lab function for use here
● Write a function output_Scores(student_list)
o Opens the file scores.txt
o Loops over every student in the list
▪ Writes the students name + "\n"
▪ Writes "HW_Percent: " + str(hw_percent) + "% \n"
▪ Writes "Quiz_Percent: " + str(quiz_percent) + "% \n"
▪ Calculate num to be the overall score for the student
▪ Writes "Overall: " + str(num) + "%" "(" + assign_grade(num) + ")"
+ "\n"
o Closes the file
● Write a function main to call the above and other functions as
needed to complete the task
Note: You are allowed to create as many helper functions as you
need.
Sample Input File:
Apple 100
Cube 69
Apple 100
Cube 50
Apple 100
Circle 85
Circle 89
Circle 88
Sample Output File:
Apple
HW_Percent: 100.0%
Quiz_Percent: 83.33333333333333%
Overall: 91.66666666666666%(A-)
Cube
You have not mentioned about the grade scale so i assume you can add that function of your own i.e. assign_grade(score) ....
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int total;
class Student
{
private:
float quiz_marks;
float hw_marks;
public:
char name[100];
Student(){
quiz_marks = 0.0f;
hw_marks = 0.0f;
memset(name,0,sizeof(name));
}
float getquizmarks(){
return quiz_marks;
}
void setquizmarks(float val){
quiz_marks = val;
}
float gethwmarks(){
return hw_marks;
}
void sethwmarks(float val){
hw_marks = val;
}
};
void get_HW_Scores(const char* file_name, Student student_list[]
, int*count);
void get_Quiz_Scores (const char* file_name, Student
student_list[], int* count);
void output_Scores( Student student_list[]);
int getIndex(Student student_list[],char* name, int max);
int main()
{
const char* quiz_file = "quiz.txt";
const char* hw_file = "hw.txt";
int count = 0, i=0;
Student student_list[200];
get_HW_Scores(hw_file,student_list,&count);
get_Quiz_Scores(quiz_file,student_list,&count);
total = count;
output_Scores(student_list);
return 0;
}
void get_HW_Scores(const char* file_name, Student student_list[] ,
int* count)
{
char name[100] = {'\0'};
float marks;
ifstream input(file_name);
int i =-1,index = -1 ;
while(input >> name){
input >> marks;
index = getIndex(student_list,name,i);
//index = -1 means a new student entry
if(index == -1){
i++;
student_list[i].sethwmarks(student_list[i].gethwmarks() +
marks);
strncpy(student_list[i].name,name,strlen(name));
}
else{
student_list[index].sethwmarks(student_list[index].gethwmarks() +
marks);
}
memset(name,0,sizeof(name));
}
*count = i;
}
void get_Quiz_Scores(const char* file_name, Student
student_list[], int* count)
{
char name[100] = {'\0'};
float marks;
ifstream input(file_name);
int i = *count,index = -1 ;
while(input >> name){
input >> marks;
index = getIndex(student_list,name,i);
//index = -1 means a new student entry
if(index == -1){
i++;
student_list[i].setquizmarks(student_list[i].getquizmarks() +
marks);
strncpy(student_list[i].name,name,strlen(name));
}
else{
student_list[index].setquizmarks(student_list[index].getquizmarks()
+ marks);
}
memset(name,0,sizeof(name));
}
*count = i;
}
int getIndex(Student student_list[], char* name,int max)
{
int i = 0;
if(max == -1)
return -1;
while(i <= max){
if(!strcmp(student_list[i].name,name)){
break;
}
else
i++;
}
if(i > max)
return -1;
else
return i;
}
void output_Scores(Student student_list[])
{
const char* score_file = "scores.txt";
ofstream outfile;
outfile.open(score_file);
int i =0 ;
float overall =0.0f ;
while(i <= total){
outfile<< student_list[i].name << "\n";
outfile<< "HW_Percent: " <<
(student_list[i].gethwmarks() / 3.0) <<"% \n";
outfile<< "Quiz_Percent: " <<
(student_list[i].getquizmarks() / 3.0) <<"% \n";
overall = ((student_list[i].gethwmarks() / 3.0) * 0.5f) +
((student_list[i].getquizmarks() / 3.0) * 0.5f);
outfile<< "Overall: " << overall <<"%"
<<"\n";
i++;
}
outfile.close();
}