Question

hello there. can you please help me to complete this code. thank you. Lab #3 -...

hello there. can you please help me to complete this code. thank you.

Lab #3 - Classes/Constructors

Part I - Fill in the missing parts of this code

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

class classGrades

     {

     public:

           void printlist() const;

           void inputGrades(ifstream &);

           double returnAvg() const;

           void setName(string);

           void setNumStudents(int);

           classGrades();

           classGrades(int);

     private:

           int gradeList[30];

           int numStudents;

           string name;

     };

int main()

{

    

     classGrades intro(5), intermediate;

     intro.printlist();

     ifstream infile;

     infile.open("data.txt");

     if (!infile)

     {

           cout << "Error opening file!" << endl;

           exit(1);

     }

    

     //set the name of the classGrades object intro to CSC126

    

     //set the name of the object intermediate to CSC211

    

    

     intermediate.setNumStudents(4);

     //input the grades for intermediate from a file

    

//print the intermediate grades

     //print the average of the intermediate grades

    

}

void classGrades::printlist() const

{

     int i;

     cout<< "***" << name<< "***" <<numStudents<< endl;

     for (i =0; i < numStudents; i++)

           cout << i << ' ' << gradeList[i] << endl;

}

void classGrades::inputGrades(ifstream & in )

           {

                int i;

                for (i = 0; i< numStudents; i++)

                     in >> gradeList[i];

           }

double classGrades::returnAvg() const

           {

                int i;

                double average;

                double sum = 0;

                //write code to return the average

           }

          

void classGrades::setName(string n)

           {

                name = n;

           }

void classGrades::setNumStudents(int x)

           {

                numStudents = x ;

           }

classGrades::classGrades()

           {

                int i;

                name = "";

                numStudents = 30;

                for (i = 0; i< 30; i++)

                     gradeList[i] = 0;

           }

classGrades::classGrades(int n)

           {

                int i;

                name = "";

                numStudents = n;

                for (i = 0; i< n; i++)

                     gradeList[i] = 0;

           }

Part II - Syntax for arrays of objects

classGrades allCS[15]; //the default constructor is applied to                                     //all elements of this array

                             //to set the number of students in each class to 25

for (i = 0; i < 15; i++)

     {

           allCS[i].setNumStudents(25);

     }

     //Assume values in all classes. Write the code to print all class averages?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

please do upvote if you have any problem do comment and i shall be happy to help you.Thanks for using HomeworkLib.

-------------------------------------------

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

class classGrades

{

public:

void printlist() const;

void inputGrades(ifstream&);

double returnAvg() const;

void setName(string);

void setNumStudents(int);

classGrades();

classGrades(int);

private:

int gradeList[30];

int numStudents;

string name;

};

int main()

{

classGrades intro(5), intermediate;

intro.printlist();

ifstream infile;

infile.open("data.txt");

if (!infile)

{

cout << "Error opening file!" << endl;

exit(1);

}

//set the name of the classGrades object intro to CSC126
intro.setName("CSC126");


//set the name of the object intermediate to CSC211
intermediate.setName("CSC211");


intermediate.setNumStudents(4);

//input the grades for intermediate from a file
intermediate.inputGrades(infile);


//print the intermediate grades

intermediate.printlist();

//print the average of the intermediate grades


cout<<"average: "<<intermediate.returnAvg()<<endl;

// Part II - Syntax for arrays of objects

classGrades allCS[15]; //the default constructor is applied to //all elements of this array

//to set the number of students in each class to 25

int i = 0;
for (i = 0; i < 15; i++)

{

allCS[i].setNumStudents(25);

}

//Assume values in all classes. Write the code to print all class averages?

for (i = 0; i < 15; i++)
{

//note: averages are 0 because there are no grade values assigned to objects in allCS array
cout << "average of class number " << i + 1 << " is " << allCS[i].returnAvg() << endl;


}

  

system("pause");
return 0;
}

void classGrades::printlist() const

{

int i;

cout << "***" << name << "***" << numStudents << endl;

for (i = 0; i < numStudents; i++)

cout << i << ' ' << gradeList[i] << endl;

}

void classGrades::inputGrades(ifstream& in)

{

int i;

for (i = 0; i < numStudents; i++)

in >> gradeList[i];

}

double classGrades::returnAvg() const

{

int i;

double average;

double sum = 0;

//write code to return the average


for (i = 0; i < this->numStudents; i++)
{
sum = sum + this->gradeList[i];
}

average = sum / this->numStudents;

return average;
}

void classGrades::setName(string n)

{

name = n;

}

void classGrades::setNumStudents(int x)

{

numStudents = x;

}

classGrades::classGrades()

{

int i;

name = "";

numStudents = 30;

for (i = 0; i < 30; i++)

gradeList[i] = 0;

}

classGrades::classGrades(int n)

{

int i;

name = "";

numStudents = n;

for (i = 0; i < n; i++)

gradeList[i] = 0;

}
---------------------------

Add a comment
Know the answer?
Add Answer to:
hello there. can you please help me to complete this code. thank you. Lab #3 -...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • Can anyone help me with this one? I really appreciate it! Find and fix errors and...

    Can anyone help me with this one? I really appreciate it! Find and fix errors and Simplify the createGroups() function using only one loop instead of two #include using namespace std; const double NO_STU = 40; void createGroups(const string myClass[], int no_stu, string group1, string group2); void printList(const string [], int); int main() { int no_stu = 8; string myClass[NO_STU] = { "Linda", "Bob", "Mary", "Jo", "Tim", "Jamie", "Ann", "Tom" }; string group1[NO_STU % 2]; string group2[NO_STU % 2]; createGroups(no_stu,...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • Please provide a multi lined pseudo code for the program below.....also it is giving me a...

    Please provide a multi lined pseudo code for the program below.....also it is giving me a build error? Could you possibly tell me how to fix the error too? #include <iostream> #include <string> using namespace std; class ship { public: string name; int year; ship(string n, int y) { name = n; year = y; } int getYear() { return year; } string getName() { return name; } void setYear(int y) { year = y; } void setName(string n) {...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

  • C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will...

    C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will work with 2 classes to be used in a RPG videogame. The first class is the class Character. The Character class has two string type properties: name and race. The Character class also has the following methods: a constructor Character(string Name, string Race), that will set the values for the name and the race variables set/get functions for the two attributes a function print(),...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT