The pseudo-code below depicts a computation I want to do. The idea is designing a C/C++ function that takes any mathematical function of x
and evaluates the sum of the first N
terms. function(x)
could be any function e.g. 2x-1 , 2x , 1/x
, etc . x
varies from zero
to N
. I think challenge is how to design the function(x)
datastructure , am not sure if this is achievable without any datastructure(this would be better) .
function(x) = 2*x - 1 ;
sum_expression_to_N( function(x) , N ){
float sum = 0.0;
for ( int x =0; x<=N; x++){
sum = sum + function(x)
}
return sum ;
}