-1

I have method(void DE(...) that finds parameters of function y=f(x, a, b...) based on some data points. When I want to use function, that has other number of arguments, I need to comment first function, write second function, add (X.PAR[i][1],..., X.PAR[i][n]) arguments of this function every time I use func(y, x, param_1....param_n). How can I make function f(double x1, x2,...xn)to be an argument of function DE, so i wont need to change my program for every other function?

double f(double x1, x2,...xn){

}
void DE(int n){
 double A[n];
 fill(A) //some function that makes up values
 cout<<f(A[0], A[1],...A[n-1]);//if I change number of arguments in f, I also need to change it here
}
Dikzamen
  • 70
  • 1
  • 5
  • 4
    Those `#define`s can get you into trouble. They are simple text substitution macros, and they don't give a if it makes sense to make the substitution or not. If you're going to use macros use identifiers what are easy to distinguish and hard to accidentally reuse. – user4581301 Nov 26 '19 at 22:14
  • 1
    In addition to variadic arguments, take a look at [variadic templates](https://en.cppreference.com/w/cpp/language/parameter_pack). Though in this case it looks like you want to pass in a [` std::vector`](https://en.cppreference.com/w/cpp/container/vector) of values. – user4581301 Nov 26 '19 at 22:15
  • 1
    Recommended reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Nov 26 '19 at 22:17
  • Replaced those "define" – Dikzamen Nov 26 '19 at 23:15
  • 1
    That seems like a lot of code given the question. Could you trim the code down to help people focus on just what's relevant to your question? (Keep in mind that your example code does not have to calculate anything useful for your real project; it just has to demonstrate the coding issue. Dummy placeholder functions might be a good idea.) – JaMiT Nov 27 '19 at 00:00
  • 99% of the shown code is completely irrelevant to the question. The actual question is actually fairly simple, and has an easy answer. However answering the question will simply encourage more bad stackoverflow.com questions, dumping their entire pile of code and demanding to know why X doesn't work or how to do Y, when either X or Y is less than 1%, and putting no effort into assembling a clear, short, concise question. – Sam Varshavchik Nov 27 '19 at 00:56

1 Answers1

0

OK, some things:

  1. double A[n]; variable length arrays are not standard c++, if you really, really want to use c-array you have to do it in another way (costant size, new).
  2. But, you don't really want c-array, you just need a vector, it brings away all of your problems.
  3. If your needings are more complicated than your example and you are dure you want to use this approach, you could pass the function to be called at your method.

Here's an example with the solution for the second point and an hint to approach the solution at point 3:

#include <iostream>
#include <vector>
using namespace std;

void fill(vector<double> &vec)
{
    vec.push_back(12);
    vec.push_back(1.3);
    vec.push_back(4.7);
}

double f(const vector<double> &vec)
{
    double some_value = 1;
    for(double val:vec)
        some_value = some_value*val;
    return some_value;
}

double fn(double a1, double a2, double an)
{
    double some_value = 1;
    some_value = some_value*a1*a2*an;
    return some_value;
}
void DE()
{
   vector<double> A;
   fill(A); //some function that makes up values
   cout<<f(A)<<endl;
}
template<typename Func>
void DE2(Func func)
{
   double A[3];
   A[0] = 12;
   A[1] = 1.5;
   A[2] = 4.6;
   cout<<func(A[0], A[1], A[2])<<endl;
}

int main()
{
    DE();
    DE2(fn);
}
Federico
  • 428
  • 6
  • 15
  • Thank you for answer. It works well for different functions with same number of arguments. I would like something like this, but i get "too many/few arguments" error ```void DE2(Func func, int n) { double A[n]; fill(A[n], n); switch(n){ case '2': cout< – Dikzamen Nov 27 '19 at 09:40
  • well, did you have defined the `func` that accepts a different number of arguments? and still, you need to change that `double A[n];` declaration – Federico Nov 27 '19 at 10:47
  • I have several functions, that have different number of arguments like ```func2(a1, a2){} func3(a1, a2, a3){}...``` I'll try exceptions to avoid this error. Thank you for help, i accepted the answer. – Dikzamen Nov 27 '19 at 11:20
  • anyway, all these doubts and over-complications were the reason to prefer the `vector` version, simpler and cleaner – Federico Nov 27 '19 at 11:31
  • Problem was in functions with different number of parameters. In original problem i had 2D array and i represented it like this ```struct Arr { double X[NP][D]; }; ```(NP vectors that have D components – Dikzamen Nov 27 '19 at 11:43
  • @Dikzamen even better, it will be a `vector> X;` and you can pass just the `vector` you need as `X[i]`. but your choice, if you feel this solution fits better to your needings go with that. – Federico Nov 27 '19 at 12:02