Question: c++. i need help passing my average function as a pointer to my main function #include #include #include using namespace std; //prototypes void getUserValues(student &);
c++. i need help passing my average function as a pointer to my main function
#include
//prototypes void getUserValues(student &); double getCalculations(student, *stu); void getDisplayInfo(student, double);
struct student { string name; int id; double marks[4]; };
int main() { const int SIZE = 4; char answer; double avg;
cout << "This program accepts 4 test scores for the student." << endl; cout << "It the calculates the average of the 3 highest scores" << endl << endl; do { student stu; getUserValues(stu); student* studentPtr = &stu;
double avg = getCalculations(studentPtr); double *avgPtr = &avg;
//output results getDisplayInfo(stu, avg);
cout << "-------------------------------------------------------------------------" << endl; cout << "press y to RESTART, other wise the other key will close program." << endl; cin >> answer;
} while (answer == 'y' || answer == 'Y');
system("pause"); return 0;
}
void getUserValues(student &stu) { int NumTest = 4; cout << "Enter students name" << endl; getline(cin, stu.name);
cout << "Enter id number" << endl; cin >> stu.id;
//get the 4 student scores for (int size = 0; size <4; size++) { cout << "enter marks for test " << size + 1 << endl; cin >> stu.marks[size]; } }
double getCalculations(student *stu, int SIZE) {
double total = 0; double avg; int topScores = 3; double lowest = stu.marks[0]; for (int i = 0; i < SIZE; i++) { if (stu.marks[i] < lowest) lowest = stu.marks[i]; }
for (int i = 0; i<4; i++) { cout << stu.marks[i] << ","; avg += stu.marks[i]; //adding all the values }
avg = (total - lowest) / topScores; }
void getDisplayInfo(student stu, double avg) {
cout << "Name.................................." << stu.name << endl; cout << "Id number............................." << stu.id << endl; cout << "Test grades..........................."; for (int i = 0; i<4; i++) { cout << "test score" << i + 1 << endl; cout << stu.marks[i] << endl;
}
cout << "Average of three highest scores is............"; cout << std::setprecision(2) << avg << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
