+ 1

How do I add values in an array using a loop without declaring the array size?

Array and loops

6th Apr 2017, 12:34 AM
itumeleng
itumeleng - avatar
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
6th Apr 2017, 12:44 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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
6th Apr 2017, 12:43 AM
Burey
Burey - avatar
+ 7
How about making a blank array. One where you do not declare the size, and add as many values as you want.
6th Apr 2017, 12:48 AM
Manual
Manual - avatar
+ 2
Why not using vector. It is works as array also it is dynamic, you can add or pop element dynamically.
6th Apr 2017, 1:16 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 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:)
7th Apr 2017, 8:19 AM
Norbivar
Norbivar - avatar