954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with function

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;
}
Slate2006
Newbie Poster
18 posts since Oct 2007
Reputation Points: 10
Solved Threads: 2
 

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

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

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;
}
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

sorry, should have been:

sum += xyz[stu].TestGrades[i];

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You