The point is that you need to write a program to sort an array by function without using global variables. I decided to go by calling functions as parameters for functions (after all, this can also be done with pointers ???) Listing my "miracle" code:

#include <cmath> #include <iostream> using namespace std; void print(double a[10]) { //double a[10]; for (int i = 0; i < 10; i++) { cout << a[i] << " "; } } double find_min_element(double a[10]) { double var; // double a[10]; int n = 10; var = 9223372036854775807; int index = 0; for (int i = 0; i < n - 1; i++) { if (a[i] < var) { var = a[i]; index = i; } } return index; } double sort_array(double a[10]) { //double a[10]; int m; int n = 10; for (int i = 0; i < n; i++) { m = i; for (int j = i + 1; j < n; j++) { if (a[j] < a[i]) { m = j; swap(a[i], a[m]); } } cout << a[i] << " "; } return a[n]; } 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]; } int main(int argc, char** argv) { //using pointer_to_function = double(*)(); //int (*pointerToFunction)(double ) = NULL; //pointerToFunction = &filling; pointer_to_function ptf = filling; filling(); find_min_element( ptf); /*В find_min_element надо отправить заполненный массив. Такой у нас имеется после вызова filling. Так как глобальные переменные использовать нельзя, то получается, что в main'е зацепить массив в переменную я не могу*/ print(ptf); return 0; } 

At the output, the compiler produces the following errors

g ++ -Wall -c "Functions.cxx" (in the directory: /home/alex/Yandex.Disk/Programming/1D_arrays_from_functions)

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 (*) ();

^ Functions.cxx: 97: 2: error: 'pointer_to_function' pointer_to_function ptf = filling;

^ Functions.cxx: 99: 19: error: 'ptf'
find_min_element (ptf);
^ Build failed.

Wednesday - Geany, g ++. ^ - separator between errors.

  • one
    From the fact that “global variables cannot be used”, it follows that local variables should be used. Why are pointers suddenly not functions - it is not clear. - AnT

3 answers 3

I'm sorry, but you have the code ... Let's go over it.

 void print(double a[10]) { //double a[10]; for (int i = 0; i < 10; i++) { cout << a[i] << " "; } } 

Perhaps one of the really working functions :)

 double find_min_element(double a[10]) { double var; int n = 10; var = 9223372036854775807; 

What a strange constant? Moreover, double does not support such accuracy, and will cut it off anyway. And if you wanted the maximum double value , then this is numeric_limits<double>::max() .

  int index = 0; for (int i = 0; i < n - 1; i++) 

Why do not you consider the last element of the array? And anyway, why introduce a variable if you have a hard-coded constant?

 double sort_array(double a[10]) { 

Honestly, at a quick glance like and nothing ... but why this variable m ? int m;

And what do you return? Non-existent array element? What for? return a [n]; }

 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]; } 

Let's say. Filled. Filled the local array, which will disappear when exiting the function ...

  return a[i]; 

And again returned a non-existent element.

 int main(int argc, char** argv) { //using pointer_to_function = double(*)(); 

This is the correct type declaration for the fill pointer, just uncomment :)

  //int (*pointerToFunction)(double ) = NULL; 

And this is some kind of nonsense; if you wanted to make a pointer to filling , then you need to write double (*ptf)() = filling;

  pointer_to_function ptf = filling; filling(); find_min_element(ptf); 

Your function requires passing an array to it, and you pass it a pointer to the function. And what should she do with it?

  print(ptf); 

Similarly.

Actually, everything that the teacher wanted you to do, I put it here (just by reading the array by filling in random numbers): http://ideone.com/w70pVj You needed to transfer the array and its size. Everything. So there are no global variables. It is possible (and sometimes necessary :) to work with function pointers, but not in your case ...

    Use std :: vector instead of an array. Or, along with the array, transfer its size. In your case 10. You can do without global variables and without function pointers. The sort_array function returns 1 double a [10] values, which is outside the bounds of the array. Fill in the same way.

      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 )();