Question

In: Computer Science

Write the header and the implementation files (.h and .cpp separately) for a class called Course,...

  1. Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                   
  1. Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array to represent the GPAs of each student.
  1. Your class has the following member functions:

       

  1. a constructor with an int parameter representing the number of students; the constructor creates the dynamic variable and sets it to the int parameter, creates the two dynamic arrays and sets everything to zero.

  1. a function which reads in all the ids and the GPAs into the appropriate arrays.
  1. a function called print_info which, for each student, does all of the

                following:

                It prints the student’s id and GPA. If the student’s GPA is greater than or equal to 3.8, it prints “an honor student”.

  1. a destructor (hint: delete array)
  2. create 5 random instances of Course objects to represents 5 sessons of CSC211, each session includes 20 students, assign random score to each student, calculate the average GPA of all 100 students.

Solutions

Expert Solution

Ans: Since It is not specified in the question hence all the data members are declared as private and an extra utility function to get average of GPA per course has been created. Alternative to this is declare gpa array as public and calculating average in the main function.

Also, assignRandom function is created just to populate the values in each instance of the class and hence it is not an essential function.

Note: Read comments in the code to get it's description.

#include<bits/stdc++.h>
using namespace std;

// Course Class
class Course{
    private:
        int* number_of_students;
        int* sId;
        double* gpa;

    public:
        // Constructor dynamically assigns memory
        Course(int student_count){
            number_of_students = new int;
            *number_of_students = student_count;

            sId = (int*)malloc(sizeof(int)*student_count);
            gpa = (double*)malloc(sizeof(double)*student_count);

            for(int i = 0; i<student_count; i++){
                sId[i] = 0;
                gpa[i] = 0;
            }
        }

        // Function to add students at the end in a particular course
        void enroll(int Id, double gpa_score){
            int i = 0;

            while(i<*number_of_students && sId[i] != 0)
                i++;

            // If no more students can be added then return
            if(i == *number_of_students)
                return;
            
            sId[i] = Id;
            gpa[i] = gpa_score;
        }

        // Function prints the required info per course
        void print_info(){
            int n = *number_of_students;

            for(int i = 0; i<n; i++){
                cout<<sId[i]<<"\t"<<gpa[i]<<"\t";
                if(gpa[i] >= 3.8)
                    cout<<"An honor student";
                cout<<"\n";
            }
        }

        // Returns the average value of GPA in a particular course
        double getAverage(){
            double sum = 0.0;
            for(int i = 0; i<*number_of_students; i++)
                sum += gpa[i];

            return sum/(*number_of_students);
        }

        // Destructor function
        ~Course(){
            delete (number_of_students);
            delete [] sId;
            delete [] gpa;
        }
};


// Utility function to assign random values to the instance of the course class
Course * assignRandom(Course* obj, int size){
    int id;
    double gpa;

    for(int i = 0; i<size; i++){
        id = (rand()%100)+1;
        gpa = (rand()%5)+1;
        gpa = gpa+(id/100.0);

        obj->enroll(id, gpa);
    }
    return obj;
}

// Driver function
int main(){

    Course *c1 = new Course(20);
    Course *c2 = new Course(20);
    Course *c3 = new Course(20);
    Course *c4 = new Course(20);
    Course *c5 = new Course(20);

    c1 = assignRandom(c1, 20);
    c1->print_info();
    cout<<"\n";

    c2 = assignRandom(c2, 20);
    c2->print_info();
    cout<<"\n";

    c3 = assignRandom(c3, 20);
    c3->print_info();
    cout<<"\n";

    c4 = assignRandom(c4, 20);
    c4->print_info();
    cout<<"\n";

    c5 = assignRandom(c5, 20);
    c5->print_info();
    cout<<"\n";

    // Calculating and printing overall GPA
    double overAllGpa = (c1->getAverage()+ c2->getAverage() + c3->getAverage() + c4->getAverage() + c5->getAverage())/5;

    cout<<"OverAll average GPA: "<<overAllGpa<<"\n";

    return 0;
}

Related Solutions

Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are witten below) "KINGSOM.h " program below: #ifndef WESTEROS_kINGDOM_H_INCLUDED #define WESTEROS_KINGDOM_H_INCLUDED #include <iostream> namespace westeros { class Kingdom{         public:                 char m_name[32];                 int m_population; };         void display(Kingdom&);                      } #endif } Kingdom.cpp Program below: #include <iostream> #include "kingdom.h" using namespace std; namespace westeros {         void display(Kingdom& pKingdom) {                 cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;                                                               FINAL:...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
You are to write a class named Rectangle. You must use separate files for the header...
You are to write a class named Rectangle. You must use separate files for the header (Rectangle.h) and implementation (Rectangle.cpp) just like you did for the Distance class lab and Deck/Card program. You have been provided the declaration for a class named Point. Assume this class has been implemented. You are just using this class, NOT implementing it. We have also provided a main function that will use the Point and Rectangle classes along with the output of this main...
i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are going to build a C++ program which runs a single game of Rock, Paper, Scissors. Two players (a human player and a computer player) will compete and individually choose Rock, Paper, or Scissors. They will then simultaneously declare their choices and the winner is determined by comparing the players’ choices. Rock beats Scissors. Scissors beats Paper. Paper beats Rock. The learning objectives of this...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary search tree using the numbers of the sequence. The tree set should not have any nodes with same values and all repeated numbers of the random sequence must be stored in the node as a counter variable. For example, if there are five 20s’ in the random sequence then the tree node having...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class that holds the information for each item for the inn. GildedRose is a class that holds an internal listing of many Item objects. This inventory should hold at least 10 items. For this you can use arrays, the std::array class, or even the vector class. Complete the implementation of these classes, adding public/private member variables and functions as needed. You should choose an appropriate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT