Question

In: Computer Science

Please post code in format that can be copied. If possible please explain code but not...

Please post code in format that can be copied.

If possible please explain code but not necessary.

Directions:

1. Create a new project for the lab and copy stat.h, stat.cpp, and teststat.cpp into your workspace.

2. Now review the header file. Make sure you understand the design. Can you explain the difference between what the class represents versus how it plans to accomplish it?

3. Now edit the stat.cpp file. Notice that many of the member functions are missing. For all of the missing functions you will need to copy the function prototypes from the header file into the .cpp file. Remember to add Statistician:: before the name of each member function. (Not the friend functions or helper functions.)

4. Add the logic for 3 of the following methods (you may choose): • reset • mean • minimum • maximum • operator+ • operator==

5. You should now test all the functionality of the class (in the client/application code). Run the test code and determine if there are any errors in the code.

Stats.cpp

#include <iostream> // Provides istream classes
#include "stats.h"

using namespace std;

Statistician::Statistician( )
{
reset( );
}

void Statistician::next(double r)
{
total += r;
count++;
if ((count == 1) || (r < tinyest))
tinyest = r;
if ((count == 1) || (r > largest))
largest = r;
}


// implement reset

// implement mean

// implement minimum

// implement maximum

// implement operator +

// implement operator ==

istream& operator >>(istream& ins, Statistician& target)
{
double user_input;

while (ins && (ins.peek( ) != ';'))
{
   if (ins.peek( ) == ' ')
   ins.ignore( );
   else
   {
   ins >> user_input;
   target.next(user_input);
   }
}
ins.ignore( );

return ins;
}

ostream& operator <<(ostream& outs, Statistician& target)
{
outs << "Values for statistician object: " << endl;
outs << "\ttotal: " << target.total << endl;
outs << "\tcount: " << target.count << endl;
outs << "\tsmallest: " << target.tinyest << endl;
outs << "\tlargest: " << target.largest << endl << endl;

return outs;

}

stats.h

#ifndef STATS_H // Prevent duplicate definition
#define STATS_H
#include <iostream>

using namespace std;

class Statistician
{
public:
// CONSTRUCTOR
Statistician( );
// MODIFICATION MEMBER FUNCTIONS
void next(double r);
void reset( );
// CONSTANT MEMBER FUNCTIONS
int length( ) const { return count; }
double sum( ) const { return total; }
double mean( ) const;
double minimum( ) const;
double maximum( ) const;
// FRIEND FUNCTIONS
friend Statistician operator +
(const Statistician& s1, const Statistician& s2);
friend istream& operator >>
           (istream&, Statistician& s1);
       friend ostream& operator <<
       (ostream&, Statistician& s1);
private:
int count; // How many numbers in the sequence
double total; // The sum of all the numbers in the sequence
double tinyest; // The smallest number in the sequence
double largest; // The largest number in the sequence
};

// NON-MEMBER functions for the Statistician class
bool operator ==(const Statistician& s1, const Statistician& s2);

#endif

teststat.cpp


#include <iostream>
#include "stats.h"

using namespace std;

void menu (void)
{ cout<<endl<<endl;
cout<<"1. Add a number"<<endl;
cout<<"2. Reset the statistician"<<endl;
cout<<"3. Length"<<endl;
cout<<"4. Sum"<<endl;
cout<<"5. Mean"<<endl;
cout<<"6. Minimum"<<endl;
cout<<"7. Maximum"<<endl;
cout<<"8. Add"<<endl;
cout<<"9. Equality"<<endl;
cout<<"10. Print" << endl;
cout<<"11. Quit"<<endl<<endl;
cout<<"Enter selection>";
}

void add(Statistician& S)
{Statistician s2, s3;
s2.reset();
s3.reset();

//enter values for second stat
cout<<"Enter values for second stat, enter ; to stop"<<endl;
cin>>s2;

s3 = S + s2;
cout<<"Values for added statistician are:"<<endl;
cout<<"Length: "<<s3.length( )<<endl;
cout<<"Sum: "<<s3.sum( )<<endl;
}


void equal(const Statistician &S)
{ Statistician s2;
s2.reset();

//enter values for second stat
cout<<"Enter values for second stat, enter ; to stop"<<endl;
cin>>s2;

   if (s2==S) cout<<"equal"<<endl;
       else cout<<"unequal"<<endl;
}


//Beginning of main program
int main (void)
{
   int choice;
   double num;

   //declare a Statistician
   Statistician s;
   s.reset();

   do {
   //print menu
   menu();
   cin>>choice;

   switch (choice) {
       case 1 : cout<<endl<<"Enter a number> ";
               cin>>num;
               s.next(num);
               cout<<endl;
               break;

       case 2 : s.reset();
               cout<<endl<<"Statistician reset"<<endl;
               break;

       case 3: cout<<endl<<"Length is "<<s.length()<<endl;
               break;

       case 4: cout<<endl<<"Sum is "<<s.sum()<<endl;
               break;

       case 5: if (s.length() > 0)
                   cout<<endl<<"Mean is "<<s.mean()<<endl;
               else
                   cout<<"Must add a number first."<<endl;
               break;

       case 6: if (s.length() >0)
                   cout<<endl<<"Minimum is "<<s.minimum()<<endl;
               else
                   cout<<"Must add a number first."<<endl;
               break;

       case 7: if (s.length() >0)
                   cout<<endl<<"Maximum is "<<s.maximum()<<endl;
               else
                   cout<<"Must add a number first."<<endl;
               break;

       case 8: add(s);
               break;

       case 9: equal(s);
               break;

       case 10: cout << s;
               break;

       case 11: cout<<endl<<"Thanks and goodbye."<<endl;
               break;

       default : cout<<endl<<"Invalid command. Try again."<<endl;
   }

   } while (choice != 11);
}

Solutions

Expert Solution

Answer 2:

  • class want to calculate basic statistcal values such as minimum,maximum,mean.
  • For this it want to make header file.So,we can easily use it.
  • As we have stats.h file for prototype declaration and stats.cpp for their definition.
  • And we also have teststat.cpp file for testing purpose.

Answer 4:

Stats.cpp

#include <iostream> // Provides istream classes
#include<string>
#include "stats.h"

using namespace std;

Statistician::Statistician( )
{
reset( );
}
void Statistician::reset( )
{
this->count=0;
this->total=0;
this->largest=0;
this->tinyest=0;
}
double Statistician::mean( ) const
{
return total/count;
}
double Statistician::minimum( ) const
{
return tinyest;
}
double Statistician::maximum( ) const
{
return largest;
}
void Statistician::next(double r)
{
total += r;
count++;
if ((count == 1) || (r < tinyest))
tinyest = r;
if ((count == 1) || (r > largest))
largest = r;
}

Statistician operator +(const Statistician& s1, const Statistician& s2)
{
Statistician s3;
s3.count=s1.count+s2.count;
s3.total=s1.total+s2.total;
s3.tinyest=(s1.tinyest<s2.tinyest)?s1.tinyest:s2.tinyest;
s3.largest=(s1.largest<s2.largest)?s1.largest:s2.largest;
return s3;
}
bool operator ==(const Statistician& s1, const Statistician& s2)
{
if(s1.length()==s2.length()&&s1.sum()==s2.sum()&&s1.minimum()==s2.minimum()&&s1.maximum()==s2.maximum())
return true;
else
return false;

}
// implement reset

// implement mean

// implement minimum

// implement maximum

// implement operator +

// implement operator ==

istream& operator >>(istream& ins, Statistician& target)
{
string user_input;

while (ins && (ins.peek( ) != ';'))
{
if (ins.peek( ) == ' ')
ins.ignore( );
else
{
ins >> user_input;
if(user_input==";")
break;
target.next(stof(user_input.c_str()));
}
}
ins.ignore( );

return ins;
}

ostream& operator <<(ostream& outs, Statistician& target)
{
outs << "Values for statistician object: " << endl;
outs << "\ttotal: " << target.total << endl;
outs << "\tcount: " << target.count << endl;
outs << "\tsmallest: " << target.tinyest << endl;
outs << "\tlargest: " << target.largest << endl << endl;

return outs;

}

Stats.h

#ifndef STATS_H // Prevent duplicate definition
#define STATS_H
#include <iostream>

using namespace std;

class Statistician
{
public:
// CONSTRUCTOR
Statistician( );
// MODIFICATION MEMBER FUNCTIONS
void next(double r);
void reset( );
// CONSTANT MEMBER FUNCTIONS
int length( ) const { return count; }
double sum( ) const { return total; }
double mean( ) const;
double minimum( ) const;
double maximum( ) const;
// FRIEND FUNCTIONS
friend Statistician operator +
(const Statistician& s1, const Statistician& s2);
friend istream& operator >>
(istream&, Statistician& s1);
friend ostream& operator <<
(ostream&, Statistician& s1);
private:
int count; // How many numbers in the sequence
double total; // The sum of all the numbers in the sequence
double tinyest; // The smallest number in the sequence
double largest; // The largest number in the sequence
};

// NON-MEMBER functions for the Statistician class
bool operator ==(const Statistician& s1, const Statistician& s2);

#endif

testStat.cpp

#include <iostream>
#include "stats.h"

using namespace std;

void menu (void)
{ cout<<endl<<endl;
cout<<"1. Add a number"<<endl;
cout<<"2. Reset the statistician"<<endl;
cout<<"3. Length"<<endl;
cout<<"4. Sum"<<endl;
cout<<"5. Mean"<<endl;
cout<<"6. Minimum"<<endl;
cout<<"7. Maximum"<<endl;
cout<<"8. Add"<<endl;
cout<<"9. Equality"<<endl;
cout<<"10. Print" << endl;
cout<<"11. Quit"<<endl<<endl;
cout<<"Enter selection>";
}

void add(Statistician& S)
{Statistician s2, s3;
s2.reset();
s3.reset();

//enter values for second stat
cout<<"Enter values for second stat, enter ; to stop"<<endl;
cin>>s2;

s3 = S + s2;
cout<<"Values for added statistician are:"<<endl;
cout<<"Length: "<<s3.length( )<<endl;
cout<<"Sum: "<<s3.sum( )<<endl;
}


void equal(const Statistician &S)
{ Statistician s2;
s2.reset();

//enter values for second stat
cout<<"Enter values for second stat, enter ; to stop"<<endl;
cin>>s2;

if (s2==S) cout<<"equal"<<endl;
else cout<<"unequal"<<endl;
}


//Beginning of main program
int main (void)
{
int choice;
double num;

//declare a Statistician
Statistician s;
s.reset();

do {
//print menu
menu();
cin>>choice;

switch (choice) {
case 1 : cout<<endl<<"Enter a number> ";
cin>>num;
s.next(num);
cout<<endl;
break;

case 2 : s.reset();
cout<<endl<<"Statistician reset"<<endl;
break;

case 3: cout<<endl<<"Length is "<<s.length()<<endl;
break;

case 4: cout<<endl<<"Sum is "<<s.sum()<<endl;
break;

case 5: if (s.length() > 0)
cout<<endl<<"Mean is "<<s.mean()<<endl;
else
cout<<"Must add a number first."<<endl;
break;

case 6: if (s.length() >0)
cout<<endl<<"Minimum is "<<s.minimum()<<endl;
else
cout<<"Must add a number first."<<endl;
break;

case 7: if (s.length() >0)
cout<<endl<<"Maximum is "<<s.maximum()<<endl;
else
cout<<"Must add a number first."<<endl;
break;

case 8: add(s);
break;

case 9: equal(s);
break;

case 10: cout << s;
break;

case 11: cout<<endl<<"Thanks and goodbye."<<endl;
break;

default : cout<<endl<<"Invalid command. Try again."<<endl;
}

} while (choice != 11);
}

Answer 5:

  • As there is an error in testatat.cpp file in add function.because it want to take input in double variable of character input.
  • So, i correct it as taking input as string .
  • then compare it with ";".
    • if not ";"
      • covert into double using function
    • otherwise
      • break out of loop

Related Solutions

Can someone please explain step-by-step if possible? Miller Company’s contribution format income statement for the most...
Can someone please explain step-by-step if possible? Miller Company’s contribution format income statement for the most recent month is shown below: Total Per Unit Sales (37,000 units) $ 222,000 $ 6.00 Variable expenses 111,000 3.00 Contribution margin 111,000 $ 3.00 Fixed expenses 44,000 Net operating income $ 67,000 Required: (Consider each case independently): 1. What is the revised net operating income if unit sales increase by 20%? 2. What is the revised net operating income if the selling price decreases...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1. List the following functions according to their order of growth from the lowest to the highest: (n−2)!, 5lg(n+100)10, 22n, 0.001n4 +3n3 +1, ln2 n, √3 n, 3n. 2. The range of afinite nonempty set of...
Please explain the answer as much as possible. If picture can be drawn to explain please...
Please explain the answer as much as possible. If picture can be drawn to explain please draw the picture as well. 1. Demonstrate Containerization knowledge and experience 2. Demonstrate usage of DockerHub 3. Demonstrate an understanding of the virtual hosts configuration in Apache2 4. Demonstrate the implementation of the https protocol in Apache2
Can anyone make a documentation and comments for the following code? Documentation in doxgen format please....
Can anyone make a documentation and comments for the following code? Documentation in doxgen format please. Thank you! And simple comments for the code. Thank s. template <typename T> bool LinkedList<T> :: search(const T& item) const { // if list is not empty display the items Node<T>* current = first; while (current != NULL) { if (current->info == item) { return true; } current = current->link; } return false; } template <typename T> void LinkedList<T> :: deleteNode(const T& item) {...
Please explain in excel format if possible: A manufacturer of air filter systems for industrial facilities,...
Please explain in excel format if possible: A manufacturer of air filter systems for industrial facilities, is considering the addition of a new system to its current product line. The following data has been forecasted: Years 2012 2013 2014 2015 Depreciation 30,000 35000 40000 45000 EBIT 100,000 125,000 150,000 175,000 Investment in Operating Assets 30,000 40,000 50,000 60,000 The market value of the firm's debt is $500,000 and it has $150,000 in marketable securities. The company also has 10,000 of...
PLEASE NO HANDWRITTEN OR COPIED ANSWERS Explain the model for terrestrial planet formation.
PLEASE NO HANDWRITTEN OR COPIED ANSWERS Explain the model for terrestrial planet formation.
Please complete in Python and neatly explain and format code. Use snake case style when defining...
Please complete in Python and neatly explain and format code. Use snake case style when defining variables. Write a program named wordhistogram.py which takes one file as an argument. The file is an plain text file(make your own) which shall be analyzed by the program. Upon completing the analysis, the program shall output a report detailing the shortest word(s), the longest word(s), the most frequently used word(s), and a histogram of all the words used in the input file. If...
Please complete in Python and neatly explain and format code. Use snake case style when defining...
Please complete in Python and neatly explain and format code. Use snake case style when defining variables. Write a program named wordhistogram.py which takes one file as an argument. The file is an plain text file(make your own) which shall be analyzed by the program. Upon completing the analysis, the program shall output a report detailing the shortest word(s), the longest word(s), the most frequently used word(s), and a histogram of all the words used in the input file. If...
(Please answer this question in an excel format if possible.) Please provide very detailed explanation and...
(Please answer this question in an excel format if possible.) Please provide very detailed explanation and answers if possible. The Poster Bed Company believes that its industry can best be classified as monopolistically competitive. An analysis of the demand for its canopy bed has resulted in the following estimated demand function for the bed: P ¼ 1760 12Q The cost analysis department has estimated the total cost function for the poster bed as TC ¼ 1 3 Q3 15Q2 þ...
Please explain in manual written format and EXCEL function. Mercury is a heavy metal that can...
Please explain in manual written format and EXCEL function. Mercury is a heavy metal that can cause severe health problems in even small concentrations. Fish and shellfish efficiently concentrate mercury into their flesh, so it is important to monitor seafood for its mercury content. An extensive study conducted in 1980 concluded that the mean mercury level in oysters from the White Bear Estuary was .020 parts per million (ppm) with a population standard deviation , σ, of .022 ppm. In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT