A
A
ashot_powershot2022-03-06 22:04:41
C++ / C#
ashot_powershot, 2022-03-06 22:04:41

A function with a parameter pointer to another function. How to do it right so that it does not give an error?

It is necessary to make a function with a parameter a pointer to another function. There was a problem with the array and I do not understand how to solve it.

Error (active) E0167 type argument "double" is incompatible with type parameter "double (*)(int, int)"
This error is thrown on this line: analiz(func(n));

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<conio.h>
#include<ctime>
#include<windows.h>

//Вариант 14. Создать функцию, которая описывается следующим прототипом:
//double analiz(int *op1, int op2, double(*func)(*int, int))
//{
//	return func(op1, op2);
//}
//где функция func – возвращает сумму четных из массива n - целых чисел, 
//сумму нечетных массива n - целых чисел, среднее геометрическое из массива n - целых чисел.
//В главной программе выполнить вызов всех реализаций функции analiz.




using namespace std;

int const y = 6;
double func(int n[y][y])
{
  int op1 = 0, op2 = 0;
  double s = 0;
  for (int j = 0; j < y; j++)
  {
    for (int i = 0; i < y; i++)
    {
      if (n[j][i] % 2 == 0)
      {
        op1++;
      }
      else
      {
        op2++;
      }
      s *= n[j][i];
    }
  }
  s = pow(s, 1 / y);
  return op1, op2, s;
}
double analiz(double(*func)(int, int))
{

  cout << func << endl;
}
int main()
{
  setlocale(LC_ALL, "RUS");
  srand(time(NULL));
  int n[y][y];
  int op1 = 0, op2 = 0;
  cout << "Массив n" << endl;
  for (int j = 0; j < y; j++)
  {
    for (int i = 0; i < y; i++)
    {
      n[j][i] = rand() % 10;
      cout << "n[" << j << "][" << i << "] = " << n[j][i] << endl;
    }
    cout << endl;
  }
  cout << endl;
  analiz(func(n));
  system("PAUSE");
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2022-03-06
@ashot_powershot

analiz(func(n));
This is where you call the func function and pass the result of its work to analiz. Although analiz wants to receive a function. You have to do it like this:analiz(&func);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question