Step 1: Preparation Comment out the following under-construction code: In dynamicaray.h: Al function prototypes except the constructors and destructor. Keep the member variables. In dynamicarray.cpp: All function implementations except the constructors and destructor. Also remove (not just comment out) INITIAL_CAP, and replace it with a hard-coded 10 inside the default constructor. This will also eventually go away. In main: Comment out all the code inside the RunPart1Tests function between the lines.  Also in main: Comment out all the code in the main function starting with the "Equality comparison" comment and just before the "return O;" line. bool pass = true; and return pass; Step 2: Replacing member data and the two constructors Replace the current member data (arr, len, and capacity) with a single vector of integers. At the top of your h, include the vector library. This is the only place you should need to include it. Also in your .h, remove your current member data, replace it with a vector of ints. Name it "arr" so you can keep as much of your current code as possible. Next, you need to change the constructor and destructor. For the copy constructor, copy the vector from the other object; do so by doing an element-by-element copy. Here's your new copy constructor in all its glory: DynamicArray::DynamicArray(const DynamicArray& other) : arr(other.arr) {} It should report the Lab 6 tests as "passed." Step 3: The one-liners plus output Tackle the four member functions (cap(), at), append() and operator=) that translate very well to functions that the vector class provides for you. You'll also tackle print, since you'll need it to check your results. What to Do: Uncomment the declarations and definitions from the .h and .cpp files Replace cap() function (defined in the .h) with a single-line function that uses a member function of the vector class. Do the same for the at() and append() function. Do not return -11111 when out of bound Repeat for the = operator. This will be two lines: one to actually copy the vector. and then the obligatory return * this; Replace these references with the appropriate ways to access them using your new vector. When you're ready to test your code, go to the RunPart1 Tests function in main.cpp. Put everything back in until the line labeled "Test sum." Two bad things are going to hapen: You're going to fail two capacity tests. You can comment out the sections that test the capacity. This code should crash, with an error message including: terminate called after throwing an instance of 'std::out_of_range: vector'

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Step 1: Preparation

Comment out the following under-construction code:

  • In dynamicaray.h: Al function prototypes except the constructors and destructor. Keep the member variables.
  • In dynamicarray.cpp: All function implementations except the constructors and destructor. Also remove (not just comment out) INITIAL_CAP, and replace it with a hard-coded 10 inside the default constructor. This will also eventually go away.
  • In main: Comment out all the code inside the RunPart1Tests function between the lines. 

    Also in main: Comment out all the code in the main function starting with the "Equality comparison" comment and just before the "return O;" line.

bool pass = true;

and

return pass;

Step 2: Replacing member data and the two constructors

Replace the current member data (arr, len, and capacity) with a single vector of integers.

  • At the top of your h, include the vector library. This is the only place you should need to include it.
  • Also in your .h, remove your current member data, replace it with a vector of ints. Name it "arr" so you can keep as much of your current code as possible.

Next, you need to change the constructor and destructor.

  • For the copy constructor, copy the vector from the other object; do so by doing an element-by-element copy.

    Here's your new copy constructor in all its glory:

DynamicArray::DynamicArray(const DynamicArray& other) : arr(other.arr) {}

It should report the Lab 6 tests as "passed."

Step 3: The one-liners plus output

Tackle the four member functions (cap(), at), append() and operator=) that translate very well to functions that the vector class provides for you. You'll also tackle print, since you'll need it to check your results.

What to Do:

  1. Uncomment the declarations and definitions from the .h and .cpp files
  2. Replace cap() function (defined in the .h) with a single-line function that uses a member function of the vector class.
  3. Do the same for the at() and append() function. Do not return -11111 when out of bound
  4. Repeat for the = operator. This will be two lines: one to actually copy the vector. and then the obligatory return * this;

Replace these references with the appropriate ways to access them using your new vector.

When you're ready to test your code, go to the RunPart1 Tests function in main.cpp. Put everything back in until the line labeled "Test sum."

Two bad things are going to hapen:

  1. You're going to fail two capacity tests. You can comment out the sections that test the capacity.
  2. This code should crash, with an error message including:

terminate called after throwing an instance of 'std::out_of_range: vector'

Step 4: The remaining functions
You should have three functions left. They don't map as nicely to vector's member functions as append(), cap(), and at(), so you will have to write more of your own code.
sum
Sum is the next easiest function to write, because the changes you make will be very similar to what you already did for print(). Here are the steps:
1. Un-comment it from dynamicarray.h
2. In dynamicarray.cpp, make similar modifications to the ones you made for print()
3. In main.cpp, put the "Test sum" section back into RunPart1 Tests().
Compile and run your code. You should now be passing all the current tests.
operator ==
The vector function doesn't have an equality operator, so your code still has to do the work of defining one. Again, the changes aren't too bad:
1. Uncomment it from your.h
2. Modify the implementation as necessary
3. In the main() function of main.cpp, put the Equality Comparison tests back in.
Recompile and rerun. Everything should pass.
remove
Last but not least, we have remove().
The good news is that tracking the length of the array and resizing the array as needed are no longer your problem, so that gets rid of a lot of your code.
Here's what you still have to do:
1. Uncomment it from your .h
2. Modify the implementation as follows
1. Find valToDelete in the vector, and return false if it's not there.
2. When you find it, you need to call vector's erase function and pass it an iterator for the position you want to delete. For us, that's
vector.begin() + i where i is the position of the element. Then return true. You do NOT need to worry about scooting the remaining elements to fill the gap -- vector will do that for you.
Testing times:
1. In function RunPart1 Tests() in main.cpp, put everything back except the capacity tests you've already commented out, and the single (long) line of code under a.remove(100 + i). At this point, you should be passing all the Part 1 tests.
2. In function main() in main.cpp, uncomment the rest of the code. All tests should pass, and you should be getting the desired output.
BTW, notice that we barely ended up changing main at all in the end! This is an example of using the same interface for two very different implementations of arrays.
That said, let's make one FINAL FINAL change to the main() to accommodate the output operator.
Step 5: Overloading the output (<<) operator
Define and implement the output operator, which mirrors the print() method. Recall, the signature of this method is:
friend ostream& operator<< (ostream &out, const DynamicArray& objToPrint);
To test, replace the following lines at the end of main():
stringstream testoutput;
a.print(testoutput);
b.print(testoutput);
cout << testoutput.str();
with
cout << a << b;
All tests should pass, and you should be getting the desired output.
Transcribed Image Text:Step 4: The remaining functions You should have three functions left. They don't map as nicely to vector's member functions as append(), cap(), and at(), so you will have to write more of your own code. sum Sum is the next easiest function to write, because the changes you make will be very similar to what you already did for print(). Here are the steps: 1. Un-comment it from dynamicarray.h 2. In dynamicarray.cpp, make similar modifications to the ones you made for print() 3. In main.cpp, put the "Test sum" section back into RunPart1 Tests(). Compile and run your code. You should now be passing all the current tests. operator == The vector function doesn't have an equality operator, so your code still has to do the work of defining one. Again, the changes aren't too bad: 1. Uncomment it from your.h 2. Modify the implementation as necessary 3. In the main() function of main.cpp, put the Equality Comparison tests back in. Recompile and rerun. Everything should pass. remove Last but not least, we have remove(). The good news is that tracking the length of the array and resizing the array as needed are no longer your problem, so that gets rid of a lot of your code. Here's what you still have to do: 1. Uncomment it from your .h 2. Modify the implementation as follows 1. Find valToDelete in the vector, and return false if it's not there. 2. When you find it, you need to call vector's erase function and pass it an iterator for the position you want to delete. For us, that's vector.begin() + i where i is the position of the element. Then return true. You do NOT need to worry about scooting the remaining elements to fill the gap -- vector will do that for you. Testing times: 1. In function RunPart1 Tests() in main.cpp, put everything back except the capacity tests you've already commented out, and the single (long) line of code under a.remove(100 + i). At this point, you should be passing all the Part 1 tests. 2. In function main() in main.cpp, uncomment the rest of the code. All tests should pass, and you should be getting the desired output. BTW, notice that we barely ended up changing main at all in the end! This is an example of using the same interface for two very different implementations of arrays. That said, let's make one FINAL FINAL change to the main() to accommodate the output operator. Step 5: Overloading the output (<<) operator Define and implement the output operator, which mirrors the print() method. Recall, the signature of this method is: friend ostream& operator<< (ostream &out, const DynamicArray& objToPrint); To test, replace the following lines at the end of main(): stringstream testoutput; a.print(testoutput); b.print(testoutput); cout << testoutput.str(); with cout << a << b; All tests should pass, and you should be getting the desired output.
Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Events
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education