2.7k views
1 vote
There are 7 files in this project, and they are down below. And I guess the professor wants us to modify the set.cpp make sure the numbers are entered by the user to be set dynamically. If you pay attention to the set.cpp I marked in bold a section that may be needed to be changed. I have put all the codes down so that you can copy them to your computer to have an idea of how they work together. Only that Set.cpp must be modified so the number of elements can be set dynamically during the program's execution. And please can you be more specific about what line of code needs to be replaced?

** Note: The goal of this assignment is to MODIFY the existing code. **

Implement a dynamically allocated version of the mathematical concept of a 'set'. First, examine the provided library and driver (in main) for a statically allocated Set class. You may also want to refresh your mathematical memory of the set concept before proceeding.

Now that you are familiar with how the Set class works, let's make it work better. Currently, the user is limited to a certain maximum number of elements in their Set. Change it so the number of elements can be set dynamically during the program's execution.

Extend the driver to test all the ADT's operations.

** Things to Consider **

1. If your set is implemented in dynamic memory:

1a. How do you access the members?

1b. How and when do you re-size the memory?

User Protoproto
by
8.2k points

1 Answer

5 votes

Final answer:

To modify the Set.cpp file and implement a dynamically allocated version of a Set class, you need to use pointers and dynamic memory allocation. Resize the array by creating a new array with a larger size, copying the existing elements, and deleting the old array.

Step-by-step explanation:

The goal of this assignment is to modify the existing code for a Set class and implement a dynamically allocated version. Currently, the user is limited to a certain maximum number of elements in their Set, but the task is to change it so the number of elements can be set dynamically during the program's execution. To achieve this, you need to modify the Set.cpp file.

To implement a dynamically allocated Set, you would need to use pointers and dynamic memory allocation. So, instead of initializing the array with a fixed size, you would create a dynamic array using the 'new' operator. Here's an example:

int* elements = new int[maxSize];

When the user needs to add more elements than the current size, you would resize the array by creating a new array with a larger size, copying the existing elements to the new array, and deleting the old array. Here's an example:

int* newElements = new int[newSize];
for (int i = 0; i < currentSize; i++) {
newElements[i] = elements[i];
}
delete[] elements;
elements = newElements;

User Exebook
by
8.7k points