First of all, on function parameters:
T foo( U bar[] );
means exactly the same as:
T foo( U * bar );
In other words, a function does not take
array. It merely takes a
pointer to (first) element in array.
A pointer does not now whether the pointed to element is part of array, nor how many elements the array has.
That is why one has to pass the size to function separately:
T foo( U * bar, size_t size );
(The
size_t
is an unsigned
int
type. More logical for sizes and indices that should not be negative.)
On reference code you could count from line 6 that the array has exactly 5 elements.
The code, however, does not rely on human eyes and uses
sizeof
on line 9 to count for you.
On your code the element count is very much known; it is 10.
I'll refactor the reference code into separate function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
int small( int* data, size_t size )
{
if ( data and 0 < size ) { // sanity check
int smallest = data[0];
for ( size_t index=1; index < size; ++index )
if ( data[index] < smallest )
smallest = data[index];
return smallest;
}
else return 0; // dummy, there was no array
}
int main ()
{
int array []= {4,2,3,1,5};
int smallest = small( array, sizeof(array)/sizeof(array[0]) );
std::cout << smallest << '\n' ;
return 0;
}
|
Note that C++ Standard Library has a function:
http://www.cplusplus.com/reference/algorithm/min_element/
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
#include <algorithm>
int main ()
{
int array []= {4,2,3,1,5};
int smallest = *std::min_element( std::begin(array), std::end(array) );
std::cout << smallest << '\n' ;
return 0;
}
|
PS. Your lines 84-89 do not do what you probably expect them to do.