DInamic Arrays

Hi everyone!!!

I'm writing code which has an array and i have to decrement it while ANY of it's elements is > 0. I'm doing it using following code:

int ar[4] ={10};

while(ar[0] >0 || ar[1] >0 || ar[2] >0 || ar[3] >0){
 for(int i=0; i<4; i++)
    ar[i] -= 2;

it works ok, until the point in the code when i have to dynamically increase the array size. After that I don't know already how many elements are there in my array. Thus the condition

while(ar[0] >0 || ar[1] >0 || ar[2] >0 || ar[3] >0){

is no longer valid.

My question is there any way how to set the condition of doing something with the array while ANY of it's elements are/is still greater than 0 without specifying the concrete size of the array?
Like in shell script you can do something like while(ar[@] > 0)...
Is there something like this in c++?

Thank you in advance!!!
Last edited on
If you can't use a container type that keeps track of its own size, keep track of the size of the array yourself.
Last edited on
Actually I'm not trying to keep truk of array size. I'm trying to find out if ALL the elements of the array are less tan zero.

and the example I gave is the only way I can think to solve this problem.

My problem is I don't know how many are there elements in the array to make the condition like this:

while(ar[0] >0 || ar[1] >0 || ar[2] >0 || ar[3] >0..... ar[n] > 0){

Because I I don't know the size of n after the array would be incremented.
Last edited on
Because I I don't know the size of n after the array would be incremented.

If you don't know this, how is it being incremented? Do you not have access to the code that does the incrementation?
Topic archived. No new replies allowed.