I need help with a C++ program where you Specify, design, and implement aclass c
ID: 3541168 • Letter: I
Question
I need help with a C++ program where you
Specify, design, and implement aclass called statistician. After a statistician isinitialized, it can be given a sequence of double numbers. Each number in sequence is given to the statistician by activatinga member function called next_number. For example, wecan declare a statistician called s, and then give it the sequenceof numbers 1.1, -2.4, 0.8 as shown here:
Implement "Statistician" at the end of Chapter 2, #2 in your text and #3 plus what is below. (Note: do NOT use an array to hold the sequence of input data. You need only the data members - length, sum, mean, smallest number, and largest number as stated in the text. You must be able to handle an empty Statistician.
You are to add the following to you Statistician program.
Two constructor methods that initialize your statistician objects: One is the default and one to initialize a statistician with the value of another statistician. The second constructor is called a copy constructor. Ex. "Statistician stat1;" declares an object as you normally do. The second one "Statistician stat2(stat1);" declares stat2 and initializes stat2 to the values (data member values) in stat1. So if you print stat2 and stat1, both would have the same values.
The copy constructor would look like
Statistician ( statistician s ) //where 's' is the argument 'stat1' above.
{ Several assignments of the data members}
An overloaded (<<) output method using the "friend" attribute.
The output should look something like the following where you print out of the data members with some labeling:
sum = xxx
len = xxx
....
An overloaded (+) as a function outside the class.
An overloaded (==) and (!=) as methods.
Add a 'erase' function member that resets a Statistician object.
Use the following input to test you program.
Data for statistician1: 5.5, 6.6, 8.8, -3.4, -0.5, 4.7, 9.1
Print out: the Sum, length and average only for statistica1 (you'll need getter functions for these)
add to statistician1: 5.2 - 3.3 - 8.5 3.2 5.5
Print out: statistician1 using the << operator.
Data for statistician2: initialize statistician2 with the value of statistician1 on declaration of statistician2.
Print out: statistician 1 and statistician2 using the << operator.
add to statistician2: 13 21 71 47 12 25 24 23 24 32
Print out: the Sum, length and average only for statistica2
Print out: statistician 1 & statistician2 using the << operator.
Add To add two statisticians together. The results should add the sums, the lengths and determine which of the two are the largest and the smallest of the new statisticians form the two inputs. ex statistician3 largest is the largest of statistican1 and statistician 2 for the statement below
statistician3 = statistician1 + statistica2;
Print out: statistician1 & statistician2 & statistician3 using the << operator.
Erase statistician2: Reset statistica2 to its initial declaration by calling the function member 'erase'.
Print out: statistician1 & statistician2 & statistician3 using the << operator.
Explanation / Answer
please rate - thanks
tested with DEV C++
partial output any questions ask--I wasn't sure of some things, what you needed
#include <iostream>
using namespace std;
class statistician
{
private:
int count;
double total;
double smallest;
double largest;
public:
statistician::statistician( )
{reset();
}
void statistician:: next(double r)
{count++;
total+=r;
if(count==1)
{smallest=r;
largest=r;
}
else
{if(r<smallest)
smallest=r;
if(r>largest)
largest=r;
}
}
void statistician::reset( )
{count=0;
total=0;
smallest=0;
largest=0;
}
int statistician::length( ) const
{return count;
}
double statistician::sum( ) const
{return total;
}
double statistician:: mean( ) const
{if(count>0)
return total/count;
else
return 0;
}
double statistician:: minimum( ) const
{if(count>0)
return smallest;
else
return 0;
}
double statistician:: maximum( ) const
{if(count>0)
return largest;
else
return 0;
}
statistician operator +( const statistician& s2)
{statistician temp;
temp.count= (int)(count+s2.count);
temp.total=total+s2.total;
if(smallest<s2.smallest)
temp.smallest=smallest;
else
temp.smallest=s2.smallest;
if(largest>s2.smallest)
temp.largest=largest;
else
temp.largest=s2.largest;
return temp;
}
friend ostream &operator <<(ostream &strm, const statistician &s)
{cout<<"sum: "<<s.sum()<<" length: "<<s.length()<<" largest: "<<s.maximum()<<
" smallest: "<<s.minimum()<<" average: "<<s.mean()<<endl;
}
};
bool operator ==(const statistician& s1, const statistician& s2)
{if(s1.length()>0)
if(s1.length()==s2.length())
if(s1.sum()==s2.sum())
if(s1.mean()==s2.mean())
if(s1.minimum()==s2.minimum())
if(s1.maximum()==s2.maximum())
return true;
return false;
}
bool operator !=(const statistician& s1, const statistician& s2)
{
return !(s1==s2);
}
int main()
{statistician s1;
s1.next(5.5);
s1.next(6.6);
s1.next(8.8);
s1.next(-3.4);
s1.next(-0.5);
s1.next(4.7);
s1.next(9.1);
cout<<"sum of s1: "<<s1.sum()<<endl;
cout<<"length of s1: "<<s1.length()<<endl;
cout<<"mean of s1: "<<s1.mean()<<endl;
s1.next(5.2);
s1.next(-3.3);
s1.next(-8.5);
s1.next(3.2);
s1.next(5.5);
cout<<"s1: "<<s1<<endl;
statistician s2;
s2.next(5.5);
s2.next(6.6);
s2.next(8.8);
s2.next(-3.4);
s2.next(-0.5);
s2.next(4.7);
s2.next(9.1);
cout<<"s1: "<<s1<<" s2: "<<s2<<endl;
s2.next(13);
s2.next(21);
s2.next(71);
s2.next(47);
s2.next(12);
s2.next(25);
s2.next(24);
s2.next(23);
s2.next(24);
s2.next(32);
cout<<"sum of s2: "<<s2.sum()<<endl;
cout<<"length of s2: "<<s2.length()<<endl;
cout<<"mean of s2: "<<s2.mean()<<endl;
cout<<s1<<" "<<s2<<endl;
statistician s3;
s3=s1+s2;
cout<<"s1: "<<s1<<" s2: "<<s2<<" s3: "<<s3<<endl;
s2.reset();
cout<<"s1: "<<s1<<" s2: "<<s2<<" s3: "<<s3<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.