就像标题所说的一样,如何将我的discr变量从discr函数放入calcRoots函数中?
// Function calculating roots
bool calcRoots(double coeffs[], int coeffLength, double roots[], int rootLength) {
if(disc >= 0) {
roots[rootLength - 1] = ((-(coeffs[coeffLength - 2]) + sqrt(disc)) / (2*(coeffs[coeffLength - coeffLength])));
roots[rootLength - rootLength] = ((-(coeffs[coeffLength - 2]) - sqrt(disc)) / (2*(coeffs[coeffLength - coeffLength])));
return true;
}
else {
return false;
}
}
// Solves the discriminant
double discr(double coeffs[], int coeffLength){
double disc = (pow(coeffs[coeffLength - 2],2)-4*coeffs[coeffLength - coeffLength]*coeffs[coeffLength - 1]);
return disc;
}
最佳答案
您没有提到语言,但我想是C ++。
在calcRoots内部,您可以调用discr并将值获取为这样的变量:
double disc = discr(coeffs[], coeffLength);
然后使用变量中的值存储。希望能有所帮助。
关于c++ - 如何将变量从一个函数转换为另一个函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22672364/