Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following C++ program. At the top you can see the interface for the SafeArray class that lists the public methods and the private

Consider the following C++ program. At the top you can see the interface for the SafeArray class that lists the public methods and the private attributes. Next, we have the implementation of all of the class methods. Finally, we have an empty main program.

#include #include #include #include using namespace std; // Interface for SafeArray class //-------------------------------- const int SIZE = 20; 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: float Array[SIZE]; }; // Implementation for SafeArray class //----------------------------------- SafeArray::SafeArray() { cout << "construct\n"; for (int i = 0; i < SIZE; i++) Array[i] = 42; } SafeArray::SafeArray(const SafeArray & copy) { cout << "copy\n"; for (int i = 0; i < SIZE; i++) Array[i] = copy.Array[i]; } SafeArray::~SafeArray() { cout << "destruct\n"; } 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() { return 0; }

Step 1: Copy this program into your C++ program editor, and compile it. Hopefully you will not get any error messages. When you run your program it should not do anything because main is empty.

Step 2: Edit your code and declare two SafeArray objects "data1" and "data2" in the main program. When you compile and run the program, you should see several messages printed out. Go back to the code and look at the implementation of the class constructors and destructor, and you will see the cout statements that produced the output. These messages are often useful for debugging because they let us see when methods are called.

Step 3: Now edit your main program to call the "Print" method on your "data1" object. When you compile and run, you should see the contents of the class variable "Array" printed out. This a silly value to initialize any variable with, so edit the program and set the initial value to zero instead.

Step 4: Because the class variable "Array" is declared to be private, we know it can not be accessed in the main program using anything like "data1.Array[0]". Instead, we have to use the "GetElement" and "SetElement" methods. Copy and paste the code below into your main program just before your "data.Print()" line.

for (int i = -5; i < 25; i++) { data1.SetElement(i, i * 0.1); cout << "set " << i << " " << i * 0.1 << endl; }

Step 5: When you compile and run your program, you should see a long list of "set" messages, followed by the current contents of "Array". Notice that locations 0 to 19 now contain values between 0 and 1.9. Our for loop is clearly trying to set values for locations -5 to 24 but the program is not crashing thanks to the range checking in our "SetElement" method.

Step 6: If you look at the implementation of the "SetElement" method you will see that it is returning "true" if the index is within range, and "false" if it is out of the array range. Modify the for loop above, so you only print the "set" message when the method call is successful. Now when you compile and run your program, you should only see 20 "set" messages printed.

Step 7: Using your improved "SetElement" loop as an example, write a second for loop after your "data1.Print" line that goes from -5 to 24 and calls "GetElement" to access the values in your "data1" object. For each successful method call, print out "get", the value of i, and value the method gives you back from data1. When you compile and run your program, you will be testing the array bounds checking in "GetElement". Hopefully it works properly and 20 "get" messages are printed.

Step 8: Now we need to test the final two methods "ReadArray" and "WriteArray". To start, copy and paste the following 20 values into an ascii file called "data2.in".

3.1 4.1 5.9 2.6 5.3 5.8 9.7 9.3 2.3 8.4 6.2 6.4 3.3 8.3 2.7 9.5 0.2 8.8 4.1 9.7

Step 9: Your final task is to figure out how to use the "ReadArray" method to read the "data2.in" file and store these values in the "data2" object. To verify they have been read correctly, call the "Print" method. Then call the "WriteArray" method to store these 20 values in file called "data2.out". Add this code at the very bottom of your program just before the "return 0" line. When you compile and run your program, the last 20 lines on the screen should show the "data2.in" values above, and you should have the same values in "data2.out". Congratulations, you have now tested all of the methods in the SafeArray class.

Step 10: 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

Step: 1

Answer Heres the modified C program based on the provided steps include iostream include fstream inc... 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

Computer organization and architecture designing for performance

Authors: william stallings

8th edition

136073735, 978-0136073734

More Books

Students also viewed these Programming questions

Question

Explain Moore's law.

Answered: 1 week ago