C++ confusion on how to find the root, by bisection, in an array.

"Calls a void function that uses the bisection method to determine the value of c. Because there are two values of c to determine (one for the diabetic and one for the nondiabetic), you will call this bisection-method function twice. For the bisection method, use a tolerance of 0.001, and limit the maximum number of iterations to 100."

I know how to find the root by bisection method, when the user inputs the data, but how would i go about using data i have stored in an array? This is a similar program i used with bisection method with input, how can i do this again but with information from my array?

// finds the root to f(x) = ax^3 + bx^2 + cx + d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<fstream.h>
#include<math.h>
#include<iomanip.h>
// Prototypes of functions used
void bisection(double ,double, double, double,
			   double, double, double&, int& );
double fx(double, double, double, double, double);
const double tol=0.0001; // tolerance for convergence
const int max_iter=100;  // Number of maximum iterations allowed
// main program
void main()
{
	int iteration; // number of iterations
	double a, b, c, d;	  // Constants in f(x)
	double xl, xr;    	  // Starting points for x
	double root; // root found by Method
	cout<<"Enter a, b, c, and d"<<endl<<"separated by a space ";
	cin>>a>>b>>c>>d;
	cout<<"Enter two initial values for x ";
	cin>>xl>>xr;
	bisection(a, b, c, d, xl, xr, root, iteration);// call Method
// Output
	cout<<"The root is = "<<root<<endl;
	cout<<"The number of iterations was = "<<iteration<<endl;
	cout<<"The value of f at the root = "<<fx(a,b,c,d,root)<<endl;
}
// Function Bisection starts here
// Recieves a, b, c, d and x0 values from main program
// Sends root found and the iteration number
void bisection(double a,double b, double c, double d, 
			double xl, double xr, double& root, int& iteration)
{
	double xm;   // local variables
	iteration=0;   // setting number of iterations to zero
do
{
	xm=(xl+xr)/2.;
	++iteration;
	if(fx(a,b,c,d,xl)*fx(a,b,c,d,xm)<0) xr=xm;
	else xl=xm;
	cout<<"xm = "<<xm<<endl;
}  
while ((fabs(fx(a,b,c,d,xm)) >= tol )&& (iteration < max_iter));
	root=xm;  
}
// fx, calculates ax^3+bx^2+cx+d
double fx(double a, double b, double c, double d, double x)
{
	return a*pow(x,3)+b*pow(x,2)+c*x+d;
}


Here is the information for my array in the program:
non-diabetic then diabetic
t S
0 0.9
100 1.011
200 1.074
300 1.097
400 1.092
500 1.071
600 1.041
700 1.010
800 0.981
900 0.956
1000 0.936
1100 0.920
1200 0.909

0 0.90
100 1.028
200 1.143
300 1.244
400 1.334
500 1.412
600 1.479
700 1.535
800 1.583
900 1.624
1000 1.657
1100 1.684
1200 1.707

And here is how i put it in my array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(){
    double sugardataarray[4][13];
    int i, j, k;
    char skiplines[100];
    ifstream datain("sugardata.txt");
    for (i=0; i<=1; i++){
        datain.getline(skiplines, 100);
        }
    cout<<"  Non-Diabetic          Diabetic"<<endl;
    cout<<"  ------------          --------";
    cout<<fixed<<left<<setprecision(3)<<setw(10)<<endl;
    
    for (k=0; k<13; k++){
        for (j=0; j<2; j++){
            datain>>sugardataarray[j][k];
            }}
    for(k=0; k<13; k++){
             for (j=2; j<4; j++){
                 datain>>sugardataarray[j][k];
                 }}   


Sorry about the long post, but i just wanted to give you an idea of where i am in the process of writing the code. Thanks for any help!
Last edited on
Please edit your post to include [code] tags so that the code is formatted and legible.
Topic archived. No new replies allowed.