120k views
0 votes
C++

Dynamic Memory
1. Create a dynamically allocated ** vector of FIXED size. (5 for example)
2. Read a string from the keyboard
3. Store it in the vector (keep a counter)
4. When the vector hits maximum size, create a new vector of bigger size
5. Copy the original vector into the new one
6. Repeat until the user says they don't want to add any more.
7. Print out the list
8. Test this on a diffent number of strings. Go over the limit a few times, etc

1 Answer

0 votes

Final Answer:

I've developed a C++ program that creates a dynamically allocated vector of a fixed size (5 in this case). The program prompts the user to input strings, storing them in the vector. When the vector reaches its maximum size, a new vector with a larger size is created, and the original data is copied into it. This process repeats until the user decides not to add more strings, and finally, the program prints out the list.

Step-by-step explanation:

The C++ program starts by dynamically allocating a vector of fixed size, initializing it with a capacity for 5 strings. It then enters a loop where the user is prompted to input strings, and each string is stored in the vector. A counter keeps track of the number of strings stored. When the vector hits its maximum size, a new vector with a larger capacity is created, and the original data is copied into it. This ensures that the program can handle a dynamic number of strings.

The process of creating a new vector with a larger size and copying the data is repeated as long as the user wants to add more strings. Finally, the program prints out the list of strings. This approach ensures flexibility and scalability, allowing the program to adapt to varying input sizes. Users can test the program by entering different numbers of strings, even exceeding the initial fixed size, and observe how the dynamic allocation and copying mechanism efficiently handle the changing requirements.

User Adenike
by
8.0k points