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

IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
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...
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
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...
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined up horizontally in a row Each locker is numbered from 1 to 100 in sequential order Every locker can be fully closed (open state = 0.00) Every locker can be fully opened (open = 1.00) Every locker can be partially open with any possible value between 0.00 and 1.00 inclusive on both ends A locker cannot ever be more closed than fully closed (open...
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...
Using C++ Use dynamic allocation and functions (using pointer variables only) to read the names from...
Using C++ Use dynamic allocation and functions (using pointer variables only) to read the names from the .txt file and sort the names in lexical order Grumpy Dopey Doc Happy Bashful Sneezy Sleepy
I am Writing a C-Program to read and write files. but none of my code is...
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed. Thank you in advance //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void rfile(void); void wfile(void); struct personnel {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT