Question: #include #include #include #include #include using namespace std; // Interface for SafeArray class //-------------------------------- class SafeArray { public: SafeArray(); SafeArray(const SafeArray & copy); ~SafeArray(); bool

#include  #include  #include  #include  #include  using namespace std; // Interface for SafeArray class //-------------------------------- class SafeArray { public: SafeArray(); SafeArray(const SafeArray & copy); ~SafeArray(); bool GetElement(const int i, float & Value) const; bool SetElement(const int i, float Value); bool ReadArray(const string Filename); bool WriteArray(const string Filename) const; void Print() const; void Randomize(); void Sort(); private: int Size; vector Array; }; // Implementation for SafeArray class //----------------------------------- SafeArray::SafeArray() { Size = 20; Array.resize(Size, 0); } SafeArray::SafeArray(const SafeArray & copy) { Size = copy.Size; Array = copy.Array; } SafeArray::~SafeArray() { } bool SafeArray::GetElement(const int i, float & Value) const { bool Success = false; if ((i >= 0) && (i < Size)) { Success = true; Value = Array[i]; } return Success; } bool SafeArray::SetElement(const int i, float Value) { bool Success = false; if ((i >= 0) && (i < Size)) { Success = true; Array[i] = Value; } return Success; } bool SafeArray::ReadArray(const string Filename) { ifstream infile; infile.open(Filename.c_str()); if (infile.fail()) return false; for (int i = 0; i < Size; i++) infile >> Array[i]; infile.close(); return true; } bool SafeArray::WriteArray(const string Filename) const { ofstream outfile; outfile.open(Filename.c_str()); if (outfile.fail()) return false; for (int i = 0; i < Size; i++) outfile << Array[i] << endl; outfile.close(); return true; } void SafeArray::Print() const { for (int i = 0; i < Size; i++) cout << "Array[" << i << "] = " << Array[i] << endl; } void SafeArray::Randomize() { for (int i = 0; i < Size; i++) Array[i] = (random() % 100) / 100.0; } void SafeArray::Sort() { // Simple but slow bubble sort for (int i = 0; i < Size; i++) for (int j = i + 1; j < Size; j++) if (Array[j] < Array[i]) { float temp = Array[i]; Array[i] = Array[j]; Array[j] = temp; } } // Program to demonstrate SafeArray class //--------------------------------------- int main() { SafeArray data1; // Randomize values data1.Randomize(); data1.Print(); // Sort values data1.Sort(); data1.Print(); return 0; } 

Step 1: Copy this program into your C++ program editor, and compile it. Hopefully you will not get any error messages.

Step 2: When you run your program it should print 20 random values between 0.0 and 1.0. Then it will print 20 sorted values between 0.0 and 1.0. Take a look at the main program to see how we did this. You will see that we created a SafeArray object and then called "Randomize" and "Print" followed by "Sort" and "Print". All the real work was done in these methods.

Step 3: Our previous version of the SafeArray class used a fixed size array of floats to store user data. To make our class more flexible we are now using the following:

private: int Size; vector Array; 

The private variable "Array" is now a vector of float values. When you read the online documentation for the vector class, you will see that a vector can be used like an array, but there are many methods that allow you to change the size of the vector at run time. We will use the private variable "Size" to store the current size of the "Array" vector.

Step 4: Edit your program and look at the constructor function. You will see that we initialize the private variables using the following:

SafeArray::SafeArray() { Size = 20; Array.resize(Size, 0); } 

The "resize" method in the vector class will allocate space for "Size" float values at run time. Each value will be initialized to 0. It is silly to use the hard coded constant "20" here. We should really use a parameter instead. Replace the constructor function with the following:

SafeArray::SafeArray(int size) { Size = size; Array.resize(Size, 0); } 

Step 5: Recompile your program. You will see an error message saying that "SafeArray::SafeArray(int)" does not have a matching prototype. This is because we forgot to add the "int size" parameter to the constructor function in the class interface. Make this change and recompile again. This time you will get an error saying that there is no matching function for "SafeArray::SafeArray()". To fix this error, edit your main program and replace your data1 declaration with the following:

 int size; cout << "Enter size: "; cin >> size; SafeArray data1(size); 

Step 6: Recompile your program. Now test it by entering a size of "20". You should see some familiar random values. Run your program again and try some larger and smaller sizes. Now that your SafeArray class is more flexible, you do not need to edit and recompile to perform these experiments.

Step 7: Since you are now able to create multiple SafeArray objects with different sizes in a program, it is very useful to have a SafeArray method that returns the current size of an object. Edit your class interface to add a public method "int GetSize();" and then add the implementation of this method. To test your new method, add the following line at the bottom of your main program just before the "return 0;" line:

 cout << "size = " << data1.GetSize() << endl; 

Step 8: Once you think your program is working correctly, upload your final program into the auto grader by following the instructions below.

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!