# Thread: C programs not working..:-(

1. ## C programs not working..:-(

Im writing a program to find the root of the sixth degree equation f(x) = x^6 - 10x^5 - 38x^4 + 368x^3 + 445x^2 - 2950x -3000

but it keeps giving me a floating exception...:-(
Can nyone help!!!!!!?????

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
float new_guess;
float last_guess;
new_guess=9;
do
{
last_guess=new_guess;
new_guess = last_guess - f(last_guess)/fprime(last_guess);
printf("Guess: %f\n",new_guess);
}while(abs(new_guess - last_guess) > 0.01);
printf("Root of the sixth degree equation is: %f\n",new_guess);
return EXIT_SUCCESS;
}

float f(float x)
{
float y;
y= x*x*x*x*x*x - 10*(x*x*x*x*x) - 38*(x*x*x*x) + 368*(x*x*x) + 445*(x*x) - 2950*x -3000;
return y;
}
float fprime(float x)
{
float z;
z= 6*(x*x*x*x*x) - 50*(x*x*x*x) - 152*(x*x*x) + 1140*(x*x) + 890*x -2950;
return z;
}

2. I cut and pasted ur program, compiled it using Borland's free compiler and it ran fine.

here's what I get...

Guess: 11.617638
Guess: 10.730997
Root of the sixth degree equation is: 10.730997

so, not sure what ur problem is.
Oh, I did move the function definitions in front of the main program, but I think that's just cuz this compiler is picky, and didn't see them.
good luck

3. yes it does give that value when i move teh functions to teh front of main but the value is still incorrect
i tried teh program with a smaller equation like x^2-3x+2 and it gives wrong result..
cant figure put wht the problem is..
Thanx a heap for ure help

float f(float x)
{
float y;
y= x*x*x*x*x*x - 10*(x*x*x*x*x) - 38*(x*x*x*x) +368*x*x*x) + 445*(x*x) - 2950*x -3000;
return y;
}
float fprime(float x)
{
float z;
z= 6*(x*x*x*x*x) - 50*(x*x*x*x) - 152*(x*x*x) + 1140*(x*x) + 890*x -2950;
return z;
}

int main(int argc, char *argv[])
{
float new_guess;
float last_guess;
new_guess=1;
do
{
last_guess=new_guess;
new_guess = last_guess - f(last_guess)/fprime(last_guess);//relation to calculate result
printf("Last Guess: %f\n",last_guess);//prints the last guess
printf("New Guess: %f\n",new_guess);/*primnts teh calculated value of new guess*/
printf("f=:%f\n",f(new_guess));//prints value of f(x)
printf("fprime=:%f\n",fprime(new_guess));/*prints value of fprime(x)*/
}while(abs(new_guess - last_guess) > 0.01);//condiiton for loop
printf("Root of the sixth degree equation is: %f\n",new_guess);
printf("%f\n",f(new_guess)); /*if root evaluated correctly this should be almost zero*/
return EXIT_SUCCESS;
}

5. Also you seem to have misplaced a bracket in f()

6. Also, when posting code, could you use the [CODE] tag (the # button above)?

It makes it a lot easier to read as it doesn't break lines & it keeps tabs.

Thanks

Tell me if the above stuff helped.

7. hi stranger and dragon...thanx again for all ure help..this one is still giving me problems
even after correcting the values i still cant get it to give me the correct result.Must be somethign to do with the logic
Ny suggesstions
Thanx again

8. Your maths looks OK... I get -3 as the answer, which works.

9. yes it is...stupid me
thanx

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q