Code:

#include "StdAfx.h"
#include <stdio.h>
#include <math.h>
/* Function Prototypes */
/*double ffunction(),
dfunction(),
calcfunction(),
incguess(),
displayfound(),
displaynotfound();*/
double ffunction(double beta, double gamma, double delta, double x),
dffunction(double beta, double gamma, double x),
calcfunc(double b,double c,double d, double *iguess),
found(double b,double c,double d, double *iguess, double attempt, int count, double x),
notfound(double b,double c,double d, double *iguess, double fx, double x);
int secondattempt(int count);
int
main(void)
{
double b,
c,
d,
iguess;
FILE *inp = fopen("input1.dat", "r");
// *outp;*/
//int inpstat = getc(inp);
//while (inpstat != EOF){
//printf("please enter b, c, d and guess>\n");
fscanf(inp, "%lf %lf %lf %lf\n", &b, &c, &d, &iguess);
calcfunc(b, c, d, &iguess);
//}
fclose(inp);
return(0);
}
double
ffunction(double beta, double gamma, double delta, double x){
return ((pow(x,3))+beta*pow(x,2)+gamma*x+delta);
}
double
dffunction(double beta, double gamma, double x){
return(3*pow(x,2)+2*beta*x+gamma);
}
double
calcfunc(double b,double c,double d, double *iguess){
int attempt=1,
count=0;
double difference=1,
x=*iguess,
approximation,
dfx=dffunction(b, c, *iguess),
fx=ffunction(b, c, d, x);
while (fabs(difference) > 0.000005){
if (count<20 && fabs(dfx)>0.000005){
approximation = x - (ffunction(b, c, d, x)/dffunction(b, c, x));
difference = approximation - x;
x = approximation;
count ++;
}
else if (count>=20 && attempt == 1){
count=1;
attempt++;
difference=1;
x=*iguess + 10;
}else {
notfound( b, c, d, iguess, fx, x);
return(0);}
}
found(b, c, d, iguess, attempt, count, x);
return(x);
}
double
found(double b,double c,double d, double *iguess, double attempt, int count, double x){
printf("Newton's Method\n");
printf("For cubic polynomial a=1; b=%8.5f; c=%8.5f; d=%8.5f\n", b, c, d);
printf("With initial guess = %8.5f\n", *iguess);
if (attempt == 2){
count=2*count;
}
printf("Gives F(x) = zero at x= %8.5f...", x);
printf("...Root found after %d iterations\n", count);
return(0);
}
double
notfound(double b,double c,double d, double *iguess, double fx, double x){
printf("Newton's Method\n");
printf("For cubic polynomial a=1; b=%8.5f; c=%8.5f; d=%8.5f\n", b, c, d);
printf("With initial guess = %8.5f\n", *iguess);
printf("After 20 iterations...");
printf("...Root not found, method did not converge\n");
printf("F(x) = %8.5f and x = %8.5f\n", fx, x);
return(0);
}