0
i created a programe for printing all prime no between say 1 to 100 now i want to store all that in an array,how to do that?
only storing all that number in the array.
5 Respostas
+ 1
Push the numbers back into the array as you find them. Then iterate through the array to print or change it as needed. I generally use vectors and the syntax for that is:
vector_name.push_back(value_name)
I'm not sure if arrays use the same syntax as vectors, but you could try, if not try using a vector.
Syntax: vector<int>vector_name{};
Or look through StackOverflow as I guarenttee you this question has probably been answered there.
+ 1
arrays are immutable, you would need to use a vector or set the array to a fixed size beforehand arr[100] for example.
alternatively keep allocating more memory and deleting the previous array.
+ 1
arrays can't have numbers pushed into mr.robot
+ 1
Yeah i use vectors, not much knowledge on arrays in the c lang. Thanks for clearing that up, destro. I would use a vector then
+ 1
no problem! usually I keep to this philosophy :
known number of elements? array
possibly more or less than needed? vector