a program to create a histogram H of 0 ≤ N ≤ 16 entries. A histogram is a set of values
and the associated frequencies of each of those values. For example, a histogram of colors
might contain the colors “red”, “green”, “blue”, with frequencies of 6, 12 and 5, respectively.
H is represented as a global array of 16 of elements, each a struct or tuple containing a
string and an integer, and has the interface:
• A function init to set each element of H to an empty value and a frequency of 0.
• A function insert(s,n) to insert a new element, made up of a value s and a frequency n, into H. A new element is inserted into an empty position of H. If an element with the value s already exists in H, then no new element is inserted into H and instead the frequency of that (existing) element is increased by n.
• A function find(s) to find an element, given a value s, in H. If that element is found,
then the frequency of that element is returned. Otherwise, a value of -1 is returned.
• A function count to return the number of elements of H that are not empty
So, as you were instructed, a histogram is a count of how many items there are. The fruit bowl will do. You might find that the fruit bowl on the table contains:
3 apples
27 grapes
1 orange
Written as a histogram, you could list it as:
Apple 3
Grape 27
Orange 1
You can even have fruits in your histogram that are not in your bowl:
Apple 3
Banana 0
Grape 27
Orange 1
Pear 0
You might think this looks a whole lot like a basic inventory. (Because it is!)
Structure
In C++, a std::unordered_map <string, unsigned> is an excellent way to manage a histogram. However, you have not been given that option. You are asked to use a global array composed of either struct or tuple:
1 2 3 4 5 6 7 8 9 10 11
#include <string>
struct element
{
std::string name;
unsigned frequency;
};
constexprint MAX_ENTRIES = 16;
element histogram[ MAX_ENTRIES ];
1 2 3 4 5 6 7 8
#include <string>
#include <tuple>
using element = std::tuple <std::string, unsigned> ;
constexprint MAX_ENTRIES = 16;
element histogram[ MAX_ENTRIES ];
Either way is just as easy, but as a beginner I recommend the struct way.
Functions
Next you only need to implement the functions given you.
1 2 3 4
void init();
void insert( const std::string& name, unsigned n );
int find( const std::string& name );
int count();
As a freebie, here is a good way to implement count():
1 2 3 4 5 6 7 8
int count()
{
int n = 0;
for (int i = 0; i < MAX_ENTRIES; i++)
if (! histogram[i].name.empty())
n += 1;
return n;
}
Usage
Using the histogram is now very easy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
init();
insert( "Apple", 1 ); // I don't have to insert them all at once!
insert( "Banana", 0 ); // An element may appear in the histogram with zero occurrences in the data!
insert( "Grape", 27);
insert( "Orange", 1 );
insert( "Pear", 0 );
insert( "Apple", 2 );
int n = find( "Orange" );
if (n > 0) std::cout << "We have at least one orange!\n";
else std::cout << "Sorry, no oranges.\n";
}