Sunday, 29 April 2012

C++ Program to Count the Elements of an Array till a Zero Element Appears


#include <assert.h>
#include <iostream>

int a[10] = {4, 5, 8, 9, 8, 1, 0, 1, 9, 3};
int i;

int main(  )
{
    i = 0;
    while (true) {
        assert(i >= 0);
        assert(i < sizeof(a)/sizeof(a[0]));

        if (a[i] == 0)
            break;

        ++i;
    }

    std::cout << "Number of elements before zero " << i << '\n';
    return (0);
}

No comments:

Post a Comment