The polyval has a nested loop. Both loops introduce 'i'. I'm not sure whether the inner masks the outer or not.
Lets assume it does. Lets assume that n=2. Now we can unroll the loops to make it easier to see what happens:
px = p[0]*pow(x,2) + p[1]*pow(x,2) + p[2]*pow(x,2) + p[0]*pow(x,1) + p[1]*pow(x,1) + p[2]*pow(x,1);
Is that what it should be?
You don't call polyder. That part of your main is essentially:
1 2 3 4 5 6
|
int main()
{
float dp; // uninitialized, undefined value
cout << "Derivative = " << dp << endl;
return 0;
}
|
Note: i
nt main(void)
is non-standard signature for main; use
int main()
.
Line 45 says clearly
float * dp
and therefore 'dp' in the function is a pointer. Pointer holds an address. Parameter's value is set by the caller. Your function can change the address hold by pointer, but then it does not point to the same memory location as the caller gave and the caller cannot possibly know what you will do in that other memory location.
Note: you do allocate a memory block dynamically on line 15, but you don't deallocate it at the end of the program. You could use a smart pointer (std::unique_ptr) instead of plain pointer. Smart pointer deallocates the memory it points to when it exits the scope.
However, a better solution is to use std::vector and pass the vector to functions by reference. Vector has size attribute and therefore you won't have to pass 'n' separately to the functions.