One of the functions in my program needs to count the elements of an array or data file for use in computing an average of the elements sum. How might I go about getting this number for a function?
One of the functions in my program needs to count the elements of an array or data file for use in computing an average of the elements sum. How might I go about getting this number for a function?
The following will only work on arrays where the last element is a null character.
However, in a well designed system you should always know the size of your arrays, be it by defining a constant to set the base size or keeping a counter for a dynamic array. That number - 1 would be the actual length of the array.Code:int countArray(char *array){ int n = 0; while(array[n] != '\0') n++; return n; }
-Prelude
My best code is written with the delete key.