Question
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
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...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started