Hey guys, im tryig to finish a homework project so i can study it for an upcomin
ID: 3700862 • Letter: H
Question
Hey guys, im tryig to finish a homework project so i can study it for an upcoming test, but ive come across this problem that im not sure how to write a code for. i need a copy of code, and output in C++. below ive attached what the program needs to do. thanks so much!
Create arrays of class objects for 15 student records including student ID’s, student names, pointers point to each of student’s test scores, and the average of each student’s test scores.
The student records must be in a file for input including students ID’s, and student names. The file path in the desktop can be located by file property, for example:
“C:\Users\yourUsersName\Desktop\filename.txt”
The 10 testScores of each student may be initialized in the program body or stored in a second file for input.
After inputting the file into arrays of class objects, calculate the average score of each student. The call function for average must be a member function of the student class.
Print out student ID’s, student names, average scores and the associated 10 testScores of each student.
Your class declaration should include student records as member data of the class. Your function definition should include the average function and other functions as you desire as member functions of the class.
Must use class specification (.h) and class implementation (.cpp). There will be no credit if specification file and implementation file are not used.
Explanation / Answer
ScreenShot
--------------------------------------------------------------------------------------------------
//Header file
#include <iostream>
#include <iomanip>
#include <fstream>
#include<string>
using namespace std;
// Declare structure(s)
struct StudentAverage {
string name; // Name of student
int id; // Student ID number
int* tests; // Pointer to an array of test scores
double average; // Average of test scores
};
// Function Prototype(s)
int inputTests(); // Asks user for number of test scores
int *inputScores(int); // Asks user for the test scores for each student
double calcAverage(int*, int); // Calculates the average test score for each student
void display(StudentAverage*, int); // Displays each student's name, ID#, average test score, and course g
//Main method
int main() {
// Create a dynamic array of the GradeBook structure. Array size is based upon user input given in
// the inputStudents function.
StudentAverage *studentList;
int size = 15;
/*string name[15];
int idNum[15];*/
string name;
int idNum;
studentList = new StudentAverage[size];
int x;
string y;
// Check for possible memory allocation errors. If error is found, end program.
if (studentList == NULL) {
cout << "Memory Allocation Error!";
system("pause");
return 0;
}
ifstream inFile; //file object creation
//Open fie for reading
inFile.open("C:/Users/deept/Desktop/Students.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
//Read data into an array
/*for (int i = 0; i < size; i++) {
inFile >> name[i]>>idNum[i];
}*/
// Create a variablearray to hold the number of test scores for each student.
int numOfTests = inputTests();
//Loop to add data into structure
for (int count = 0; count <= size; count++) {
while(inFile>>name>>idNum)
studentList[count].name =name;
studentList[count].id = idNum;
studentList[count].tests = inputScores(numOfTests);
studentList[count].average = calcAverage(studentList[count].tests, numOfTests);
}
//Display result
display(studentList, size);
delete[] studentList;
return 0;
}
//User can enter score number
int inputTests() {
int tests;
cout << "How many tests will there be?" << endl;
cin >> tests;
return tests;
}
//Enter scores
int *inputScores(int numOfTests) {
int *scores;
scores = new int[numOfTests];
cout << "Enter the test scores for the student. Press ENTER after each score." << endl;
for (int count = 0; count < numOfTests; count++) {
cout << "Score " << (count + 1) << ": ";
cin >> scores[count];
}
return scores;
}
//Calculate average
double calcAverage(int *testScores, int numOfTests) {
int total = 0;
double average;
for (int count = 0; count < numOfTests; count++) {
total += testScores[count];
}
average = total / numOfTests;
return average;
}
//Display result
void display(StudentAverage* studentList, int size) {
for (int count = 0; count <size; count++)
{
cout<<studentList[count].name << " " << studentList[count].id << " "<< studentList[count].average << endl;
}
}
--------------------------------------------------------------------------------
Note