Thread: Creating this formula

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    20

    Creating this formula

    So I need to create this calculation in a program. Yes this is HW but I am having the utter most difficult time with this. I just need some help understanding why I can't get this to work.

    Now honestly, I am very terrible with math and when I saw this equation, be honest, don't know how to even begin solving.

    Any help would be much appreciated. This is the code I have thus far. The formula I need to create is below.

    Note: variable a,b,c require user input.

    Creating this formula-capture-png

    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
        float a, b, c; 
        float neper, powNeper, solution;
        float e = 2.71828, pi = 3.1415926;
    
        printf("\nThis program will calculate 1/2*sqrt(pi/a)*neper number to the (b^2-4ac)/4a power");
        printf("\nwhere variables a-c are inputed by you.");
        printf("\nNote: Pi=3.1415926 (approximately), Neper Number=2.71828 (approximately)");
    
        
        printf("\n\na = ");
        scanf("%f", &a);
        fflush(stdin);
        printf("\nb = ");
        scanf("%f", &b); 
        fflush(stdin);
        printf("\nc = ");
        scanf("%f", &c);
    
        b = pow(b, 2);
        powNeper = (b - (4 * a*c)) / (4 * a);
        neper = pow(e, powNeper);
        
        solution = (1 / 2) * (sqrt(pi / a)) * neper;
    
        printf("/n%.5f", solution);
    
        getch();
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    12
    Code:
     #include <stdio.h>
    #include <math.h>
    
     
    main()
    {
        float a, b, c; 
        float neper, powNeper, solution;
        float e = 2.71828, pi = 3.1415926;
     
       /* printf("\nThis program will calculate 1/2*sqrt(pi/a)*neper number to the (b^2-4ac)/4a power");
        printf("\nwhere variables a-c are inputed by you.");
        printf("\nNote: Pi=3.1415926 (approximately), Neper Number=2.71828 (approximately)");
     */
         
        printf("\n\na = ");
        scanf("%f", &a);
        
        printf("\nb = ");
        scanf("%f", &b); 
       
        printf("\nc = ");
        scanf("%f", &c);
         
    neper =( b * b - 4 * a * c ) / (4*a);
    powNeper = pow(e,neper);
     
    
    solution = sqrt(pi /(float)a) * 0.5 * powNeper; 
     
    printf("\n%f",solution);
     
        return 0;
    }
    i changed a little bit, i deleted the fflush that i do not know. The code is working i think,try this one. I think the reason is that in your code 1/2 produces zero. instead (float)1/2 -->produces without any loss.
    Last edited by muhtar; 06-08-2015 at 10:44 PM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you better of using doubles - you get noting here by using floats except increasing the calculation errors
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    20
    Thanks for the solutions guys. The (float)1/2 helped actually start showing numbers. I was wondering though. As complex as it may be in terms of setting up, could someone show me how I would be able to write the full equation just in one line. If you notice my original code, I went ahead and determined what the exponent was for 'e' before writing up the final calculation.

    Code:
    b = pow(b, 2);
        powNeper = (b - (4 * a*c)) / (4 * a);
        neper = pow(e, powNeper);
    Instead of adding all this to the source code, how could I write it out that i could just include it in the 'solution' calculation?

    Also, I have to show the solution in scientific notation which i figured out is %e, but say i use the values: a=10, b=15, and c=20, i get the right answer (1.60e-007) but is there a way to format that even further to eliminate the '0s' in the -007?

    Much appreciated again for the help.
    Last edited by raven2313; 06-09-2015 at 07:22 AM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by raven2313 View Post
    Also, I have to show the solution in scientific notation which i figured out is %e, but say i use the values: a=10, b=15, and c=20, i get the right answer (1.60e-007) but is there a way to format that even further to eliminate the '0s' in the -007?
    According to one source I found regarding the %e format specifier in printf:
    The exponent always contains at least two digits; if the value is zero, the exponent is 00. In Windows, the exponent contains three digits by default, e.g. 1.5e002, but this can be altered by Microsoft-specific _set_output_format function.
    Which seems to be echoed in another post here.

    So, assuming you are indeed on a Windows system maybe you could use the suggestion. Otherwise it looks like you can't do it... except maybe by printing it to a string and then finding/deleting those extra 0's and then printing the modified string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with a formula!!
    By brigas in forum C Programming
    Replies: 4
    Last Post: 03-15-2012, 01:49 PM
  2. need help on formula?
    By Radox in forum C Programming
    Replies: 3
    Last Post: 03-04-2011, 09:14 PM
  3. Anyone know how to use this formula?
    By gator6688 in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2007, 10:42 PM
  4. Formula for Pi
    By zowen in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 05:17 AM
  5. formula
    By pete in forum C++ Programming
    Replies: 7
    Last Post: 05-26-2003, 08:32 PM