use an array of pointers to reference a set of three double type arrays, which w
ID: 3769287 • Letter: U
Question
use an array of pointers to reference a set of three double type arrays, which will represent three different engineering courses. Also, two more arrays of size 3 need to be created. Program Requirements: Your program will begin by displaying your information using the header and footer functions you have previously created. Declare three different arrays in the main() as follow: a. An array of pointers of size 3. Each pointer (within the array of pointers) will point to a double type arrays in the heap. b. An array of string that will hold on to three course names: ES1036, ES1050 and ES1021. c. An array of double type variable of size 3 to hold on to the class average for the three different courses. Prompt the user to enter the class-size for each course. Based on that response, create an array in the heap and point to it with one of the pointers (from the array of pointers, you already created). Declare and define a function with the prototype void populateAndFindAverage(double *, double &, int); which will take care of the array (created in the heap) with the aid of its first formal parameter, keep track of the course average with the aid of the second parameter, and take care of the array size with the help of the third parameter. Inside this function and with the help of the user, the array will be populated with grades, and the average of that course will be calculated. Page 6 of 11 Now, in the main (), your code will find the highest and lowest course-average and corresponding course name and display these data items on the screen. Create a second loop to release the dynamic memory. Make sure that you assign NULL value to each pointer after releasing each dynamic array. After completing the above tasks the program will be terminated. NOTE: You are not allowed to use any global variable for this code.
Explanation / Answer
Executable code:
#include <iostream>
#include <string>
using namespace std;
void myHeader(string name, int lab, string date, int question);
//print header
void populateAndFindAverage(double *, double &, int);
int main()
{
//Invoke myHeader function
myHeader("Kals vin", 8, "|Dec 12", 2);
//variables declaration
double *ary = new double[3];
string crse[3] = {"ES1036", "ES1050", "ES1021"};
double scr[3] = {0}; int sb, hi, lw;
cout<<"This program finds the class average of 3 courses and compare those averages"<<endl;
cout<<"The courses are: ES1036, ES1050 and ES1021"<<endl<<endl;
for (int i = 0; i < 3; i++)
{
cout<<"Enter class size for "<<crse[i]<<": ";
cin>>sb;
cout<<crse[i]<<" has been created successfully!"<<endl;
cout<<"This course has "<<sb<<" students."<<endl<<endl;
double *ptr = new double[sb];
ptr = &ary[i];
// Invoke function to calculate the average
populateAndFindAverage(ptr, scr[i], sb);
cout<<endl<<endl;
}
// code to find the highest,lowest average
hi = 0; hi = 1; hi = 2;
if (scr[0] > scr[1] && scr[0] > scr[2])
hi = 0;
else if(scr[1] > scr[0] && scr[1] > scr[2])
hi = 1;
else if(scr[2] > scr[1] && scr[2] > scr[0])
hi = 2;
if (scr[0] < scr[1] && scr[0] < scr[2])
lw = 0;
else if(scr[1] < scr[0] && scr[1] < scr[2])
lw = 1;
else if(scr[2] < scr[1] && scr[2] < scr[0])
lw = 2;
cout<<crse[hi]<<" course has the highest class average."<<endl;
cout<<crse[lw]<<" course has the lowest class average."<<endl<<endl;
//release the dynamic memory delete a;
ary = NULL;
cout<<"Good bye!"<<endl;
}
//myHeader function
void myHeader(string name, int lab, string date, int question)
{
cout<<"*********************************************************************"<<endl;
cout<<"Name: "<<name<<endl; cout<<"Lab #"<<lab<<", "<<date<<", Question #"<<question<<endl;
cout<<"*********************************************************************"<<endl<<endl;
}
//populateAndFindAverage function to find average
void populateAndFindAverage(double *ptr, double &scr, int sb)
{
for(int j = 0; j < sb; j++)
{
cout<<"Enter grade for student "<<j+1<<": ";
cin>>ptr[j]; scr += ptr[j];
}
scr = scr/sb;
cout<<"Class average: "<<scr<<endl;
}
Result:
*********************************************************************
Name: Kals vin
Lab #8, |Dec 12, Question #2
*********************************************************************
This program finds the class average of 3 courses and compare those averages
The courses are: ES1036, ES1050 and ES1021
Enter class size for ES1036: 2
ES1036 has been created successfully!
This course has 2 students.
Enter grade for student 1: 87
Enter grade for student 2: 89
Class average: 88
Enter class size for ES1050: 2
ES1050 has been created successfully!
This course has 2 students.
Enter grade for student 1: 65
Enter grade for student 2: 76
Class average: 70.5
Enter class size for ES1021: 2
ES1021 has been created successfully!
This course has 2 students.
Enter grade for student 1: 76
Enter grade for student 2: 78
Class average: 77
ES1036 course has the highest class average.
ES1050 course has the lowest class average.
Good bye!
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.