I need to change the AvgGrade2 function to work with what is shown, but i do not know what to edit for it to work correctly.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct ClassList { // Definition of a structure called ClassList
// ClassList is the "tag"
// following are the "members" of the structure
string StudentName;
int StudentID;
double TestGrades[10];
double Avg;
};
//void AvgGrade1 (ClassList &, int );
void AvgGrade2 (ClassList [], int );
int main()
{
int i, No_Tests, No_Students, j;
ClassList Student1, // Declare a variable of type ClassList (just like int x) (memory=amount needed for structure)
*Students; // Declare a pointer to a variable of type ClassList
// ****************************************************
// *Array of structures with dynamic memory allocation*
// ****************************************************
cout << "How many students are there --> ";
cin >> No_Students;
Students = new ClassList [No_Students]; //Creates an array of structures. So there is a structure for each student.
for (j = 0; j < No_Students; j++) { //For each student get everything needed. (for loop keeps track of how many students.)
cin.ignore(10000,'\n');
cout << "What is the name of student # " << j+1 << " --> ";
getline (cin, Students[j].StudentName);
cout << "What is the student's ID number --> ";
cin >> Students[j].StudentID; //Content of whats being pointed to by the pointer. First member of the first structure.
cout << "How many tests were taken --> ";
cin >> No_Tests;
for (i = 0; i < No_Tests; i++) {
cout << "What was the grade on test " << i+1 << " --> ";
cin >> Students[j].TestGrades[i];
}
AvgGrade2 (Students, No_Tests); //Sends the entire structure to the function.
cout << "Test average for " <<
Students[j].StudentName << " with ID " <<
Students[j].StudentID << " is " << Students[j].Avg << endl;
}
delete [] Students; //DeAllocate memory.
cout << endl;
return 0;
}
/*// Passing a structure to a function by reference
void AvgGrade1 (ClassList &xyz, int x)
{
double sum=0.0;
for (int i = 0; i < x; i++)
sum += xyz.TestGrades[i];
xyz.Avg = sum / x; //Average
}*/
// Passing a structure to a function using a pointer
void AvgGrade2 (ClassList *xyz, int x) // Passing a structure to a function
{ // Pointer syntax
double sum=0.0;
for (int i = 0; i < x; i++)
sum += xyz[i].TestGrades[i];
xyz[i].Avg = sum / x;
}