There are several errors in your program.
The print function will correctly be declared by specifying the second parameter — the size of the array, since it is not possible in the function itself to determine the size of the array passed to it as an argument, since it is implicitly converted to a pointer to its first element. That is, in this function parameter declaration
void print(double a[10]); ^^^^
the number 10 does not play any role, since the same function can be declared with any value of the size of the array, which is simply ignored in the sense of converting an array parameter to a pointer to an array element. That is, these function declarations are equivalent and declare the same function.
void print(double a[100]); void print(double a[10]); void print(double a[]); void print(double *a);
Therefore, it will be correct to define the function as follows.
void print( const double a[], size_t n ) { for ( size_t i = 0; i < n; i++ ) { std::cout << a[i] << " "; } }
You can declare a function with one parameter. But in this case, the type of the parameter should be a reference to the array. for example
void print( const double ( &a )[10] ) { for ( size_t i = 0; i < 10; i++ ) { std::cout << a[i] << " "; } }
Or, if the compiler supports a for loop based on a range, then you can write
void print( const double ( &a )[10] ) { for ( double x : a ) { std::cout << x << " "; } }
For the find_min_element function find_min_element there is exactly the same problem with the function declaration. In addition, a certain magic constant is used, it is not clear where it came from, the use of which can give a result not the one you expect, since when converting to double type, its inaccurate representation may take place.
Besides, for some reason, the last element of the array is ignored.
for (int i = 0; i < n - 1; i++) ^^^^^^^^^
There is a standard algorithm std::min_element , declared in the header, which performs the task of finding the minimum element of an array. In addition, you declared the type of the return value of the function as double while returning the index to the minimum element. It would be more logical if the return type of the function's value were integer. Otherwise, it is misleading to read your code, since you might think that it is not the minimum value index that is returned, but the minimum value itself.
size_t find_min_element( const double a[], size_t n ) { size_t min_i = 0; for ( size_t i = 1; i < n; i++ ) { if ( a[i] < a[min_i] ) { min_i = i; } } return min_i; }
Or if you declare the first parameter of the function as an array reference, then
size_t find_min_element( const double ( &a )[10] ) { size_t min_i = 0; for ( size_t i = 1; i < 10; i++ ) { if ( a[i] < a[min_i] ) { min_i = i; } } return min_i; }
In the sort function, there is the same problem described above with the function declaration. Modify its declaration as shown for other functions. In addition, the function has an undefined behavior, since it returns from the function a non-existent array element. That is, there is a memory access outside the array.
return a[n]; ^^^^^
There is no point in returning something from a function. Therefore, it is better to declare the return type as Void . For example,
void sort_array( double a[], size_t n );
or
void sort_array( double ( &a )[10] );
In the filling function, the local array is filled, which will cease to exist after exiting the function, and the non-existent element of the local array returns from the function again. That is, the function has an undefined behavior.
Edit her ad as
void filling( double a[], size_t n );
or how
void filling( double ( &a )[10] ); double filling() { int i; double a[10]; int n = 10; cout << "fill the row, 10 elements" << endl; for (i = 0; i < n; i++) { cin >> a[i]; } return a[i]; }
Judging by the first error message shown
Functions.cxx: In function 'int main (int, char **)': Functions.cxx: 94: 8: error: expected nested-name-specifier before 'pointer_to_function' using pointer_to_function = double (*) ();
Your compiler does not support alias declarations using the using keyword.
So try replacing it with a typedef declaration.
typedef double( *pointer_to_function )();