C++ Code Dynamic Arrays

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

C++ Code Dynamic Arrays

Step 1: Read your files!
You should now have 3 files. Read each of these files, in the order listed below.
dynamicarray.h contains the class definition for DynamicArray. Be sure you fully understand the changes to the class definition:
arr is defined differently
●
O
--
o A new variable, capacity, has been added
o A new member function, cap(), has been added to allow external access to the capacity. This is to make sure you're resizing correctly.
dynamicarray.cpp -- contains the definition of the print() function for DynamicArray.
main.cpp client code to test your dynamicarray class.
o Note that the multi-line comment format (/* ... */) has been used to comment out everything in main below the first print statement. As you proceed through the lab, you will need to move
the starting comment (/*) to a different location. Other than that, no changes ever need to be made to this file.
Transcribed Image Text:Step 1: Read your files! You should now have 3 files. Read each of these files, in the order listed below. dynamicarray.h contains the class definition for DynamicArray. Be sure you fully understand the changes to the class definition: arr is defined differently ● O -- o A new variable, capacity, has been added o A new member function, cap(), has been added to allow external access to the capacity. This is to make sure you're resizing correctly. dynamicarray.cpp -- contains the definition of the print() function for DynamicArray. main.cpp client code to test your dynamicarray class. o Note that the multi-line comment format (/* ... */) has been used to comment out everything in main below the first print statement. As you proceed through the lab, you will need to move the starting comment (/*) to a different location. Other than that, no changes ever need to be made to this file.
Step 2a: Default constructor
The first step is to write the default constructor in dynamicarray.cpp. This is going to be different from before, because you have to allocate the array dynamically.
Your job is to set the 3 member variables (make sure you know what these are) appropriately. This means:
• Setting the capacity of the array equal to the INITIAL_CAP constant defined in the .cpp file.
Allocating this many integers for the array
• Initializing len, the actual number of elements, appropriately for an empty array.
At this point, you should compile and run the code. The output statements in main will tell you what to expect.
Step 2b: Destructor
●
Un-comment the destructor's prototype in your .hpp file, and then define it in your .cpp file.
This function should deallocate any memory that was dynamically allocated for any of your member variables.
Step 3: Append
The next step is to define an append() member function. Un-comment the prototype in your .h file, and write the definition in your .cpp file. It's an extension of the version you already wrote for StaticArray:
1. If the array is already at its maximum capacity, then allocate a new array with twice the capacity, copy over the old elements, delete the old array, and set arr to point to the new array.
2. Append the new value to the array and update the length
Not required / "Expert" mode: If you feel like you're starting to write redundant code in different functions, feel free to refactor it! Any new functions you define in your refactoring should be private.
In main.cpp, put the code that tests append (up to the declaration of indices) back into your code. Compile and check the output to make sure everything is correct so far.
Step 4: At
The at() member function should work exactly like before. Un-comment it from the .h, and define it in the .cpp. Recall, if the index is outside the bounds of the array, it should return -11111 (an arbitrary default value).
Then add everything up to "Testing sum" back into main. Make sure the tests run correctly.
Step 5: Sum
This function should just return the sum of all the current array elements and will also work the same as before. Add it back into your code and re-test it, similar to what you just did for the at() function. In main, put everything up to "Testing remove" back in.
Step 6: Remove
This is going to work similarly to your old Remove(), with one addition.
The old part: You should pass this function the value you want to remove, and then delete the first array element equal to that value. In the example below, we have the values 200 through 209 in an array, and we pass 203 to remove:
(len is 10 before calling remove)
200 201 202 203 204 205 206 207 208 209
Here's what the array should look like after deletion. Note that, because the length is 9, we will consider the second 209 as arbitrary garbage; it's past the end of the array we're modeling.
200 201 202 204 205 206 207 208 209
(len is now 9)
This function should return true if the delete was successful, and false if the value didn't match any array element. For example, for this array, remove(214) should return false.
The new part: If the user has deleted a lot of elements, you should resize your array to make it smaller. Here's the formula:
If the array length has dropped below half of the capacity (i.e. < 0.5*capacity), you should resize it to 80% of the capacity, rounding down.
HOWEVER, the array should never go below the initial capacity (INITIAL_CAP), no matter how many elements are in it.
Resizing the array to make it smaller should be no different than resizing to make it bigger. Again, if you feel like an extra challenge, consider refactoring your code to eliminate redundancies.
Then add this function to your class, and run the original main, with all tests back in.
.
prob 209 but it doesn't
matter
Final deliverable
Ensure your code follows
Style guidelines related to variable Naming, Comments to explain the logic, Whitespace around most operators, Curly-Braces around control statements (if, while, for), and logic to explain their use.
Document functions by describing:
●
O the function's intended use,
O the function's parameters, using a @param tag for each function/method parameters
O the function's return value, using a @returns tag (if applicable),
O any pre-conditions using a @pre tag.
o any post-conditions using a @post tag.
Transcribed Image Text:Step 2a: Default constructor The first step is to write the default constructor in dynamicarray.cpp. This is going to be different from before, because you have to allocate the array dynamically. Your job is to set the 3 member variables (make sure you know what these are) appropriately. This means: • Setting the capacity of the array equal to the INITIAL_CAP constant defined in the .cpp file. Allocating this many integers for the array • Initializing len, the actual number of elements, appropriately for an empty array. At this point, you should compile and run the code. The output statements in main will tell you what to expect. Step 2b: Destructor ● Un-comment the destructor's prototype in your .hpp file, and then define it in your .cpp file. This function should deallocate any memory that was dynamically allocated for any of your member variables. Step 3: Append The next step is to define an append() member function. Un-comment the prototype in your .h file, and write the definition in your .cpp file. It's an extension of the version you already wrote for StaticArray: 1. If the array is already at its maximum capacity, then allocate a new array with twice the capacity, copy over the old elements, delete the old array, and set arr to point to the new array. 2. Append the new value to the array and update the length Not required / "Expert" mode: If you feel like you're starting to write redundant code in different functions, feel free to refactor it! Any new functions you define in your refactoring should be private. In main.cpp, put the code that tests append (up to the declaration of indices) back into your code. Compile and check the output to make sure everything is correct so far. Step 4: At The at() member function should work exactly like before. Un-comment it from the .h, and define it in the .cpp. Recall, if the index is outside the bounds of the array, it should return -11111 (an arbitrary default value). Then add everything up to "Testing sum" back into main. Make sure the tests run correctly. Step 5: Sum This function should just return the sum of all the current array elements and will also work the same as before. Add it back into your code and re-test it, similar to what you just did for the at() function. In main, put everything up to "Testing remove" back in. Step 6: Remove This is going to work similarly to your old Remove(), with one addition. The old part: You should pass this function the value you want to remove, and then delete the first array element equal to that value. In the example below, we have the values 200 through 209 in an array, and we pass 203 to remove: (len is 10 before calling remove) 200 201 202 203 204 205 206 207 208 209 Here's what the array should look like after deletion. Note that, because the length is 9, we will consider the second 209 as arbitrary garbage; it's past the end of the array we're modeling. 200 201 202 204 205 206 207 208 209 (len is now 9) This function should return true if the delete was successful, and false if the value didn't match any array element. For example, for this array, remove(214) should return false. The new part: If the user has deleted a lot of elements, you should resize your array to make it smaller. Here's the formula: If the array length has dropped below half of the capacity (i.e. < 0.5*capacity), you should resize it to 80% of the capacity, rounding down. HOWEVER, the array should never go below the initial capacity (INITIAL_CAP), no matter how many elements are in it. Resizing the array to make it smaller should be no different than resizing to make it bigger. Again, if you feel like an extra challenge, consider refactoring your code to eliminate redundancies. Then add this function to your class, and run the original main, with all tests back in. . prob 209 but it doesn't matter Final deliverable Ensure your code follows Style guidelines related to variable Naming, Comments to explain the logic, Whitespace around most operators, Curly-Braces around control statements (if, while, for), and logic to explain their use. Document functions by describing: ● O the function's intended use, O the function's parameters, using a @param tag for each function/method parameters O the function's return value, using a @returns tag (if applicable), O any pre-conditions using a @pre tag. o any post-conditions using a @post tag.
Expert Solution
Step 1: Part1:

Certainly! Below is a simplified example of a C++ code implementing dynamic arrays based on the provided instructions. This code includes the default constructor, destructor, append, at, sum, and remove functions for the DynamicArray class. Please note that this is a basic implementation, and you may need to adapt it to fit your specific requirements.


// dynamicarray.h
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

class DynamicArray {
private:
    int* arr;
    int len;
    int capacity;

public:
    // Constructor
    DynamicArray();

    // Destructor
    ~DynamicArray();

    // Append function
    void append(int value);

    // At function
    int at(int index) const;

    // Sum function
    int sum() const;

    // Remove function
    bool remove(int value);

    // Capacity function
    int cap() const;
};

#endif // DYNAMICARRAY_H

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Concept of pointer parameter
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.
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