Question: Define a class called counters in a file counters.h : class counters { private: static const int size = 10; // array size int data

Define a class called counters in a file counters.h:

class counters

{

private:

static const int size = 10; // array size

int data [size]; // array of values

int *last_inc; // pointer to last value incremented

int *last; // pointer to last value in array

public:

counters (); // constructor places zeros

void print (); // prints current array

void rand_inc (); // increments a random spot in array

bool filled (); // returns true if all elements

// are non-zero

};

The class maintains an array of integer counters, data. Values in the array will be incremented. last_inc is a pointer to the last element in the array incremented. last is a pointer to the last element in the array.

The member functions should only use pointers to access the array. You are not allowed to use array indexes. The member functions are:

counters: Constructor places zeros in all elements of the array. It also calls srand to initialize the random number seed, sets the value of last_inc to NULL and appropriately initializes last based on size.

print: Prints the values in the array. print first goes to a new line. It then prints each array value with a space before and after. It prints the element pointed to by last_inc with an * before and after.

rand_inc: Randomly chooses one of the elements in the array and increments it. last_inc is appropriately changed.

filled: Returns true if all of the values in the array are non-zero. Otherwise, it returns false.

These functions should all work properly if size is changed.

2. Write an application program, countapp.cpp, that declares a variable of type counters, lets call it c. This program prints c at the start and then repeatedly applies the functions rand_inc and print to c until filled returns true (each element is non-zero). It should count the number of random increments needed to fill the array.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!