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

#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; private: static const int SIZE = 20; float Array[SIZE]; }; // Implementation for SafeArray class //----------------------------------- SafeArray::SafeArray() { for (int i = 0; i < SIZE; i++) Array[i] = 0; } SafeArray::SafeArray(const SafeArray & copy) { for (int i = 0; i < SIZE; i++) Array[i] = copy.Array[i]; } 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; } // Program to demonstrate SafeArray class //--------------------------------------- int main() { SafeArray data1; // Initialize with random values for (int i = 0; i < 20; i++) { float value = (random() % 100) / 100.0; data1.SetElement(i, value); } data1.Print(); // Find minimum value float min; data1.GetElement(0, min); for (int i = 1; i < 20; i++) { float value; data1.GetElement(i, value); if (value < min) min = value; } cout << "min value = " << min << endl; 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 and the minimum value. Take a look at the main program to see how we did this. There is one loop that uses "random" and "SetElement" to store 20 random values into the SafeArray, and another loop that uses "GetElement" to search for the minimum.

Step 3: Notice that we are using the hard coded value of "20" to control the two loops in main. This is a bad design decision because the program will have to be edited in several places every time the array size changes. We really should use a constant instead. Edit your class interface and move the "static const int SIZE = 20;" line from the private section into the public part. This will let us use "SafeArray::SIZE" in the main program instead of "20". Make these changes and compile and run your program to verify it still works properly.

Step 4: We may find ourselves randomizing the data in the SafeArray several times in our main program. To avoid duplicate code, we can add a "Randomize" method to the SafeArray class. This is a three step process. First, you need to add "void Randomize();" to the public interface of the class. Then, you need to create an empty Randomize method. Finally, you need to copy/paste the for loop from the main program into your Randomize method. Remember, when you are calling SafeArray methods from within your Randomize method, you do not need the "data1." in front of the method call.

Step 5: To test your Randomize method, remove the first loop from the main program, and replace it with "data1.Randomize();". Recompile and run your program. You should see the same output as before. Now add two lines of code to Randomize and Print data1 a second time. When you recompile and run you should see 40 values printed and a different minimum value.

Step 6: Finding the minimum value in the SafeArray is another operation that probably belongs in the SafeArray class instead of the main program. To do this, you need to follow the same three step process used above. First, add a line to the public interface of the class to declare the method "GetMinimum". You need to decide if this method is going to return the minimum value or store it in a reference parameter. Then, create an empty GetMinimum method. Finally, use cut/paste to remove the second loop from the main program and put it into your GetMinimum method.

Step 7: To test your modified SafeArray class, add a call to GetMinimum in the main program just before the cout line, and recompile and run. You should get the same results as before but your main program is much smaller and easier to read.

Step 8: To make the SafeArray class more symmetrical, we should add a "GetMaximum" method to the class. Use the three step process above and copy/paste GetMinimum code as needed to add this method to your SafeArray class. Once you have this done, add call to GetMaximum in your main program after GetMinimum and print out the maximum value.

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!