Thread: Newbie - cubic polynomial program - help!

  1. #1
    Registered User
    Join Date
    Mar 2006

    Exclamation Newbie - cubic polynomial program - help!

    Hi guys!

    Ok first of all, I am completely new to c programming & need a bit of expert help with a program I am writing. I am required to write a c program to find the roots of a cubic polynomial. I just want to add at this point that I am NOT looking for someone to write the program for me, I'm just after a little assistance. In fact, I have written what I thought to be right, but it doesn't seem to work properly. The program compiles fine, but the problem is the way it works! There are supposed to be 3 different conditions, but when I execute the program (& input coefficients), I only get the condition where there is 'one real root & two imaginary roots'. The roots are not correct either, they have letters in the answer etc. Anyway, if anyone could have a look at my code & offer me any help as to where I am going wrong, I would be most grateful!

    Thanks in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    #include <stdio.h>
    #include <math.h>
    #define PI 3.1415926536
    void main(void)
    /*declare variables*/
    char rpt;
    float p,q,r;
    float root;
    float a,b;
    float A,B;
    float x1,x2,x3;
    float a1,b1;
    float real,imag;
    float angle;
    printf("Cubic Polynomial Solver (form y*y*y + p*y*y + q*y + r)\n");
    while(1) { /*keep looping until 'break'*/
    rpt = 'T';
    printf("Enter coefficients:\n");
    printf("p = ");
    printf("q = ");
    printf("r = ");
    a=(1.0/3.0)*(3.0*q - p*p);
    b=(1.0/27.0)*(2.0*p*p*p - 9.0*p*q + 27.0*r);
    if(root>0.0) {
    printf("There is one real root of %f and two conjugate imaginary roots of %f + i*%f and %f - i*%f\n",
    else if(root<0.0) {
    x2=2.0*sqrt(-a/3.0)*cos((angle/3.0) + 2.0*PI/3.0);
    x3=2.0*sqrt(-a/3.0)*cos((angle/3.0) + 4.0*PI/3.0);
    printf("Real distinct roots are: %f, %f and %f\n",x1-(p/3.0),x2-(p/3.0),x3-(p/3.0));
    else { /*root=0*/
    printf("There is a real distinct root of %f and two equal real roots of %f\n",x1-(p/3.0),x2-(p/3.0));
    /*check for repeat*/
    printf("\nIf you wish to calculate the roots of another cubic polynomial press: Y\n");
    scanf(" %c",&rpt);
    if (rpt !='Y') break;
    } /*end while*/
    } /*end main*/
    > void main(void)
    main returns an int.

    I don't know whether the cubic root algorithm is correct, but you should try doing things like
    a=(1.0/3.0)*(3.0*q - p*p);
    printf("Computed a is %f\n", a );
    b=(1.0/27.0)*(2.0*p*p*p - 9.0*p*q + 27.0*r);
    printf("Computed b is %f\n", b );

    And compare these results with your own worked out on paper for example.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Is it just me, or is the polynomial program popular?
    By CaptainMorgan in forum A Brief History of
    Replies: 6
    Last Post: 10-05-2006, 08:10 PM
  4. newbie : compiling a C++ program in linux
    By gemini_shooter in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2005, 02:45 PM
  5. Polynomial
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 05:00 PM