Efficient Coding??

This is a discussion on Efficient Coding?? within the C Programming forums, part of the General Programming Boards category; Ok the question is this.......... Write a function that will calculate and display the real roots of the quadratic equation ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Efficient Coding??

    Ok the question is this..........

    Write a function that will calculate and display the real roots of the quadratic equation
    ax2 + bx+c = 0
    use the function in the program.

    I have coded this
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    int a, b, c, D; 
    double root1, root2;
    
    printf("Enter a,b,c for the quadratic equation\n");
    scanf("%d %d %d",&a,&b,&c);
    
    check(a, b, c, D, root1, root2);
    }
    int check(int a, int b, int c, int D, double root1, double root2)
    {
    if ((a==0) && (b==0)) {
    printf("This equation has no solution");
    }
    
    else if (a==0){
    root1=(-c/a);
    printf("This equation has only one root %lf",root1);
    }
    
    else {
    D = (b*b - 4*a*c);
    if (D>0){
    root1 = (-b + sqrt(D))/(2*a);
    root2 = (-b - sqrt(D))/(2*a);
    printf("The real roots are %lf & %lf",root1,root2);
    }
    else if (D<0){
    printf("There are no real roots");
    }
    else {
    root1 = root2 = (-b/2*a*c);
    printf("The repeated roots are %lf",root1);
    }
    }
    }
    This is working fine, but i want to know that is it efficient & correct answer to the question??
    if not how to make it more efficient.
    Also how can i make it multi function code.......i mean check will be for checking of a, b, c & then it will return to main function & new calculate function will be called where all calculations will be made
    Last edited by getout; 01-01-2007 at 09:32 AM.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    first of all your code need a proper indendation

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int check(int, int, int);
    
    int main(void)
    {
        int a, b, c; 
    
        printf("Enter a,b,c for the quadratic equation\n");
        scanf("%d %d %d",&a,&b,&c);
    
        check(a, b, c);
        
        getchar();
        return 0;
    }
    
    int check(int a, int b, int c)
    {
        int D;
        
        if ((a==0) && (b==0)) 
            printf("This equation has no solution");
        else if (a==0)
            printf("This equation has only one root %lf",(-c/a));  
                                // your code is broken here. Anything divided by 0 is ??? 
        else 
        {
            D = (b*b - 4*a*c);
            if (D>0)
                    printf("The real roots are %lf & %lf",(-b + sqrt(D))/(2*a*c),(-b - sqrt(D))/(2*a*c));
            else if (D<0)
                    printf("There are no real roots");
            else 
                    printf("The repeated roots are %lf",(-b/2*a*c));
        }
    }
    ssharish2005

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You got the divisor wrong. It should be:
    Code:
            if (D>0)
                    printf("The real roots are %lf & %lf",(-b + sqrt(D))/(2*a),(-b - sqrt(D))/(2*a));
    as he had it. But otherwise, nice job.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    and
    Code:
    printf("The repeated roots are %lf",(-b/2*a));
    ssharish2005

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by ssharish2005
    and
    Code:
    printf("The repeated roots are %lf",(-b/2*a));
    ssharish2005
    or
    -b/2.0/a
    or
    -b/(2.0*a)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    True, never looked at that

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 06:22 PM
  2. Efficient Algorithm
    By purple in forum C Programming
    Replies: 10
    Last Post: 02-05-2003, 03:01 PM
  3. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 05:32 PM
  4. Efficient Coding Styles
    By ActionMan in forum C Programming
    Replies: 3
    Last Post: 10-04-2001, 10:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21