Hey there,
I have a probably straight-forward question about the following problem:
I have a function
double afunction(double *myarray)
{
double ret = 1.0;
for(int i = 0; i < 4; i++)
ret *= myarray[i]*myarray[i];
return ret;
}
Now I want to modify it so that I pass two new parameters: int index describing which index of myarray to change like the following: myarray[ index ] = *add;. This would be the following:
double afunction(double *myarray, int index, double *add)
{
myarray[ index ] += *add;
double ret = 1.0;
for(int i = 0; i < 4; i++)
ret *= myarray[i]*myarray[i];
return ret;
}
The problem is: I don’t want to modify the array myrray and I don’t want to create a new array for this because of memory-regards (this will later be computed on the GPU and there I couldn’t allocate a whole new array in a kernel function anyway.
Easy solution to this?
Thanks!
EDIT
Sorry, I mistyped something. Instead myarray[ index ] = *add; I meant to say myarray[ index ] += *add;
EDIT2
Example of a bigger function which could later on easily be extended to about 50 different return-cases. So to have the if-statement to modify the certain value myarray[index] with adding *add in each return-case is pretty ugly 🙁
double afunction(double *myarray, int funcIndex, int indexAdd, double *add)
{
myarray[ indexAdd ] += *add;
if(funcIndex >= 1 && funcIndex <= 4)
return myarray[1]*myarray[1]*myarray[2];
switch(funcIndex)
{
case 5:
return sin(myarray[3]) * cos(myarray[1]);
case 6:
double ret = exp(myarray[1]);
for(int i = 1; i < 5; i++)
ret *= (myarray[ i ]-myarray[ 5-i ]);
return ret;
case 7:
double ret = 0.0;
for(int i = 1; i < 10; i++)
ret += myarray[ i ];
return ret;
}
return 0.0;
}
1 Answer