First, the text structure does not include how many test scores are in the Tests
ID: 3556250 • Letter: F
Question
First, the text structure does not include how many test scores are in the Tests array for each student. We will assume that each student can have a different number of scores stored in the dynamic array pointed to by Tests. Therefore, we will add in a member with the number of test scores:
Member Name
Description
Name
Student name
Idnum
Student ID number
Test Number
Number of scores in Tests array
Tests
Pointer to an array of test scores
Average
Average test score
Grade
Course grade
Instead of asking a user for data, the data will be read in from a file, TopicHin.txt. The first record in the file is called a header record. This record has the number of records in the file. This allows you to have a size for the dynamic array of structures. Each subsequent line in the file is a student record. More information on these records will be given under the appropriate function description.
Here are the minimum functions to have in the file:
You can assume that all the data is present for each student, that there will be the correct number of scores, and that the scores are all positive values. You can also assume that integers will be present when integers are expected.
There may be more records than the header record number indicated. This is not considered an error. In this case, read the number of records indicated by the header record value. However, fewer records is a problem. This error should be returned to main and main should terminate the program.
This function also calculates the average for each student and determines the student's grade. This data is stored in the student structure.
Member Name
Description
Name
Student name
Idnum
Student ID number
Test Number
Number of scores in Tests array
Tests
Pointer to an array of test scores
Average
Average test score
Grade
Course grade
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student{
string name;
string idnum;
int testNumber;
int *tests;
double average;
char grade;
};
ifstream in;
int read_header();
Student *allocate(int);
void deallocate(Student*);
void readRecords(Student*, int);
void print(Student*,int);
int main(int argc, char const *argv[])
{
int n = read_header();
if(n==-1){
return 0;
}
Student *s = allocate(n);
readRecords(s,n);
for(int i=0;i<n;i++){
print(s,i);
}
deallocate(s);
in.close();
return 0;
}
int read_header(){
string s;
int a;
while(true){
cout<<"Please enter the input filename --> ";
cin>>s;
in.open(s.c_str(),ifstream::in);
if(in.is_open()){
in>>a;
if(a<5){
return -1;
}
return a;
break;
}
}
return -1;
}
Student *allocate(int a){
return new Student[a];
}
void deallocate(Student *s){
delete[] s;
}
void readRecords(Student *s, int a){
for(int i=0;i<a;i++){
int sum = 0;
in>>s[i].name;
in>>s[i].idnum;
in>>s[i].testNumber;
s[i].tests = new int[s[i].testNumber];
for(int j=0;j<s[i].testNumber;j++){
in>>s[i].tests[j];
sum+=s[i].tests[j];
//cout<<sum<<endl;
}
s[i].average = sum/(s[i].testNumber);
if(s[i].average>=91){
s[i].grade = 'A';
}else if(s[i].average>=81){
s[i].grade = 'B';
}else if(s[i].average>=71){
s[i].grade = 'C';
}else if(s[i].average>=60){
s[i].grade = 'D';
}else{
s[i].grade = 'F';
}
}
}
void print(Student *s,int i){
cout<<"Name : "<<s[i].name<<" ID Number: "<<s[i].idnum<<endl;
cout<<"Test Scores: ";
for(int j=0;j<s[i].testNumber;j++){
cout<<s[i].tests[j]<<" ";
}
cout<<" Average: "<<s[i].average<<" Grade: "<<s[i].grade;
cout<<" ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.