It needed to be done C++ Program Thank you in advance. PROJECT#2 [Student Grades
ID: 3788152 • Letter: I
Question
It needed to be done C++ Program
Thank you in advance.
PROJECT#2 [Student Grades] Due Date: Displayed on TRACS 100 points CS 2308 Department of Computer Science, Texas State Universit The program will print a grade report for students in a course. Input: An instructor has a class of no more than 40 students each of whom takes 6 tests. For each student in the class, there is one line in the input file. The line contains the student's first name (no more than 10 characters), last name (no more than 12 characters), ID# a string of 6 characters) and 6 integer test scores. Input text file should be named student input.dat Example of file input: Adam Zeller 452313 78 8691 64 90 76 Barbara Young 274253 88 77 91 66 82 93 Carl Wilson 112235 87 99 76 93 95 94 Note that a different file will be used for testing. Also note that amount of blank spaces between names, ID, and grades can be arbitrary, i.e., any number. There will be at least one space though. There might be a space between the last grade and the invisible symbol that signifies the end of the line "/n'. Total amount of characters for each line will not exceed 256, including the end of the line symbol Processing: The program is to read the input file and calculate each student's average and letter grade for the course. The average is calculated by dropping the student's lowest test score and then averaging the remaining 5 scores. In addition, the number of students receiving each letter grade (A, B, C, D, or F for the course is to be calculated. The cutoff for the letter grades is 89.5 for an A, 79.5 for a B, 69.5 for a C, 59.5 for a D output: The program is to print to an output file (student results.dat) a table showing each student's name (Last, First), identification number, course average (rounded to 1 decimal place) and letter grade. Following the last student, a summary of the number of students receiving each letter grade is to be printed. All output should be clearly labeled and easy to read. The output file should have a meaningful heading at the top of the file Example of file output Last name First name ID Average Score Grade 452313 Zeller Adam 82.3Explanation / Answer
The following are the descriptions of the functions:
readFile: reads student_input.dat file and reads each record. Process each record and calculates average based on the given criteria and grade. Finally updates the student_results.dat file with this information
updateGradeCount: updates the grade count of the member variables
printTotalGradeCount: prints the count for each grade
dropMinValue: places "-1" at the minimum value location in the array for each record
calcAverage: This function is triggered when the grade count is >=5. If grade count is 6, then It calls the dropMinValue member function to place -1 for the min value. Otherwise it will simply calculates the average for the 5 elements.
Code:
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
class StudentGrade
{
char *inFileName;
char *outFileName;
int array[6];
int id;
char firstName[10];
char lastName[12];
/*
Gradecount : index 0 - A grade count
index 1 - B grade count
index 2 - C grade count
index 3 - D grade count
index 4 - F fail count
*/
int gradeCount[5];
public:
StudentGrade(char *in, char *out="student_results.dat")
{
inFileName=new char[25];
outFileName=new char[25];
id=0;
for(int i=0;i<5;i++)
gradeCount[i]=0;
for(int i=0;i<6;i++)
array[i]=0;
strcpy(inFileName,in);
strcpy(outFileName,out);
}
void readFile(){
FILE *fp,*ofp;
int index=0;
int arr_size=0;
fp=fopen(inFileName,"r");
ofp=fopen(outFileName,"w");
if(fp==NULL|| ofp==NULL){
cout<<"Error in opening the file"<<endl;
return;
}
char line[256];
char * tokptr;
char grade;
double average;
while(fgets(line,256,fp)!=NULL){
cout<<line<<endl;
tokptr=strtok(line," ");
memset(firstName,'',10);
memset(lastName,'',12);
index=0;
arr_size=0;
while(tokptr!=NULL){
switch(index)
{
case 0:
strcpy(firstName,tokptr);
break;
case 1:
strcpy(lastName,tokptr);
break;
case 2:
id=atoi(tokptr);
cout<<"id:"<<id<<endl;
break;
default:
array[arr_size]=atoi(tokptr);
if(array[arr_size]<0)
{
fclose(fp);
fclose(ofp);
cout<<"Negative grades present. Exiting.."<<endl;
return;
}
arr_size++;
}
index++;
tokptr=strtok(NULL, " ");
}
//Skipping the student record having total grades <5
if(arr_size<5){
continue;
}
average=calcAverage(arr_size);
grade=updateGradeCount(average);
cout<<"Average:"<<average<<endl;
cout<<"Grade:"<<grade<<endl;
fprintf(ofp,"%s %s %d %f %c ",lastName,firstName,id,average,grade);
}
fclose(fp);
fclose(ofp);
}
void printTotalGradeCount(){
for(int i=0;i<5;i++)
printf("%C: %d ", 65+( i<=3? i: 5), gradeCount[i]);
}
char updateGradeCount(double average){
if(average >=89.5){
gradeCount[0]++;
return 'A';
}
else if(average>=79.5){
gradeCount[1]++;
return 'B';
}
else if(average>=69.5){
gradeCount[2]++;
return 'C';
}
else if(average>=59.5){
gradeCount[3]++;
return 'D';
}
else{
gradeCount[4]++;
return 'F';
}
}
void dropMinValue(){
int min=array[0];
int index=0;
for(int i=1;i<6;i++)
if(min>array[i]){
min=array[i];
index=i;
}
array[index]=-1;
}
double calcAverage(int size)
{
int sum=0;
int new_size=5;
if(size>5)
dropMinValue();
for(int i=0;i<size;i++){
cout<<array[i]<<",";
if(array[i]==-1) continue;
sum=sum+array[i];
}
cout<<endl;
return (double) sum/new_size;
}
~StudentGrade(){
delete inFileName;
delete outFileName;
}
};
int main(void){
StudentGrade sg("student_input.dat");
sg.readFile();
sg.printTotalGradeCount();
return 0;
}
student_input.dat:
Adam Zeller 452313 78 86 91 65 90 76
Barbara Young 274253 88 77 91 66 92 93
Carl Wilson 112235 99 76 93 95 94
output:
Adam Zeller 452313 78 86 91 65 90 76
id:452313
78,86,91,-1,90,76,
Average:84.2
Grade:B
Barbara Young 274253 88 77 91 66 92 93
id:274253
88,77,91,-1,92,93,
Average:88.2
Grade:B
Carl Wilson 112235 99 76 93 95 94
id:112235
99,76,93,95,94,
Average:91.4
Grade:A
A: 1
B: 2
C: 0
D: 0
F: 0
student_results.dat:
Zeller Adam 452313 84.200000 B
Young Barbara 274253 88.200000 B
Wilson Carl 112235 91.400000 A
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.