Thread: Im new at this and trying to write a program using functions

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Im new at this and trying to write a program using functions

    I appreciate any help with this program

    I really was trying to figure it out myself, but I am running out of time


    Find the roots of the equation of the form ax2 +bx+ c =0. Ask the user to input a, b and c. Calculate the discriminant = b2 – 4ac.
    a. If the discriminant is less than zero, display the message “The roots are imaginary”.
    b. Otherwise, calculate the roots and display them as follows:
    i. If the roots are the same, display “The roots are the same” and display the single calculated root.
    ii. If they are different, display “The roots are different” and display the calculated roots.
    Use the squareRoot function in chapter 8.

    The following is the square root function from chapter 8

    Code:
    ]#include <stdio.h>
    float absoluteValue (float x) 
    {
          if (x < 0)
              x = -x;
          return (x);
          }
    
    float squareRoot (float x)
    {
          const float epsilon = .00001;
          float       guess   = 1.0;
    
          while ( absoluteValue (guess * guess - x) >= epsilon )
    
    
                guess = ( x / guess + guess ) / 2.0;
    
          return guess;
    }
    int main (void)
    {
        printf (" squareroot (2.0) = %f\n", squareRoot (2.0));
        printf (" squareroot (144.0) = %f\n", squareRoot (144.0));
        printf (" squareroot (2.0) = %f\n", squareRoot (144.0));
    
        getch();
    }
    Ok I incorporated the square root function from above into my program and wrote the following:


    Code:
    #include <stdio.h>
    float absoluteValue (float x) 
    {
          if (x < 0)
              x = -x;
          return (x);
          }
    
    float squareRoot (float x)
    {
          const float epsilon = .00001;
          float       guess   = 1.0;
    
          while ( absoluteValue (guess * guess - x) >= epsilon )
    
    
                guess = ( x / guess + guess ) / 2.0;
    
          return guess;
    }
    int main (void)
    {
        int a,b,c,y,z,Z;
    
            printf ("We will find the roots of the equation of the form ax^2 +bx+ c =0\n");
            printf ("First enter a value for a\n");
    
            scanf ("%i", &a);
    
            printf ("Next enter a value for b\n");
    
            scanf ("%i", &b);
    
            printf ("Next enter a value for c\n");
    
            scanf ("%i", &c);
    
            y = (-b + (squareRoot(b*b*-4*a*c)))/2*a;
    
            z = (-b - (squareRoot(b*b*-4*a*c)))/2*a;
    
            Z = b*b*(-4*a*c);
    
              if (Z < 0)
              printf (" The roots are imaginary\n");
              else if ( y==z)
                   printf ("The roots are the same and are equal to %i\n ", y);
              else if ( y != z )
                   printf ("The roots are different and are equal to %i and %i\n ", y,z);
    
    
               getch();
            }
    I am able to compile the program with out errors but it does not run correctly:

    I am pretty sure it has to do with this part of the program
    y = (-b + (squareRoot(b*b*-4*a*c)))/2*a;

    z = (-b - (squareRoot(b*b*-4*a*c)))/2*a;

    Z = b*b*(-4*a*c);

    I do not think I can use the Square root function like this, but then I am stuck on how to use this function to get my program to operate correctly. I Appreciate any help with this program. Thanks very, very much, Bill

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The problem is in the "b*b*-4*a*c" expression. There is an extra * in there, which one is it?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    i suggest you parenthesize every term in your expressions so you don't depend on the language rules for association. that will help you find the type of problem brewbuck noted. a lot of professional programmers do this even though they know the rules, because its easy to get caught by accident.

    for example b*b*-4*a*c turns into b * b * (-4) * a * c which compiles but computes incorrectly but if you used parens you would get a compiler error (b * b *) - (4 * a * c) which would tell you where you went wrong
    Last edited by dmh2000; 03-21-2012 at 09:59 AM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A few more things:

    • You should check the discriminant before you go trying to calculate it's square root, since if it's negative, it has no real square root. That is the order the instructions told you to do it in anyhow. Things like this can cause problems with square root functions, since negative numbers have imaginary square roots.
    • You should not have variables/functions whose names only differ in upper/lower case. Z is a horrible name for the discriminant anyhow, call it discriminant. You ought to use better names than y and z, since those often refer to axes in a graph. How about root1 and root2?
    • Use this value when you calculate your two roots, instead of repeating the calculation. It's easier to read, less error prone (only one place to screw up the calculation) and easier to fix (only one place you have to fix it). Plus it's more efficient.


    An example:
    Code:
    float discriminant, root1, root2;
    ...
    discriminant = (b * b) - (4 * a * c);
    if (discriminant < 0)
        print "imaginary"
    else
        root1 = (-b + squareRoot(discriminant)) / (2 * a);
        root1 = (-b - squareRoot(discriminant)) / (2 * a);
        ...
    Note that I put the 2*a in parentheses. Without it, it divides by two then multiplies that quotient by a, whereas you want it to divide by the product 2*a.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you write a functions declaration?
    By scarlet00014 in forum C Programming
    Replies: 2
    Last Post: 10-26-2008, 11:02 AM
  2. write functions with pointers variables
    By noob programmer in forum C Programming
    Replies: 5
    Last Post: 10-25-2008, 05:48 AM
  3. How could i write functions for these?
    By Matus in forum C Programming
    Replies: 3
    Last Post: 02-29-2008, 03:31 AM
  4. Replies: 7
    Last Post: 05-31-2007, 10:14 AM
  5. functions to write a file
    By subbuy2k in forum C++ Programming
    Replies: 1
    Last Post: 12-27-2001, 10:59 AM