【问题标题】:Return values from one function work properly, returns from the second do not reflect the correct value一个函数的返回值正常工作,第二个函数的返回值不反映正确的值
【发布时间】:2019-10-29 22:06:28
【问题描述】:

基本上我有两个功能。函数1,calculateGamma返回angleY。这是我的函数原型:

double calculateGamma(int l1, int l2, int l3) {
    double eqn1Numer = (pow(l1, 2) + pow(l2, 2) - pow(l3, 2)); 
    double eqn1Denom = 2 * l1 * l2;
    double angleY = acos(eqn1Numer / eqn1Denom);
    return angleY;
}

我用这个语句调用函数:

angleYres = calculateGamma(l1, l2, l3);

我的第二个函数 radiusMethod1 使用了 calculateGamma 的返回值。 (我已经检查过,angleYres的实际值是正确的,我可以在main中打印出来。)

这是我的第二个功能:

double radiusMethod1(int angleYres, int l1, int l2, int l3) {
    printf("Angle Y: %lf\n", angleYres);
    printf("%d %d %d", l1, l2, l3);```
}

如果我在 main 中打印 angleY,则值为 1.44568。如果我在函数 radiusMethod1 中打印 angleYres(我在函数 main 中定义的 angleY),它将返回 0.0000。这让我很困惑,因为 l1、l2 和 l3 都打印到正确的输出:200、250、300,这是我从另一个函数中获得的三个值。

我的问题是:如何从 calculateGamma 获取返回值 angleY 以用于函数 radiusMethod2?

我尝试使用与 l1、l2、l3 相同的方法重新分配函数,但它的工作方式似乎不同;这让我感到困惑,因为我认为我使用的是相同的方法。

这是我对 cme​​ts/prints 等进行故障排除的原始代码

#include <stdio.h>
#include <math.h>

int readLength(void) {
    int length;
    printf("Please enter a side (largest value last): ");
    scanf("%d", &length);
    return length;
}

double calculateGamma(int l1, int l2, int l3) {
    double eqn1Numer = (pow(l1, 2) + pow(l2, 2) - pow(l3, 2)); // Equation 1 calculation
    double eqn1Denom = 2 * l1 * l2;
    double angleY = acos(eqn1Numer / eqn1Denom);
    return angleY;
}

double radiusMethod1(int angleYres, int l1, int l2, int l3) {
    printf("Angle Y: %lf\n", angleYres);
    printf("%d %d %d", l1, l2, l3);
}

/*double radiusMethod2(int l1, int l2, int l3) {
    double valS = (.5) * (l1 + l2 + l3); // calculates value under the square root
    double eqn2Numer = (pow(valS * (valS - l1) * (valS - l2) * (valS - l3), 0.5));
    double radiusT_Circ = eqn2Numer / valS; 
    return radiusT_Circ;
}

void printResults(double angleYres, double radiusMethod1, double radiusMethod2) {
    printf("Angle Gamma: %lf", angleYres);
    printf("Method 1: %lf", radiusMethod1);
    printf("Method 2: %lf", radiusMethod2);
}*/

int main() {
    int length;
    int l1, l2, l3, angleY;
    double angleYres, radius1res, radius2res;
    // Gets user lengths and assigns them to l1, l2, l3
    l1 = readLength();
    l2 = readLength();
    l3 = readLength();
    // Calculates angle Y
    angleYres = calculateGamma(l1, l2, l3);
    printf("Angle Y: %lf\n", angleYres);
    // Calculates radius using method 1
    radius1res = radiusMethod1(angleYres, l1, l2, l3);
    printf("\n%lf", radius1res);

我希望 angleY 在函数 radiusMethod1 中输出到 ~1.4,但是它在函数中输出到 0.0000,在 main 中输出到 -nan(ind)。

【问题讨论】:

  • 打开编译器警告(-Wall -Wextra,如果使用 gcc 或 clang)并注意它们。
  • radiusMethod1 没有返回语句...
  • double radiusMethod1(int angleYres... --> double radiusMethod1(double angleYres... ?
  • @4386427 我正在尝试使用calculateGamma 的return 语句在radiusMethod1 内打印angleY,因为我需要使用angleY 来计算radiusMethod1。
  • 天啊..我太傻了。当然我没有使用相同的类型...谢谢。对不起大家。

标签: c function


【解决方案1】:

double radiusMethod1(int angleYres, int l1, int l2, int l3)angleYres 的声明中应该是double,而不是int。

在主函数中将 angleY 声明为 int 也会出现同样的问题。这里也应该是双倍的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多