Skip to content

Instantly share code, notes, and snippets.

@bitwiser
Last active August 29, 2015 13:57
Show Gist options
  • Save bitwiser/9383547 to your computer and use it in GitHub Desktop.
Save bitwiser/9383547 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
double f(double c3,double c2,double c1,double c0,double x){
double f = c3*x*x*x + c2*x*x + c1*x + c0;
return f;
}
int main(){
char *poly = "C3*x^3 + C2*x^2 + C1*x + C0";
double x, c3,c2,c1,c0, *val;
double x1,x2, max, min;
int num,i;
double mxpos,mnpos;
mxpos = mnpos = 0.0;
printf("For the polynomial %s, enter the value of C3,C2,C1 and C0: ",poly);
scanf("%lf %lf %lf %lf",&c3,&c2,&c1,&c0);
printf("Enter range of x(x1 to x2): ");
scanf("%lf %lf",&x1,&x2);
num = (int)((x2-x1)/0.1)+1;
val = (double*)(malloc(sizeof(double)*num));
for(i=0;i<num;i++){
val[i] = f(c3,c2,c1,c0,x1+0.1*i);
if(i==0){
max = val[0];
min = val[0];
continue;
}
if(val[i]>max){
max = val[i];
mxpos = x1+0.1*i;
}
if(val[i]<min){
min = val[i];
mnpos = x1+0.1*i;
}
}
printf("Largest value of f(x): %lf at %lf\n", max,mxpos);
printf("Smallest value of f(x): %lf at %lf\n", min,mnpos);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment