Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include <math.h>
- float horner(float *p, int n, float x);
- float bisection(float *p, int n, float a, float b);
- float horner(float *p, int n, float x)
- {
- /*
- float result = 0.0;
- for (int i=0; i<n; i++) {
- result = result*x + p[i];
- }
- return result;
- */
- // pointer implementation
- float holder;
- holder = 0.0;
- for (int i = 0;i < n;i++){
- holder = holder * x + *(p + i);
- }
- return holder;
- }
- //your bisection method implementation
- float bisection(float *p, int n, float a, float b)
- {
- float result;
- float tolerance = 1e-6;
- float c = 0.0;
- if ( horner(p,n,a) * horner(p,n,b) < 0.0){
- while ( b - a >= tolerance){
- c = (a+b)/2.0;
- if (horner(p,n,c) == 0.0){
- result = c;
- break;
- }
- else if ( horner(p,n,c) * horner(p,n,a) < 0.0 ){
- b = c;
- }
- else {
- a = c;
- }
- }
- if ( b - a < tolerance){
- result = c;
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement