Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. function doesn't reply

function doesn't reply

Scheduled Pinned Locked Moved C / C++ / MFC
help
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hasani2007
    wrote on last edited by
    #1

    Hello all; I write a simple program to calculate quadratic equation with function in c. /*purpose: calculate the roots of a quadratic equation supposing b^2>4*a*c */ #include <stdio.h> #include <math.h> float solver(float a,float b, float c){ float delta,x1,x2; delta=(b*b)-4*a*c; x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; return(x1,x2); } void main(){ float a,b,c,d; printf("a = "); scanf("%f", &a); printf("b = "); scanf("%f", &b); printf("c = "); scanf("%f", &c); printf("%f\n%f",solver(a,b,c)); } the result for a=1,b=2,a=1 is -1.00000 0.00000 and for any other numbers the second result is 0.00000 Where is the problem. Thanks anyone help me.

    C D S L 4 Replies Last reply
    0
    • H hasani2007

      Hello all; I write a simple program to calculate quadratic equation with function in c. /*purpose: calculate the roots of a quadratic equation supposing b^2>4*a*c */ #include <stdio.h> #include <math.h> float solver(float a,float b, float c){ float delta,x1,x2; delta=(b*b)-4*a*c; x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; return(x1,x2); } void main(){ float a,b,c,d; printf("a = "); scanf("%f", &a); printf("b = "); scanf("%f", &b); printf("c = "); scanf("%f", &c); printf("%f\n%f",solver(a,b,c)); } the result for a=1,b=2,a=1 is -1.00000 0.00000 and for any other numbers the second result is 0.00000 Where is the problem. Thanks anyone help me.

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      hasani2007 wrote:

      printf("%f\n%f",solver(a,b,c));

      -here you try to print 2 float values but feed printf only with one.

      hasani2007 wrote:

      return(x1,x2);

      -this is not the way to return 2 values from a function (i guess this is what you are tryin to do), use output parameters, a struct, global variables, ..., e.g like:

      void solver(float a,float b, float c, float &outX1, float &outX2){
      ...
      outX1 = x1;
      outX2 = x2;
      }

      ...
      float x1, x2;
      solver(a,b,c,x1,x2)
      printf("%f\n%f",x1,x2);
      ...

      or

      struct result
      {
      float x1;
      float x2;
      };

      result solver(float a,float b, float c){
      ...
      result res;
      res.x1 = x1;
      res.x2 = x2;
      return res;
      }

      ...
      result res = solver(a,b,c);
      printf("%f\n%f",res.x1,res.x2);
      ...

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

      1 Reply Last reply
      0
      • H hasani2007

        Hello all; I write a simple program to calculate quadratic equation with function in c. /*purpose: calculate the roots of a quadratic equation supposing b^2>4*a*c */ #include <stdio.h> #include <math.h> float solver(float a,float b, float c){ float delta,x1,x2; delta=(b*b)-4*a*c; x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; return(x1,x2); } void main(){ float a,b,c,d; printf("a = "); scanf("%f", &a); printf("b = "); scanf("%f", &b); printf("c = "); scanf("%f", &c); printf("%f\n%f",solver(a,b,c)); } the result for a=1,b=2,a=1 is -1.00000 0.00000 and for any other numbers the second result is 0.00000 Where is the problem. Thanks anyone help me.

        D Offline
        D Offline
        Dr Walt Fair PE
        wrote on last edited by
        #3

        I'm not an expert in the details of C/C++, but the solver function definition shows a float should be returned and it appears you're trying to return a tuple.

        CQ de W5ALT

        Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

        1 Reply Last reply
        0
        • H hasani2007

          Hello all; I write a simple program to calculate quadratic equation with function in c. /*purpose: calculate the roots of a quadratic equation supposing b^2>4*a*c */ #include <stdio.h> #include <math.h> float solver(float a,float b, float c){ float delta,x1,x2; delta=(b*b)-4*a*c; x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; return(x1,x2); } void main(){ float a,b,c,d; printf("a = "); scanf("%f", &a); printf("b = "); scanf("%f", &b); printf("c = "); scanf("%f", &c); printf("%f\n%f",solver(a,b,c)); } the result for a=1,b=2,a=1 is -1.00000 0.00000 and for any other numbers the second result is 0.00000 Where is the problem. Thanks anyone help me.

          S Offline
          S Offline
          Software_Developer
          wrote on last edited by
          #4

          Your program to calculate quadratic equations with . Input MUST have the format: AX2 + BX + C = 0 EXAMPLE: input the equation 2X2 + 4X -30 = 0 as: A= 2 B= 4 C= -30 The answers should be 3 and -5. x1=3 x2=-5

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

          float x1,x2;

          bool solver(float a,float b, float c)
          {
          float delta = sqrt( (b*b) - (4*a*c) );

          if (!a)  return false; 
          
          x1 = ( -b + delta)/(2\*a);
          x2 = ( -b - delta)/(2\*a);
          
          return true;
          

          }

          int main()
          {
          float a=2,
          b=4,
          c=-30;

          /*
          printf("a = ");
          scanf("%f", &a);
          printf("b = ");
          scanf("%f", &b);
          printf("c = ");
          scanf("%f", &c);

          */

          if ( solver(a, b ,c) ) printf("x1=%f\nx2=%f\n",x1,x2);

          return 0;
          }

          ..

          U 1 Reply Last reply
          0
          • H hasani2007

            Hello all; I write a simple program to calculate quadratic equation with function in c. /*purpose: calculate the roots of a quadratic equation supposing b^2>4*a*c */ #include <stdio.h> #include <math.h> float solver(float a,float b, float c){ float delta,x1,x2; delta=(b*b)-4*a*c; x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; return(x1,x2); } void main(){ float a,b,c,d; printf("a = "); scanf("%f", &a); printf("b = "); scanf("%f", &b); printf("c = "); scanf("%f", &c); printf("%f\n%f",solver(a,b,c)); } the result for a=1,b=2,a=1 is -1.00000 0.00000 and for any other numbers the second result is 0.00000 Where is the problem. Thanks anyone help me.

            L Offline
            L Offline
            loyal ginger
            wrote on last edited by
            #5

            I want to add a comment to Code-o-mat's reply. The value of the expression (x1,x2) is the value of x2. You were trying to return two float values, however, you did not pay attention to the "," operator, and thought that (x1,x2) represents the two value you wanted to return. The compiler actually returned just the value of x2. Please read the documentation of C/C++ for the operator ",".

            1 Reply Last reply
            0
            • S Software_Developer

              Your program to calculate quadratic equations with . Input MUST have the format: AX2 + BX + C = 0 EXAMPLE: input the equation 2X2 + 4X -30 = 0 as: A= 2 B= 4 C= -30 The answers should be 3 and -5. x1=3 x2=-5

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

              float x1,x2;

              bool solver(float a,float b, float c)
              {
              float delta = sqrt( (b*b) - (4*a*c) );

              if (!a)  return false; 
              
              x1 = ( -b + delta)/(2\*a);
              x2 = ( -b - delta)/(2\*a);
              
              return true;
              

              }

              int main()
              {
              float a=2,
              b=4,
              c=-30;

              /*
              printf("a = ");
              scanf("%f", &a);
              printf("b = ");
              scanf("%f", &b);
              printf("c = ");
              scanf("%f", &c);

              */

              if ( solver(a, b ,c) ) printf("x1=%f\nx2=%f\n",x1,x2);

              return 0;
              }

              ..

              U Offline
              U Offline
              User 3919723
              wrote on last edited by
              #6

              Please, don't give such a bad solution!!! I've adjusted it to avoid global variables:

              #include #include bool solver(float a,float b, float c, float &x1, float &x2)
              {
              float delta = sqrt( (b*b) - (4*a*c) );

              if (!a)  return false; 
              
              x1 = ( -b + delta)/(2\*a);
              x2 = ( -b - delta)/(2\*a);
              
              return true;
              

              }

              int main()
              {
              float a=2,
              b=4,
              c=-30;

              /*
              printf("a = ");
              scanf("%f", &a);
              printf("b = ");
              scanf("%f", &b);
              printf("c = ");
              scanf("%f", &c);

              */

              float x1,x2;
              if ( solver(a, b ,c, x1, x2) ) printf("x1=%f\nx2=%f\n",x1,x2);

              return 0;
              }

              S 1 Reply Last reply
              0
              • U User 3919723

                Please, don't give such a bad solution!!! I've adjusted it to avoid global variables:

                #include #include bool solver(float a,float b, float c, float &x1, float &x2)
                {
                float delta = sqrt( (b*b) - (4*a*c) );

                if (!a)  return false; 
                
                x1 = ( -b + delta)/(2\*a);
                x2 = ( -b - delta)/(2\*a);
                
                return true;
                

                }

                int main()
                {
                float a=2,
                b=4,
                c=-30;

                /*
                printf("a = ");
                scanf("%f", &a);
                printf("b = ");
                scanf("%f", &b);
                printf("c = ");
                scanf("%f", &c);

                */

                float x1,x2;
                if ( solver(a, b ,c, x1, x2) ) printf("x1=%f\nx2=%f\n",x1,x2);

                return 0;
                }

                S Offline
                S Offline
                Software_Developer
                wrote on last edited by
                #7

                :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups