【问题标题】:Basic Function Producing Infinite Loop产生无限循环的基本函数
【发布时间】:2015-12-25 00:11:58
【问题描述】:

这是我的代码:

#include <stdio.h>
#include <math.h>
int main(void) 
{
    double x, y, z;
    double numerator;
    double denominator;
    printf("This program will solve (x^2+y^2)/(x/y)^3\n");
    printf("Enter the value for x:\n");
    scanf("%lf", x);
    printf("Enter the value for y:\n");
    scanf("%lf", y);
    numerator = sqrt(x) + sqrt(y);
    denominator = pow((x/y),3);
    z = (numerator/denominator);
    printf("The solution is: %f\n", z);
    return(0);

}

谁能给我一个(希望是)快速指针来修复我的无限循环?

【问题讨论】:

  • 你应该通过scanf("%lf", &amp;x);读入变量。 scanf 将修改程序中变量的值,因此您始终需要在函数中传递对变量的引用。
  • sqrt(x) 返回平方根,而不是平方。使用pow(x,2)。
  • 代码中没有无限循环。如果您在处理 输入 时遇到问题,请说...

标签: c loops infinite


【解决方案1】:

您的函数中没有循环,所以我认为是您对scanf() 的调用导致了错误:

您需要传递对scanf() 的引用,即使用scanf("%lf",&amp;x) 而不是scanf("%lf",x)。

顺便说一句,根据您的函数定义,您应该使用pow(x,2) 而不是返回平方根的sqrt(x)。

【讨论】:

    【解决方案2】:

    因为这是你的第一个问题

    **Welcome to stack overflow**
    

    您的代码没有进入无限循环,存在运行时错误。 您的 scanf 代码有缺陷,请使用:

    scanf("%lf",&x); 
    scanf("%lf",&y);
    

    你希望scanf修改你的值的地址字段中包含的值。请阅读教程。

    也用

    numerator=pow(x,2) + pow(y,2);//numerator=x^2+y^2
    

    【讨论】:

      【解决方案3】:

      这不是无限循环,您的代码只是返回无穷大。那是因为 scanf() 需要一个指向变量的指针,它应该把读取的数字放在哪里。要获取变量的地址,您可以像这样使用&amp; 运算符:

      scanf("%lf", &x);
      scanf("%lf", &y);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-26
        • 1970-01-01
        • 2012-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多