无法使输出正常工作? [英] Can't get the output to work?

查看:129
本文介绍了无法使输出正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法使chechForRoots的输出正常工作.当我运行它时,我写了三个数字,并希望看到chechForRoots的输出,但是它只写了等待从键盘上按下一个字符以退出".并关闭程序.

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

 double  calDisk( double  double  double );
 double  checkForRoots( double  double  double );
 double  calRoot1( double  double  double );
 double  calRoot2( double  double  double  double  double );

主要的()
{
   double  a,b,c;
  
  printf(" );
  scanf(" ,& a,& b,& c );

  如果(a ==  0  || b ==  0  || c ==  0 ){
    printf(" );
    exit( 0 );}

  其他 {
     double  discriminant = calDisk(a,b,c);
     double  checkForRoots( double 判别式, double  a , double  b);}
  
  printf(" );
   getch();
   返回  0 ;
}

 double  calDisk( double  a, double  b , double  c){
   double 判别=(b * b- 4  * a * c);
  返回判别式;}

 double  checkForRoots( double 判别式, double  a , double  b)
{如果(判别<  0 )
    printf(" );

    其他 如果(判别==  0 ) {
      printf(" );
             double  root1 = -b/( 2 . 0  * a);
             double  root2 = root1;
            printf(" ,root1);
            printf(" ,root2);}
    
    其他 {
     printf(" );
             double  root1 =(-b + sqrt(discriminant))/( 2 . double  root2 =(-b-sqrt(discriminant))/( 2 ." ,root1);
            printf(" ,root2);}
} 

解决方案

您没有调用checkForRoots-您只是在重新制作原型.


<您的问题不是对某些小学数学的完整理解.您更高级的问题"是不了解数值计算的近似方法,这是一个更为复杂的问题.用数值方法求解的二次方程问题就其性质而言是近似的.

我可以假设您正在分析/求解方程式
ax² + bx + c = 0
您做错了.
首先,对于a ≡ b ≡ c ≡ 0,无法确定根"是完全错误的答案.正确答案将是任何实数都是根".这也是正确的:任何复数都是根".看到不同?您可以轻松查看此语句.

另一种情况是没有真实的根(输出复杂的根会更好)而恰好有两个根.您实际上不应在拥有两个根和只有一个根之间进行区分.从概念上讲,您始终具有两个或零个根,并且两个根具有相同的值.两个根相等的情况集为 measure null .更准确地说,总体位置是两个不同的根源.有关它的一些基础知识,请参见:
https://en.wikipedia.org/wiki/General_position [理想 实数并非如此,因此,仅当您计算时,该解才是正确的两个根,相等"与否.

现在,您用于计算根的公式是正确的,但是从近似计算的最佳准确性的角度来看,它可能被认为是有问题的.我记得在他的
计算机编程的艺术 Donald Knuth 坚持认为,应该选择解决方案的一个根源,一个根源少由于解决方案公式中的+或-号,导致精度损失;并使用Viera的公式找到第二个根:
https://en.wikipedia.org/wiki/Quadratic_equation [ https://en.wikipedia.org/wiki/Vieta%27s_formulas [

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

double calDisk(double, double, double);
double checkForRoots(double, double, double);
double calRoot1(double, double, double);
double calRoot2(double, double, double, double, double);

main()
{
  double a,b,c;
  
  printf("Enter coeficients a, b, c\n");
  scanf("%lf %lf %lf", &a, &b, &c);

  if (a == 0 || b == 0 || c == 0){
    printf("Error: Roots cannot be determined \n");
    exit(0);}

  else{
    double discriminant = calDisk(a, b, c);
    double checkForRoots(double discriminant,double a,double b);}
  
  printf("Waiting for a character to be pressed from the keyboard to exit.\n");
   getch();
   return 0;
}

double calDisk(double a, double b, double c){
  double discriminant = (b * b - 4 * a * c);
  return discriminant;}

double checkForRoots(double discriminant, double a, double b)
{   if (discriminant < 0)
    printf("No roots\n");

    else if (discriminant == 0){
      printf("Roots are real and equal\n");
            double root1 = -b / (2.0 * a);
            double root2 = root1;
            printf("Root1 = %f\n", root1);
            printf("Root2 = %f\n", root2);}
    
    else{
     printf("Roots are real and distinct \n");
            double root1 =(-b + sqrt(discriminant)) / (2.0 * a);
            double root2 =(-b - sqrt(discriminant)) / (2.0 * a);
            printf("Root1 = %f  \n", root1);
            printf("Root2 = %f  \n", root2);}
}

You''re not calling checkForRoots - you''re just prototyping it again.


Your problem is not a complete understanding of some elementary school mathematics. Your more "advanced problem" is not understanding approximate methods of numerical calculations, which is a much more complicated issue. The quadratic equation problem solved numerically is approximate by its nature.

I can assume that you are analyzing/solving the equation
ax² + bx + c = 0
You are doing it wrong.
First of all, for a ≡ b ≡ c ≡ 0, "Roots cannot be determined" it a totally wrong answer. Correct answer will be "Any real number is a root". Also this is true: "Any complex number is a root". See the difference? You can easily check up this statement.

Another cases are when there are no real roots (is would be much nicer to output complex root) and exactly two roots. You really should not make a distinction between having two roots and only one root. Conceptually, you always have two or zero roots, and the two roots have the same value. The set of cases when the two roots are equal is of the measure null. More accurately, two different roots is the case of general position. For some basics of it, please see:
https://en.wikipedia.org/wiki/General_position[^].

It has a special meaning for approximate calculations (remember, those calculations are always approximate; not only the roots, but even a, b and c are, by definition, know with limited accuracy. Look at the quadratic function graph. As it get closer to the position where the extremum of the function value touches the x axis, two roots get close and close. With approximate calculations, you don''t know exactly when it touches. The roots just became "equal" when the difference between them becomes lower than the accuracy of the real number representation (floating-point, in our case). In fact, you can slightly modify one of the coefficient while having the roots "equal" in this sense, which, of course, is not the case for ideal real numbers. So, the solution will only be correct if you calculate both roots, "equal" or not.

Now, your formula for calculation of the roots is correct, but it can be considered as questionable from the point of view of best accuracy of approximate calculation. As I remember, in his The Art of Computer Programming, Donald Knuth insisted that one should choose one of the roots of your solution, the one causing less loss of precision due to + or − sign in the solution formula; and the second root should be found using the Viera''s formula:
https://en.wikipedia.org/wiki/Quadratic_equation[^],
https://en.wikipedia.org/wiki/Vieta%27s_formulas[^].

—SA


这篇关于无法使输出正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆