Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ Please. This program will have you implement in histogram.cpp a class whose interface is given in histogram.h, and you will write a driver

In C++ Please. This program will have you implement in histogram.cpp a class whose interface is given in histogram.h, and you will write a driver program for it in main.cpp. The Histogram class stores the data associated with a frequency histogram, and this data is accessible via overloaded operators and other member functions.

The implementation. Your main task is to implement all the functions declared in the interface. You can tackle these tasks in the following stages:

1. The 0-argument constructor should simply initialize all the values of the counts array to zero. The 1-argument constructor should read in a stream of whitespace-separated integers between 0 and MAX, keeping track of the number of occurrences of each value. Recall that the counts array is not guaranteed to be initialized to zero, so you should initialize these values before you start reading values in from the file. You can check that your constructor is working properly by temporarily adding cout statements to the body of the constructor to report the counts after reading the file.

2. Implement the member functions size, min, max, mode, median, mean, and variance, testing each one before moving on to the next.

  • The size of the histogram is the sum of the counts across all values; each of the following functions may assume the size is positive.

  • The smallest possible value is 0 and the largest possible value is MAX, but the functions min and max should respectively return the smallest and largest values with positive count.

  • The mode is the value that occurred with highest frequency; if there is a tie, you may return any of the values with highest frequency.

1

The median is the 50th percentile value. For example, if there are 5 entries, then the third largest is also the third smallest, and that value should be returned. If there are 4 entries, then you may return either the second largest or the second smallest, which may or may not be the same value.

The mean is the sum of all the values (with repetition) divided by the total number of values, and the variance is the mean squared difference between the values and their mean.

For example, if there were one 1, four 2s, two 3s, and one 9, then the size would be 8, the min would be 1, the max would be 9, the mode and median would be 2, the mean would be 3.0, and the variance wouldbe(1(13.0)2 +4(23.0)2 +2(33.0)2 +1(93.0)2)/8=5.5.

3. Overload the += operator as a member function. The command hist1 += hist2; should not change hist2 (which is why the parameter is declared const) but it should add each of the counts in hist2 to the corresponding count in hist1 (which is why the function is not declared const).

4. Overload the [] operator as a member function. This command hist[val] should return the count of the value val.

5. Overload the << operator as a non-member function. This should insert MAX+1 lines into the specified ostream, with each line labeled with its value, a colon, and a string of stars representing the count of that value as done in your first programming assignment. You may but dont have to ensure perfect spacing to get the colons to line up.

Here's the file histogram.h:

#ifndef HISTOGRAM_H #define HISTOGRAM_H

#include #include

using namespace std;

class Histogram { public: static const size_t MAX = 100; // largest value that can be stored Histogram(string filename); // read in histogram from file Histogram(); // construct histogram with no entries size_t size() const; // total counts across all values size_t min() const; // smallest value with positive count size_t max() const; // largest value with positive count size_t mode() const; // (one of) the value(s) with highest count size_t median() const; // (the average of) the middle value(s) double mean() const; // arithmetic average of the values double variance() const; // mean squared difference between each value and the mean void operator +=(const Histogram& rhs); // add each rhs count to these counts size_t operator [](size_t val) const; // # times value occurs private: size_t counts[MAX+1]; // stores the frequency of each value 0...MAX };

// display histogram contents to ostream out as in HW1 ostream& operator <<(ostream& out, const Histogram& h);

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

ISBN: 1285427106, 978-1285427102

More Books

Students also viewed these Databases questions