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

To do the average grade calculation for each of the students, your AvgGrade2 function should call AvgGrade1 for each element of the array.

So, you need to pass to AvgGrad2 the array, the number of students, and the number of tests. Use a loop in the function, sending each student in turn to the function that does the actual calculation.

Val

xyz should be an array of ClassList objects each of which has an array of test results in it. Each ClassList object stands for a single student. You want to calculate the average test result for each student withing xyz. So you need a nested loop, the outer one to keep track of which student in xyz and the inner one to keep track of individual test results.
double sum;

void AvgGrade2 (ClassList *xyz, int x, int y)
{
  for(int stu = 0; stu < y; ++stu)
 {
   sum = 0.0;
   for (int i = 0; i < x; i++)
   {
      sum += xyz[stu].TestGrades[stu];
   }
   xyz[i].Avg = sum / x;
}
This article has been dead for over six months. Start a new discussion instead.