Thread: Figured Out the Quadratic Equation Solver, Looking to Optimize

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    5

    Figured Out the Quadratic Equation Solver, Looking to Optimize

    I recently posted a thread that had a few things going on with a program I was working on, the code in question was full of useless code and other such issues that I hadn't managed to fully clean out before posting it to look for help with my questions, but I was able to basically figure out how to get everything to work how I was wanting it to.

    I recently posted a thread that had a few things going on with a program I was working on, the code in question was full of useless code and other such issues that I hadn't managed to fully clean out before posting it to look for help with my questions, but I was able to basically figure out how to get everything to work how I was wanting it to.

    The old code that I had posted was something along the lines of:

    Code:
    #include <stdio.h>
    
    #include <math.h> //void single_quad(); void wrong_choice(); void no_letters_a(); void no_letters_b(); void no_letters_c(); void input_a(); void input_b(); void input_c(); void run_calc(); int main(double a, double b, double c)//this bit here was already pointed out, it was part of my attempts to figure out pointers, hadn't cleared it out before posting the old code { double a; double b; double c; char choice = ' '; do { input_a(); getc(stdin); printf("\nIf you would like to input another equation, please hit 'y', otherwise hit 'n':\t"); scanf_s("%c", &choice, 1); if (choice == '%c') { do { wrong_choice(); getc(stdin); } while (choice != 'y', choice != 'n');//here goes the function for when a character other than 'y' or 'n' is input } printf("\n"); }while (choice == 'y'); return 0; } void input_a() { double a; printf("Please enter the variable for 'a':\t"); scanf_s("%lf", &a); if (a == '%lf') { do { no_letters_b(); getc(stdin); } while (a > 2000000000 || a < -2000000000); } input_b(); return a; //printf("%lf\n", a); } void input_b() { double b; printf("Please enter the variable for 'b':\t"); scanf_s("%lf", &b); if (b == '%lf') { do { no_letters_b(); }while (b > 2000000000 || b < -2000000000); } input_c(); return b; } void input_c() { double c; printf("Please enter the variable for 'c':\t"); scanf_s("%lf", &c); if (c == '%lf') { do { no_letters_c(); }while (c > 2000000000 || c < -2000000000); } run_calc();// it's here I keep running into an issue, "too few arguments in function to call" is all I get return c; } void run_calc(double a, double b, double c) { double a_calc = a; double b_calc = b; double c_calc = c; printf("\na = %lf\nb = %lf\nc = %lf\n", &a_calc, &b_calc, &c_calc); double d = -a_calc; double e = -b_calc; double f = -c_calc; | | | |//all these lines are where the equation's code is, took up well over half the post with that alone so I cut it out | | } void wrong_choice() { char choice = ' '; printf("I'm sorry, that selection is not recognized, please input 'y' for yes or 'n' for no."); scanf_s("%c", &choice, 1); printf("\n"); } void no_letters_a() { double a; getc(stdin); printf("\nI'm sorry, you have input an unrecognized value, please input a numeric digit:\t"); scanf_s("%lf", &a); if (a < 2000000000 && a > -2000000000) { input_b(); } else { no_letters_a(); } } void no_letters_b() { double b; printf("\nI'm sorry, you have input an unrecognized value, please input a numeric digit:\t"); scanf_s("%lf", &b); if (b < 2000000000 && b > -2000000000) { input_c(); } else { no_letters_b(); } } void no_letters_c() { double a; double b; double c; printf("\nI'm sorry, you have input an unrecognized value, please input a numeric digit:\t"); scanf_s("%lf", &c); if (c < 2000000000 && c > -2000000000) { run_calc(); } else { no_letters_c(); }
    }


    The new code is below, this time I left in the part that actually runs through the equation, as I'm looking for any tips, tricks, advice, etc for ways I could maybe optimize things... Basically looking to see if there are any ways of doing things easier than I have, and if there are, what those ways would be. Only have like 3 weeks experience doing anything with programming language at all, so trying to build my skills with it as soon as I can, I appreciate any help that might be offered.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void wrong_choice(char* choice);
    void wrong_a(double* a);
    void wrong_b(double* b);
    void wrong_c(double* c);
    void input_a(double* a);
    void input_b(double* b);
    void input_c(double* c);
    void run_calc(double* a, double* b, double* c);
    int main(void)
    {
     double a;
     double b;
     double c;
     char choice = ' ';
     
     do
     {
      input_a(&a);
      input_b(&b);
      input_c(&c);
    
      printf("\na = %lf\nb = %lf\nc = %lf\n", a, b, c);
    
      run_calc(&a, &b, &c);
      getc(stdin);
      printf("\nIf you would like to input another equation, please hit 'y', otherwise hit 'n':\t");
      scanf_s("%c", &choice, 1);
      if (choice != 'y' && choice != 'n')
      {
       wrong_choice(&choice);
      }
      printf("\n");
     }while (choice == 'y');
     
     return 0;
    }
    void input_a(double* a)
    {
     printf("Please enter the variable for 'a':\t");
     scanf_s("%lf", a);
     if (*a > 2000000000 || *a < -2000000000)
     {
      wrong_a(a);
     }
    }
    void input_b(double* b)
    {
     printf("Please enter the variable for 'b':\t");
     scanf_s("%lf", b);
     
     if (*b > 2000000000 || *b < -2000000000)
     {
      wrong_b(b);
     }
    }
    void input_c(double* c)
    {
     printf("Please enter the variable for 'c':\t");
     scanf_s("%lf", c);
     if (*c > 2000000000 || *c < -2000000000)
     {
      wrong_c(c);
     }
    }
    void run_calc(double* a, double* b, double* c)
    {
     double d = -(*a);
     double e = -(*b);
     double f = -(*c);
     if (a > 0 && b > 0 && c > 0)//if statements set up quadratics for different uses of +/- signs, needs to be reviewed to make sure all combinations are used
     {
      printf("\nThe quadratic equation entered is: %.0lfx^2+%.0lfx+%.0lf.\n", a, b, c);
     }
     else
      if (a > 0 && b > 0 && c < 0)
      {
       printf("\nThe quadratic equation entered is: %.0lfx^2+%.0lfx-%.0lf.\n", a, b, f);
      }
      else
       if (a > 0 && b < 0 && c > 0)
       {
        printf("\nThe quadratic equation entered is: %.0lfx^2-%.0lfx+%.0lf.\n", a, e, c);
       }
       else
        if (a < 0 && b > 0 && c > 0)
        {
         printf("\nThe quadratic equation entered is: %.0lfx^2+%.0lfx+%.0lf.\n", a, b, c);
        }
        else
         if (a < 0 && b < 0 && c < 0)
         {
          printf("\nThe quadratic equation entered is: %.0lfx^2+%.0lfx+%.0lf.\n", a, e, f);
         }
         else
          if (a > 0 && b < 0 && c < 0)
          {
           printf("\nThe quadratic equation entered is: %.0lfx^2+%.0lfx+%.0lf.\n", a, e, f);
          }
          else
           if (a > 0 && b > 0 && c < 0)
           {
            printf("\nThe quadratic equation entered is: %.0lfx^2+%.0lfx+%.0lf.\n", a, b, f);
           }
           else
            if (a < 0 && b > 0 && c < 0)
            {
             printf("\nThe quadratic equation entered is: %.0lfx^2+%.0lfx+%.0lf.\n", a, b, f);
            }
    
     double f1 = -(*b);
     double f2 = ((*b) * (*b)) - (4 * (*a) * (*c));
     double f3 = sqrt(f2);
     double f3_5 = sqrt(-f2);
     double f4 = 2 * (*a);
     double f5 = (f1 + f3) / f4;
     double f6 = (f1 - f3) / f4;
     double f7 = -(*c) / (*b);
     double f8 = sqrt((-f2 / f3_5));
     double f9 = -sqrt((-f2 / f3_5));
     if (a == 0 && b == 0)
     {
      printf("\nERROR.  Input not allowed. Variables 'a' and 'b' cannot both be 0.\n\n");
     }
     else if (a == 0)
     {
      printf("\nThe equation given is in the linear format.  The answer is x=%lf.\n", f7);
     }
     else if (f2 < 0)
     {
      printf("\nThe discriminate of the equation given equals %lf.\nTherefore, this is a complex number.\n", f2);
      if (f8 == f9)
      {
       printf("\nThe x-intercept is the complex number %lf+%lfi.\nThe solutions is %lf+%lfi.\n", f1 / f4, f8, f1 / f4, f8);
      }
      else if (f8 != f9)
      {
       printf("\nThe x-intercepts are the complex numbers %lf+%lfi and %lf-%lfi.\nThe solutions are x=%lf+%lfi or x=%lf-%lfi.\n", f1 / f4, f8, f1 / f4, f8, f1 / f4, f8, f1 / f4, f8);
      }
     }
     else
     {
      printf("\nThe discriminate of the equation given equals %lf.\nTherefore, this is not a complex number.\n", f2);
      if (f5 == f6)
      {
       printf("\nThere is only one x-intercept: (%.0lf,0).\nThe solution is x=%lf", f5, f5);
      }
      else
      {
       printf("\nThe x-intercepts are: (%.0lf,0), and (%.0lf,0).\nThe solutions are x=%lf or x=%lf.\n", f5, f6, f5, f6);
      }
     }
    }
    void wrong_choice(char* choice)
    {
     getc(stdin);
     printf("I'm sorry, that selection is not recognized, please input 'y' for yes or 'n' for no:\t");
     scanf_s("%c", choice, 1);
     if (*choice != 'y' && *choice != 'n')
     {
      do
      {
       wrong_choice(choice);
      } while (*choice != 'y' && *choice != 'n');
     }
     printf("\n");
    }
    void wrong_a(double* a)
    {
     getc(stdin);
     printf("\nI'm sorry, you have input an unrecognized value, please input a numeric digit for 'a':\t");
     scanf_s("%lf", a);
     if (*a > 2000000000 || *a < -2000000000)
     {
      wrong_a(a);
     }
     printf("Thank you.\n\n");
    }
    void wrong_b(double* b)
    {
     getc(stdin);
     printf("\nI'm sorry, you have input an unrecognized value, please input a numeric digit for 'b':\t");
     scanf_s("%lf", b);
     if (*b > 2000000000 || *b < -2000000000)
     {
      wrong_b(b);
     }
     printf("Thank you.\n\n");
    }
    void wrong_c(double* c)
    {
     getc(stdin);
     printf("\nI'm sorry, you have input an unrecognized value, please input a numeric digit for 'c':\t");
     scanf_s("%lf", c);
     if (*c > 2000000000 || *c < -2000000000)
     {
      wrong_c(c);
     }
     printf("Thank you.\n\n");
     return c;
    }
    PS. Sorry if the code seems scrunched, still not quite used to posting on here, and the copy/paste wasn't wanting to behave haha...

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Blank lines between functions, and indentation of 2, 4, or 8 spaces.

    be careful about copy, and pasting from an IDE, as some hidden formatting will screw up the Code here. Only pure ASCII text!

    Can't compile and test right now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing what can be done with a quadratic equation solver
    By Deus_Venatus in forum C Programming
    Replies: 4
    Last Post: 10-17-2019, 11:32 AM
  2. quadratic equation
    By Tokalosh in forum C Programming
    Replies: 7
    Last Post: 04-13-2017, 01:13 AM
  3. Extreme beginner quadratic roots solver
    By browser in forum C Programming
    Replies: 3
    Last Post: 11-08-2009, 11:33 AM
  4. quadratic equation
    By nynicue in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 05:57 PM
  5. Quadratic Equation
    By Lucid15 in forum C Programming
    Replies: 20
    Last Post: 09-16-2008, 05:59 PM

Tags for this Thread