@Drac0,
I am referring to to your final code posting in
http://www.cplusplus.com/forum/beginner/244443/#msg1082853
Global variables:
- I think the only variables with any reason to be global here are C1, C2, C3 - they are specific to your particular function being a quadratic and would make argument lists quite long otherwise. There is a possible argument for TestTolerance, but it is only used in the derivative function and might be better defined there.
- All of your other variables should be passed through the function calls: either as arguments or as return values. They should not be global variables.
- In particular, initialising Number_of_Loops here would mean that if the derivative function were called twice then it would be incremented further each time; for example, it might tell you that you took 10 loops the first time, 20 the second, 30 the third etc.
Introduction:
- OK, but keep it short.
Input:
- If C1, C2, C3 are global variables then don't declare (pointers to) them in the argument list. You can simply set them directly.
- There is no need to use a pointer variable for X. Declare it as a local variable and just
return X;
at the end of the function. Note that it would reduce complexity to call it X, not XValue.
- Do not have global variable XValue; the function is returning this.
- I suggest that the declaration for the Input function should simply be
double Input()
It will return X as the function return value, whilst C1, C2, C3 are global variables anyway.
- If you accept this advice, then this function becomes
1 2 3 4 5 6 7 8 9 10 11
|
double Input() {
cout << "Please input your values for C1, C2 and C3: ";
cin >> C1 >> C2 >> C3; // These (slightly reluctantly) are global variables
cout << " The function you entered is : " << C1 << "x^2 + " << C2 << "x + " << C3 << "\n\n";
double X;
cout << " Using this function the program will calculate the derivative at a x value that you input\n";
cout << " Please input the x value you wish to use to find the derivative:\n";
cin >> X;
return X; // The position is sent back as the return value of the function
}
|
Double F_X:
- It is difficult to sort this out. The logical thing would be to send it the value of X (passed by value through the argument list) and simply to return the value of the function at this point.
- Thus, it could be as short as
1 2 3 4
|
double F( double X )
{
return C1 * X * X + C2 * X + C3;
}
|
- Note that I suggest simplifying names a bit here.
- In particular, you shouldn't try to do anything at a neighbouring point (X+dX) here.
- Fprime - as calculated in your function in the latest code - doesn't really calculate anything. As written, it is not an approximation for the derivative of F (as its name definitely implies, or as it is used in the following function). It simply shouldn't appear in this function or in a list of global variables.
"Calculation"
- Bear with me, I think this function should simply be called Deriv and have declaration
double Deriv( double X )
- If DeltaX and FPrime are used here they must have been initialised elsewhere. Here, you have used global variables, so any values they take on entry would be whatever they were on the last call - in the case of DeltaX this would be badly wrong if the function were called multiple times, since you would pick up from an already-small value. Similarly, Number_Of_Loops would be further incremented from any earlier call, not started afresh.
- This routine should do nothing but calculate the derivative (and, possibly output Number_Of_Loops, but only as a debugging statement).
- Your estimate of the derivative should call your existing F function; something like
FPrime = ( F(X+DeltaX) - F(X) ) / DeltaX;
although, since F(X) is used repeatedly, you could store this value (as indeed you do).
main()
- This should perform most of the output, based on the results of calls to other functions, not using global variables.
- My last reply was written between you posting two versions of code, so I was completely taken aback by you suddenly switching to pointers. The use of
F_X(&F_of_x, &FxDeltax, &FPrime);
in particular, caused me to wonder why you had suddenly decided to do this. The least frustrating request would be to ask you to consider @Ganado's illustration of simple function calls.
Other comments
- Good to comment code, but try not to overdo it. An appropriately-named variable is often sufficient without further comment.
- There is no need at all for pointers here.
- There is no need for global variables (other than probably C1, C2, C3).
I'm afraid that any code samples I would produce here would end up simply doing your assignment for you. Past experience suggests that an innocent piece of unrelated code to illustrate an approach often elicits a "don't understand what that has to do with my problem" response.