Question

In: Computer Science

Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files...

Objectives:
 Write classes in C++
 Use dynamic arrays
 Write and read from files
1. WriteaclassGradeBookcontainingthefollowing: Private attributes:
- courseName: a string representing the name of the course.
- nbOfStudents: an integer representing the number of students enrolled in the course. The
number of students is greater than or equal to 5.
- grades: a double dimensional array of integers representing the grades of Test1, Test2 and
Final of every student. It should be a dynamic array. Public Functions:
- A no-arg constructor that initializes the nbStudent to 5, the courseName to empty string and the array grades to an array of 5 rows and 3 columns. This constructor should call the setGradeBook function.
- A constructor that takes as parameters the number of students, the course name and the array grades and sets the corresponding values by calling setGradeBook.
- A copy constructor that takes an object of class GradeBook and copies the data to the instantiated object.
- A destructor that deletes the array grades.
- setGradeBook that takes all parameters and sets the attributes values.
- getMaximum that returns the highest grade in the array grades.
- getAverage that takes a one dimensional array representing the three grades of a student, one row in the array grades, and returns the average of the student.
- display function that takes an ostream and returns the following output:

Solutions

Expert Solution

I have implemented GradeBook class as per the given description.


PLEASE FIND THE FOLLOWING CODE SCREENSHOT, OUTPUT, AND CODE.

ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT

1.CODE SCREENSHOT:

2.OUTPUT:

3.CODE:

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<iomanip>
#include<fstream>
using namespace std;
class GradeBook{
        private:
        //VARIABLES TO STORE THE courseName,nbOfStudents,grades
        string courseName;
        int nbOfStudents;
        double **grades;
        public:
        /* A no-arg constructor that initializes the nbStudent to 5, 
        the courseName to empty string and the array grades to an 
        array of 5 rows and 3 columns.
        */
        GradeBook(){
                courseName="";
                nbOfStudents=5;
                grades=new double*[nbOfStudents];
                for(int i=0;i<nbOfStudents;i++)
                        grades[i]=new double[3];
        }
        /*A constructor that takes as parameters the number of students, 
        the course name and the array grades and sets the corresponding 
        values by calling setGradeBook
        */
        GradeBook(int nofSt,string name,double **g){
                courseName=name;
                nbOfStudents=nofSt;
                grades=new double*[nbOfStudents];
                for(int i=0;i<nbOfStudents;i++)
                        grades[i]=new double[3];
                setGradeBook(g);
        
        }
        /*A copy constructor that takes an object of class GradeBook and copies the data to the instantiated object.*/
        GradeBook(const GradeBook &g){
                courseName=g.courseName;
                nbOfStudents=g.nbOfStudents;
                grades=new double*[nbOfStudents];
                for(int i=0;i<nbOfStudents;i++)
                        grades[i]=new double[3];
                setGradeBook(g.grades);
                
        }
        // destructor that deletes the array grades.
        ~GradeBook(){
                delete[] grades;
        }
        //- setGradeBook that takes all parameters and sets the attributes values.
        void setGradeBook(double **g){
                int i=0,j=0;
                for(i=0;i<nbOfStudents;i++)
                        for(j=0;j<3;j++)
                                grades[i][j]=g[i][j];
        }
        //- getMaximum that returns the highest grade in the array grades.
        double getAverage(double *g){
                return (g[0]+g[1]+g[2])/3;
        }
        /*- getAverage that takes a one dimensional array 
        representing the three grades of a student, one row in the array grades, 
        and returns the average of the student.*/
        double getMaximum(double **g){
                int i=0,j=0;
                double max=0;
                for(i=0;i<nbOfStudents;i++)
                        for(j=0;j<3;j++)
                        {
                                if(grades[i][j]>max)
                                        max=grades[i][j];
                        }
                return max;
        }
        //display the result ot the require stream
        void display(ostream &out){
                out<<"The Grade Book"<<endl<<"Course Name : "<<courseName<<endl<<"Number Of Studnets : "<<nbOfStudents<<endl;
                out<<setw(8)<<left<<"Test 1"<<setw(8)<<"Test 2"<<setw(8)<<"Final"<<setw(8)<<"Agerage"<<endl;
                for(int i=0;i<nbOfStudents;i++){
                        
                        for(int j=0;j<3;j++)
                        {
                                out<<setw(8)<<grades[i][j];
                        }
                        out<<setw(8)<<setprecision(4)<<getAverage(grades[i]);
                        out<<endl;
                }
                out<<"Maximum Grade is :"<<getMaximum(grades);
        }
};
int main(){
        double **grades;
        ofstream out("output.txt");
        grades=new double*[5];
        srand(time(0));
        //generate random grades
        for(int i=0;i<5;i++)
                grades[i]=new double[3];
        for(int i=0;i<5;i++)
                for(int j=0;j<3;j++)
                        grades[i][j]=40+rand()%60;
        //create the grade book
        GradeBook g(5,"CPP",grades);
        //display the output
        g.display(cout);
        //save the output to a file
        g.display(out);
        cout<<"\nThis Output is saved into a file 'output.txt'";
}

Related Solutions

You are required to use C++ static or dynamic arrays of characters to store c-strings. You...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You are NOT allowed to use any C++ string data type variable for any purpose. Moreover, you are allowed to add any include directive. You are not allowed to include string, cstdlib or math libraries. Also, you are not allowed to use any built-in functions of c-strings. can someone help with the third, fourth, and fifth functions? I tried many ways but i cannot figure...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You are NOT allowed to use any C++ string data type variable for any purpose. Moreover, you are allowed to add any include directive. You are not allowed to include string, cstdlib or math libraries. Also, you are not allowed to use any built-in functions of c-strings. can someone help with the third, fourth, and fifth functions? I tried many ways but i cannot figure...
Skills needed to complete this assignment: dynamic arrays, classes. PROMPT: In mathematics, a polynomial is an...
Skills needed to complete this assignment: dynamic arrays, classes. PROMPT: In mathematics, a polynomial is an expression consisting of cariables and coefficients which involves only the operation of addition, subtraction, multiplication, and non-negative integer exponents of variables. A polynomial in a single variable can always be written in the form: anxn + an-1xn-1+ ....... + a2x2 + a1x + a0 Where a0 ....., an are coefficients and x is the variable. In this assignment, you will complete a polynomial class...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files into a new C++ project.    NOTE: if your IDE does not allow you to create projects - or you are not sure how to do it - then you may combine the two cpp files into a single file. 2) Complete the program by adding code the the class methods. You may want to study the existing code first to get a feel...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);....
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);. Code a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered .The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Eg: Enter a sequence of characters: abAa1121X+&$%$dc[space] Distinct characters are: abA12X+&$%dc. Use C++
Write a C++ app to read both files, store them into parallel vectors, sort the list...
Write a C++ app to read both files, store them into parallel vectors, sort the list of people in alphabetical order, display the new sorted list of names with their corresponding descriptions. Use the Bubble Sort strategy to rearrange the vector(s). File 1: Marilyn Monroe Abraham Lincoln Nelson Mandela John F. Kennedy Martin Luther King Queen Elizabeth II Winston Churchill Donald Trump Bill Gates Muhammad Ali Mahatma Gandhi Margaret Thatcher Mother Teresa Christopher Columbus Charles Darwin Elvis Presley Albert Einstein...
IN C++, IN C++, IN C++, IN C++, IN C++ Write the A and B classes,...
IN C++, IN C++, IN C++, IN C++, IN C++ Write the A and B classes, the properties of which will produce the expected output with the test code provided, and the characteristics of which are specified below. Note the types of pointers used in the test code. Methods of class A: - void hi () - Prints “Hi A” on the screen. - void selam() - Prints "Selam A" on the screen. Class B must inherit class A. Class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT