beginner c++ array == 0 ?

hi, do you guys know how to set a loop for condition where it stops when all the array = 0 ?

i tried something like this but it only works for i times.. not until all the array =0..

1
2
3
4
5
6
7
8
9
10
11
12
arr1[5]= {5,5,5,5,5};
arr2[5]= {5,5,5,5,5};

for (int i= 0; i <5; i++)
{
   arr[i];
   do
   {
      //statements//
    }
    while ( arr1[i] == 0 && arr2[i]==0);
}
Last edited on
Line 6 does nothing, assuming arr[] is even defined.

You're going to have to write a function that tests if an array is all zeroes.
1
2
3
4
5
6
7
bool array_is_null (int arr[], size_t sz)
{  for (int i=0; i<sz; i++)
    {  if (arr[i] != 0)
          return false;  // No point in checking further
    }
    return true;  // All entries zero
}

Then you can change line 11 as follows (assuming you want to continue until both arrays are null):
 
    while (! (array_is_null(arr1,5) && array_is_null(arr2,5)));

i see! i will try it.
Last edited on
Topic archived. No new replies allowed.