Question

In: Computer Science

Question 2 : Aggregate Scores: Write a program that opens 2 files, quiz.txt, and hw.txt. The...

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

Solutions

Expert Solution

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();
}


Related Solutions

C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
This is a request for Java High Scores Program with 2 files. Rewrite the high scores...
This is a request for Java High Scores Program with 2 files. Rewrite the high scores assignment(code below) so that the names and scores are stored in an array of HighScore objects instead of parallel ArrayLists. The new Program should have the same output as the original. Here is a sample run of the program: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score #2: Kim Enter the score for...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
Edit question Write a program that merges two files as follows. The two files are in...
Edit question Write a program that merges two files as follows. The two files are in the docsharing which you can download it. One file will contain usernames(usernames.txt):foster001smith023nyuyen002...The other file will contain passwords(passwords.txt):x34rdf3ep43e4rddw32eds22...The program should create a third file matching username and passwords(usernamesPasswords.txt):foster001x34rdf3esmith023p43e4rddnyuyen002w32eds22......Give the user of your programs the option of displaying you output file. CAN ANYONE SOLVE THIS IN C
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
Write a program in C++ using the following 2 Files: Book.h and Source.cpp 1.)   In Book.h,...
Write a program in C++ using the following 2 Files: Book.h and Source.cpp 1.)   In Book.h, declare a struct Book with the following variables: - isbn:   string - title:    string - price: float 2.)   In main (Source.cpp), declare a Book object named: book - Use an initialization list to initialize the object with these values:           isbn à 13-12345-01           title à   “Great Gatsby”           price à 14.50 3.)   Pass the Book object to a function named: showBook - The...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered. Your program should check to make sure that the file was opened successfully, and terminate if it was not.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT