Like Tree1Likes
  • 1 Post By Salem

Can't Write this program

This is a discussion on Can't Write this program within the C Programming forums, part of the General Programming Boards category; I am supposed to write a program in C using an algorithm. It is supposed to solve the quadratic equation ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    27

    Can't Write this program

    I am supposed to write a program in C using an algorithm. It is supposed to solve the quadratic equation when getting a,b,c. However, when I put in a =1, b=4, c=4, the program doesn't work. In other words, the last two else's of the program don't work.
    Code:
    
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    #include <math.h>
    
    
    main()
    {
        double a,b,c,d,x,x1,x2;
        printf("Enter the value for a: ");
        a=GetReal();
        printf("Enter the value for b: ");
        b=GetReal();
        printf("Enter the value for c: ");
        c=GetReal();
        if (a==0)
        {
            if (b==0)
            {
                printf("No solution");
            }
            else
            {
                x=(-c/b);
                printf("The equation is not quadratic and the solution is:%.2f",x);
            }
        }
        else 
        {
            d=((b*b)-(4*a*c));
                if (d<0)
                {
                    printf("There is no real solution.");
                }
                else 
                {
                    if (d>0) 
                    {
                        x1=(-b+(sqrt(d)))/(2*a);
                        x2=(-b-(sqrt(d)))/(2*a);
                        printf("There are 2 solutions. The first is %.2f and the second is %.2f",x1,x2);
                    }
                    else
                    {
                         if (d=0)
                        {
                            x1=(-b/(2*a));
                            printf("There is one solution which is %.2f");
                        }
                    }
                }
                
        }
    
    
        getchar();
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > if (d=0)
    Try it with
    d == 0

    Or better yet, just remove the condition altogether, since it's the only condition left after the <0 and >0 tests.

    > printf("There is one solution which is %.2f");
    Where is the parameter to print with %f ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    Quote Originally Posted by Salem View Post
    > if (d=0)
    Try it with
    d == 0

    Or better yet, just remove the condition altogether, since it's the only condition left after the <0 and >0 tests.

    > printf("There is one solution which is %.2f");
    Where is the parameter to print with %f ?
    I changed it to == 0 and when i put a=1, b=4, c=4, I get the answer as 0 when it should be -2. I'm not sure what is wrong.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Well there were two mistakes, and you said you only fixed one of them.

    Plus, simply describing your change rather than posting the actual code is not really informative.
    Crossfire likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    Salem, your slipping!!!!

    you missed his use of

    Code:
    main()
    and lack of a return

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    333
    Give the user a chance. Print out "ax^2 + bx + c = 0" then ask for a, b and c.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    27
    Quote Originally Posted by Salem View Post
    Well there were two mistakes, and you said you only fixed one of them.

    Plus, simply describing your change rather than posting the actual code is not really informative.
    I have now fixed both of those things and it is working. Thank you very much!!!

    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    #include <math.h>
    
    
    main()
    {
        double a,b,c,d,x,x1,x2;
        printf("Enter the value for a: ");
        a=GetReal();
        printf("Enter the value for b: ");
        b=GetReal();
        printf("Enter the value for c: ");
        c=GetReal();
        if (a==0)
        {
            if (b==0)
            {
                printf("No solution");
            }
            else
            {
                x=(-c/b);
                printf("The equation is not quadratic and the solution is:%.2f",x);
            }
        }
        else 
        {
            d=((b*b)-(4*a*c));
                if (d<0)
                {
                    printf("There is no real solution.");
                }
                else 
                {
                    if (d>0) 
                    {
                        x1=(-b+(sqrt(d)))/(2*a);
                        x2=(-b-(sqrt(d)))/(2*a);
                        printf("There are 2 solutions. The first is %.2f and the second is %.2f",x1,x2);
                    }
                    else
                    {
                         if (d==0)
                        {
                            x1=(-b/(2*a));
                            printf("There is one solution which is %.2f",x1);
                        }
                    }
                }
                
        }
    
    
        getchar();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anyone help me write this program ?
    By Delaram in forum C Programming
    Replies: 2
    Last Post: 05-08-2013, 10:34 AM
  2. Need help to write a program in c
    By cooljack08 in forum C Programming
    Replies: 15
    Last Post: 11-17-2011, 07:13 PM
  3. trying to write a tic, tac, toe program
    By jason007thomas in forum C Programming
    Replies: 5
    Last Post: 09-01-2010, 10:54 AM
  4. How to write a program
    By Tashfique in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2008, 11:28 AM
  5. How to write a program for...
    By babyboy in forum C Programming
    Replies: 20
    Last Post: 02-19-2008, 05:01 AM

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