+ 1
How do I add values in an array using a loop without declaring the array size?
Array and loops
5 Respuestas
+ 13
You can have the user input the array size.
int array_size;
cin >> array_size;
int array[array_size];
for (int i = 0; i < array_size; i++)
{
array[i] = 0;
}
// array filled with zeros
+ 7
either use dynamic array and continously allocate new space, or use a linked list..... or just use vectors, much simpler and not messy :)
https://www.tutorialspoint.com/cplusplus/cpp_stl_tutorial.htm
+ 7
How about making a blank array.
One where you do not declare the size, and add as many values as you want.
+ 2
Why not using vector. It is works as array also it is dynamic, you can add or pop element dynamically.
+ 1
Do note that vectors may relocate from their original place in the memory.
That only means that if you keep a reference or pointer to a member in the vector and push something into it it MAY invalidate the pointer/reference. That is, they may point to unallocated space.
Just a sidenote:)