Question

In: Computer Science

C++ please Create a Stats class whose member data includes an array capable of storing 30...

C++ please

Create a Stats class whose member data includes an array capable of storing 30 double data values, and whose member functions include total, average, lowest, and highest functions for returning information about the data to the client program. These are general versions of the same functions you created for Programming Challenge 7, but now they belong to the Stats class, not the application program. In addition to these functions, the Stats class should have a Boolean storeValue function that accepts a double value from the client program and stores it in the array. It is the job of this function to keep track of how many values are currently in the array, so it will know where to put the next value it receives and will know how many values there are to process when it is carrying out its other functions. It is also the job of this function to make sure that no more than 30 values are accepted. If the storeValue function is able to successfully store the value sent to it, it should return true to the client program. However, if the client program tries to store a thirty-first value, the function should not store the value and should return false to the client program. The client program should create and use a Stats object to carry out the same rainfall analysis requested by Programming Challenge 7. Notice that the Stats object does no I/O. All input and output is done by the client program.

Solutions

Expert Solution

#include <iostream>
#include <limits>
#include <random>

using namespace std;

class Stats
{

private:

int nextIndex; //this will maintain where to insert next element in array
double highestValue; //this will maintain the highest value in array
double lowestValue; //this will maintain the highest value in array
double statValues[30]; //this will maintain the values in an array form

public:

//This will be the constructor of this class
Stats ()
{

this->nextIndex = 0;
this->highestValue = numeric_limits < double >::min ();
this->lowestValue = numeric_limits < double >::max ();

}

double highest () //this function will return the highest value in the stats array
{

return highestValue;

}

double lowest () //this function will return the lowest value in the stats array

{

return lowestValue;

}

double total () //this function will return the total of values present in the stats array

{

double total = 0;
for (int i = 0; i < this->nextIndex; i++)
{

total = total + this->statValues[i];

}
return total;

}

double average () //this function will return the average of values present in the stats array
{

double total = 0;
for (int i = 0; i < this->nextIndex; i++)
{

total = total + this->statValues[i];

}
return (total / (this->nextIndex + 1));

}

bool storeValue (double value) //this function will insert a value in the stats array and return a boolean status that was this job successfull.
{

if (this->nextIndex > 29) return false;
if (value > highestValue) highestValue = value;
if (value < lowestValue) lowestValue = value;
this->statValues[this->nextIndex] = value;
this->nextIndex++;
return true;

}

};

/* The main function below is the client program*/
int main ()
{

Stats s;

uniform_real_distribution < double >unif (-10000, 10000);
default_random_engine re;
for (int i = 0; i < 31; i++)
{

if (s.storeValue (unif (re)))
   {

cout << "Yay!! a value stored in stats at index: " << i << endl;

}
else
   {

cout << "OOPS!! index: " << i << " does not exist in stats" << endl;

}

}

cout << "highest: " << s.highest () << endl;
cout << "lowest: " << s.lowest () << endl;
cout << "total: " << s.total () << endl;
cout << "average: " << s.average () << endl;
return 0;

}

OUTPUT:-


Related Solutions

In C++, create a 3-by-3 array that is capable of storing integers and initialize each entry...
In C++, create a 3-by-3 array that is capable of storing integers and initialize each entry to 0. Ask the user to supply a row and a column and a value, then put the value into the array at that row and column. Print the array to the screen as a table with 3 rows and 3 columns, and ask the user for another row, column, and value. Repeat until user inputs values for all entries. Once finished, compute and...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to store game states and at least two member functions for adding players’ moves and printing the game board. Write a driver to test your class. (2). The data type of the two-dimensional array can be either int or char. As a passlevel program, the size of the board can be hardcoded to 10 or a constant with a pre-set value, anything between 4 and...
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects....
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects. The Byte class will provide the following member functions: Byte - word: Bit[8] static Bit array defaults to all Bits false + BITS_PER_BYTE: Integer16 Size of a byte constant in Bits; 8 + Byte() default constructor + Byte(Byte) copy constructor + set(Integer): void sets Bit to true + clear(): void sets to 0 + load(Byte): void sets Byte to the passed Byte + read():...
C++ make a rational class that includes these members for rational -private member variables to hold...
C++ make a rational class that includes these members for rational -private member variables to hold the numerator and denominator values -a default constructor -an overloaded constructor that accepts 2 values for an initial fraction -member fractions add(), sub(), mul(), div(), less(), eq(), and neq() (less should not return true if the object is less than argument) -a member function that accepts an argument of type of ostream that writes the fraction to that open output stream do not let...
Create a C++ class with a static member item so that whenever a new object is...
Create a C++ class with a static member item so that whenever a new object is created, the total number of objects of the class can be reported.
In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
C++ Define a class PersonalRecord, which will be storing an employee’s information. Internally, the class should...
C++ Define a class PersonalRecord, which will be storing an employee’s information. Internally, the class should have the following attributes: SSN (string type), fullName (string type), homeAddress (string type), phoneNumber (string type), salaryRate (per year, float type), and vacationDays (accrued vacation, int type). The constructor should take six parameters: four of them are necessary to provide values to at the time of initialization (when a constructor will be called/invoked): SSN, fullName, homeAddress, and phoneNumber; and two of them are optional...
Write a C++ programs to: 1. Create a class called Student with four (4) private member...
Write a C++ programs to: 1. Create a class called Student with four (4) private member variables, name (string), quiz, midterm, final (all double type); 2. Include all necessary member functions for this class (at least 1 constructor, 1 get function, and 1 grading function – see #6); 3. Declare three (3) objects of Student type (individual or array); 4. Read from the keyboard three (3) sets of values of name, quiz, midterm, final and assign them to each object;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT