Thread: Need help with this formula -b/2a..Please read.

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    Need help with this formula -b/2a..Please read.

    I am a newbie here so please be kind. I am writing a program that will tell the user the vertex of a parabola. The formula for a parabola is y=ax^2+bx+c. To find the vertex of the parabola you use the formula -b/2a. So here is my problem, when the user enters 3 coefficients (a,b,c) such as 2 2 2 and you plug a and b into the formula you get zero but what I'm suppose to get is -1/2 or -.5. For example, plug in the numbers -(2)/2*2 the program spits out zero. The rule is 1 divided 2 is zero but when you use a calculator of course the answer is .5. How can I get around this. Please help, this is due tomorrow night.

  2. #2
    TheNymph
    Guest
    Try using floats instead of integers.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    3
    I tried using floats but didn't work. Here is a small part of the program. Hope you can help.

    #include<stdio.h>
    #include<math.h>


    // Main

    void main(void)
    {
    int a, b, c;
    double x;
    double y;
    double y_intercept;
    //double x;
    //double y;
    char ans;


    puts("This project by James Volkman, analyzes");
    puts("Equations of the form y=ax^2+bx+c.");
    puts("The user will be asked to enter the coefficients a,b, and c.");
    printf("Would you like to continue? ");
    scanf(" %c", &ans);

    while (ans == 'y' || ans == 'Y')
    {
    printf("\nEnter the coefficients a,b, and c of y=ax^2+bx+c: ");
    scanf("%i%i%i", &a, &b, &c);

    if (a > 0)
    {
    printf("The equation you entered is y=%ix^2+%ix+%i\n", a, b, c);
    printf("It represents a parabola opening upward\n");
    Here is the problem> x = (-b) / (2*a);
    y = a*(x*x) + b*x +c;
    printf("Vertex: (%g,%g)\n", x, y);
    y_intercept = c;
    printf("Y intercept (0,%g)\n", y_intercept);
    printf("Would you like to continue? ");
    }

    else if (a < 0)
    {
    printf("The equation you entered is y=%ix^2+(%i)x+(%i)\n", a, b, c);
    printf("It represents a parabola opening downward\n");
    x = -(b)/(2*a);
    y = a*(x*x) + b*x +c;
    printf("Vertex: (%g,%g)\n", x, y);
    y_intercept = c;
    printf("Y intercept (0,%g)\n", y_intercept);
    printf("Would you like to continue? ");
    }

    else
    {
    printf("The equation you entered is y=%ix^2+%ix+%i\n", a, b, c);
    printf("It represents a line\n");
    printf("Would you like to continue? ");

    }


    scanf(" %c", &ans);
    }

    return;
    }

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    > void main(void)
    should be int main(void)
    add return 0; to the end of main

    > scanf("%i%i%i", &a, &b, &c);
    To get input with doubles use %g or %f (float).

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    You are trying to mix data types. Try casting
    Hint: x = (-(double)b) / (2.0 * (double)a);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM