Hello. According to the task, I create pointers to functions of the following form:

int (*func1)(int *); int (*func2)(int *, float *); int (*func3)(void *); float (*func4[N])(double, double, double); 

They must be used to call functions that calculate the values ​​of expressions. For the last array of pointers to the fourth function, you need to use switch() . There are no errors during compilation (in CodeBlocks). But at runtime (after entering the index into a pointer array), the program gives an error. It is also not clear why the variable с gets the value 0. Code:

 #include<iostream> #include<cmath> #include<cstdio> #include<cstdlib> using namespace std; int func_a(int*); int func_b(int*, float*); int func_c(void*); float func_H(double a, double b, double c) { return 2 * a + 3 * b + 4 * c; } int main() { const int N = 10; int (*func1)(int*); int (*func2)(int*, float*); int (*func3)(void*); float (*func4[N])(double, double, double); func1 = func_a; func2 = func_b; func3 = func_c; for (int i = 0; i < N; i++) func4[0] = func_H; double a, b, c, H; int x, x1; float x2, x3; int* px, *px1; float* px2; void* px3; px = &x; px1 = &x1; px2 = &x2; px3 = &x3; cout << "Введите значение x (int): "; cin >> x; cout << "Введите значение x1 (int): "; cin >> x1; cout << "Введите значение x2 (float): "; cin >> x2; cout << "Введите значение x3 (float): "; cin >> x3; a = (double)(*func1)(px); cout << "\nЗначение переменной a: " << a << endl; b = (double)(*func2)(px1, px2); cout << "\nЗначение переменной b: " << b << endl; c = (double)(*func3)(px3); cout << "\nЗначение переменной c: " << c << endl; int index = 0; cout << "\nВведите индекс указателя на функцию в массиве:"; cin >> index; switch (index) { case 0: H = (*func4[0])(a, b, c); break; case 1: H = (*func4[1])(a, b, c); break; case 2: H = (*func4[2])(a, b, c); break; case 3: H = (*func4[3])(a, b, c); break; case 4: H = (*func4[4])(a, b, c); break; case 5: H = (*func4[5])(a, b, c); break; case 6: H = (*func4[6])(a, b, c); break; case 7: H = (*func4[7])(a, b, c); break; case 8: H = (*func4[8])(a, b, c); break; case 9: H = (*func4[9])(a, b, c); break; default: cout << "Значение индекса должно находится в интервале [0; 9]"; break; } cout << "\nЗначение функции H: " << H << endl; system("pause"); return 0; } int func_a(int* px) { return (int) pow(*px, 2) + pow(*px, 3); } int func_b(int* px1, float* px2) { return (int) log(abs(*px1)) - pow(*px2, 4); } int func_c(void* px3) { int* Px3 = static_cast<int*>(px3); return (int) pow(cos(*Px3 - 4.0), 2); } 

I would be very happy to fidbeku. (:

Program execution and error window

    1 answer 1

    May be not

     for (int i=0; i<N; i++) func4[0]=func_H; 

    but

     for (int i=0; i<N; i++) func4[i]=func_H; 

    ?

    • Yes thank you. Literally just fixed. That's just the value of 's' remains zero. I'm puzzling over it now. - Nastya Shpit
    • four
      I also need Newton's bin :) return (int) pow(cos(*Px3-4.0), 2); Cosine what value can you take? From -1 to 1. And in the square? From 0 to 1. And lead to the whole (drop the fractional part)? only 0 (you can hardly guess so with the value of x3 to get a cosine equal to 1 ...) - Harry
    • one
      Is logical. Thanks again for your help. - Nastya Shpit
    • @ Nastya Shpit, Harry is always logical ... - AR Hovsepyan