15.9k views
4 votes
Create a ** dynamically allocated ** vector of FIXED size. (5 for example) Read a string from the keyboard Store it in the vector (keep a counter)

When the vector hits maximum size, create a new vector of bigger size Copy the original vector into the new one Repeat until the user says they don't want to add any more. Print out the list

User Dalilah
by
7.4k points

1 Answer

4 votes

Final answer:

To create a dynamically allocated vector, start with a fixed size and read strings from the keyboard, store them until the vector is full, then allocate a larger vector, copy the original contents, and repeat as needed until the user stops adding strings. Finally, print out the stored strings.

Step-by-step explanation:

The process described involves creating a dynamically allocated vector and performing operations such as reading strings from the keyboard, storing them, and handling vector resizing when it reaches its capacity. Below are the steps coded in a general programming language:Initialize a dynamic vector with a fixed size (e.g., 5 elements).Read strings from the keyboard using a loop.Increment a counter as each string is added to the vector.

When the vector reaches maximum size, allocate a new larger vector.Copy the contents of the original vector to the new, larger vector.Free the memory of the original vector if necessary (depending on the language).Repeat the process until the user decides to stop inputting strings.Print out all stored strings after completion.The implementation of this process will vary based on the programming language being used. A typical approach would use loops, dynamic memory allocation, and possibly functions from a standard library for string input and memory operations.

User Cool Mr Croc
by
7.5k points