Homework, just advise please
Apparently my idea of what returning something in a method does is wrong. I’m trying to write methods for getting derivatives of numbers and operations. So far I just wanted to get the derivative of a non-negative number that isn’t accompanied with an “x” (result should be zero no matter what value is given).
The code is pretty long and needs to be cleaned up so I’ll just include the method, the call, and what I’m getting.
Method:
int noXDerivative(int tempInt, string tempX)
{
if (tempX == "")
{
tempInt = 0;
}
return tempInt;
}
void getDerivatives(int tempInt, string tempX, int tempInt2, string tempX2, char tempOperator)
{
noXDerivative(tempInt, tempX);
noXDerivative(tempInt2, tempX2);
}
The call:
getDerivatives(tempNumInt, tempNumX, tempNum2Int, tempNum2X, expression[iterator]);
I also called the “noXDerivative” directly to see what would happen but the result didn’t change. The test I’m running right now is “5 + 6” (tempNumInt is 5 and tempNum2Int is 6). I keep getting 11 when I need to get 0 (again, there are no “x”s). I was hoping that if I put tempNumInt as one of the parameters it would be changed from 5 to 0 within the noXDerivative method but it doesn’t. How do I correct this?
I have to say that I don’t understand what it is that you’re trying to do.
Nevertheless, to achieve your goal of modifying
tempIntinsidegetDerivatives(), you can either do:Or, you can modify the
noXDerivative()function to take its argument by reference, rather than by value: