'How to get whether there are elements left in array or not

int main()
{
    int arr[10000] = {1, 2, 3, 4};
    int i = 0;
    int num = 0;
    int n = 10;
    
    while(arr[i] != null){
        num = num * n + arr[i];
        i++;
    }
    
    cout << num << '\n';
    
    return 0;
}

It throws error, but i don't know how to get if there're left any elements in my array or not, but i don't want to change null with 0, because it is possible certain element to be 0.



Solution 1:[1]

You can initialize your array with a number that you're not going to use. Suppose you do it with -1 or you can even take INT_MAX (max value 32-bit integer can store).
But keep in mind that in order to use INT_MAX you'll need to include the header: #include<limits.h>

Now you need to initialize the array either by simply running a loop over the size of the array or by using the memset function.

Now you can use while(arr[i] != -1) or while(arr[i] != INT_MAX). The loop will stop after your elements are done processing. That way you'll know if there are elements left in your array. Hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1