Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to get grades for an exam and calculate the average of the e

ID: 3686637 • Letter: W

Question

Write a C++ program to get grades for an exam and calculate the average of the exam.
(About Classes with Pointer Member variables and writing destructor and copy constructor for such classes.)

1) Complete the body of the examType(examType &theOtherExam) copy constructor
2) Complete the body of the ~examType()
3) Print average for midterm2 (hint: the average for midterm1 and midterm2 should be the same if you have written
the copy constructor for examType class correctly).

Below is the code that I have so far:
#include <iostream>
#include <string>

using namespace std;

// this class is implemented to hold the grades for an exam
class examType
{

private:
   int noOfStudents; // this is the size of the dynamic array
   double *grades; // grades is a pointer the the dynamic array

public:
   // constructor with parameters
   examType(int studentCountInTheExam)
   {
       noOfStudents = studentCountInTheExam;
       grades = new double[noOfStudents];
   }

   // copy constructor
   examType(examType &theOtherExam)
   {
     

   }

   // destructor
   ~examType()
   {
      
}

  
   int getNoOfStudents()
   {
       return noOfStudents;
   }

   void setGrade(int thePosition, double newGrade)
   {
       grades[thePosition] = newGrade;
   }

   double getGrade(int thePosition)
   {
       return grades[thePosition];
   }

   double getAverage()
   {
       double total = 0;
       for (int i=0; i < noOfStudents; i ++)
       {
           total +=grades[i];
       }

       return total/noOfStudents;
   }
  
  

};


int main()
{
   int size;

   // ask number of students taking the exam
   cout << "Enter no of students took the midterm: ";
   cin >> size;

   examType midterm1(size);
  
   //-----------------------------

   // get grades from the user
   for (int i = 0; i< size; i ++)
   {
       double newGrade;

       cout << "Enter grade for student " << (i+1) << ": ";
       cin >> newGrade;
       midterm1.setGrade(i, newGrade);
   }

   // print the grades
   cout << endl << "Printing grades " << endl;
   for (int i =0; i< size; i ++)
   {
      
       cout << "Student " << (i+1) << ": "
           <<   midterm1.getGrade(i) << endl;
   }

   // print Average
   cout << "The midterm1 average is: " << midterm1.getAverage() << endl;
  

   // Declaring another examType object called midterm2
   // and initialize midterm2 by using the value of midterm1 object
   // NOTE THAT, the line below automatically invokes the copy constructor of the class examType
   // for midterm2. That is, midterm1 becomes theOtherExam in the copy constructor.
   examType midterm2 (midterm1);
  
  
  

   system("pause");
   return 0;
}

____________________________________________

Thank you.

Explanation / Answer

Hi I have implemented all required functionlity.


#include <iostream>
#include <string>

using namespace std;

// this class is implemented to hold the grades for an exam
class examType
{

private:
   int noOfStudents; // this is the size of the dynamic array
   double *grades; // grades is a pointer the the dynamic array

public:
   // constructor with parameters
   examType(int studentCountInTheExam)
   {
       noOfStudents = studentCountInTheExam;
       grades = new double[noOfStudents];
   }

   // copy constructor
   examType(examType &theOtherExam)
   {
       noOfStudents = theOtherExam.noOfStudents;
       grades = new double[noOfStudents];
       for(int i=0; i<noOfStudents; i++)
           grades[i] = theOtherExam.grades[i];
   }

   // destructor
   ~examType()
   {
      delete [] grades;
   }


   int getNoOfStudents()
   {
       return noOfStudents;
   }

   void setGrade(int thePosition, double newGrade)
   {
       grades[thePosition] = newGrade;
   }

   double getGrade(int thePosition)
   {
       return grades[thePosition];
   }

   double getAverage()
   {
       double total = 0;
       for (int i=0; i < noOfStudents; i ++)
       {
           total +=grades[i];
       }

       return total/noOfStudents;
   }

};


int main()
{
   int size;

   // ask number of students taking the exam
   cout << "Enter no of students took the midterm: ";
   cin >> size;

   examType midterm1(size);

   //-----------------------------

   // get grades from the user
   for (int i = 0; i< size; i ++)
   {
       double newGrade;

       cout << "Enter grade for student " << (i+1) << ": ";
       cin >> newGrade;
       midterm1.setGrade(i, newGrade);
   }

   // print the grades
   cout << endl << "Printing grades " << endl;
   for (int i =0; i< size; i ++)
   {
    
       cout << "Student " << (i+1) << ": "
           <<   midterm1.getGrade(i) << endl;
   }

   // print Average
   cout << "The midterm1 average is: " << midterm1.getAverage() << endl;

   // Declaring another examType object called midterm2
   // and initialize midterm2 by using the value of midterm1 object
   // NOTE THAT, the line below automatically invokes the copy constructor of the class examType
   // for midterm2. That is, midterm1 becomes theOtherExam in the copy constructor.
   examType midterm2 (midterm1);


   //system("pause");
   return 0;
}


/*

sample run:

Enter no of students took the midterm: 2
Enter grade for student 1: 65
Enter grade for student 2: 89

Printing grades
Student 1: 65
Student 2: 89
The midterm1 average is: 77

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote