You need to review or study a C++ text.
Code C++ - [expand] |
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
| #include<iostream>
#include<cmath>
using namespace std;
class Bisection
{
private:
float f, x;
public:
float fx1,x1,x2,x3;
int count, iter;
public:
float func(float x)
{
float fx1;
fx1 = pow(x,3) - pow(x,2) + (1.4*x) + 7.8;
return (fx1);
}
void getInput()
{
cout <<"Enter x1 = ";
cin >> x1;
cout <<"Enter x2 = ";
cin >> x2;
cout <<"Enter number of iterations = ";
cin >> iter;
}
void calcResult()
{
do
{
if(count == iter)
{
break;
}
x3 = (x1 + x2)/2;
cout << endl << "x1 = " << x1 <<" | x2="<< x2 <<" | x3=" << x3 <<" | " << " f(x1)=" << func(x1) << " | f(x2)=" << func(x2) << " | f(x3)=" << func(x3) << endl << endl;
//float temp1 = f(x1);
//float temp2 = f(x3);
if( func(x1) * func(x3) < 0 )
{
x2 = x3;
}
else
{
x1 = x3;
}
count++;
}while ( abs(x1 - x2) < 0.0000001 || func(x3) == 0 );
}
};
int main(void)
{
Bisection bisect;
bisect.getInput();
bisect.calcResult();
return 0;
} |
Compiles without warnings or errors, produces the following when ran:
Enter x1 = 9
Enter x2 = 5
Enter number of iterations = 100
x1 = 9 | x2=5 | x3=7 | f(x1)=668.4 | f(x2)=114.8 | f(x3)=311.6
Hope the above example points you in the right direction.
BigDog
- - - Updated - - -
Alternatively, you could define the func() method as private, if you wish to restrict its use to within the class.
Or you could define the methods as static, which would alleviate the need to instantiate a Bisection object before utilizing the methods.
- - - Updated - - -
And, if you wish to retain the input and output tasks within main(), you can utilize the same class as so:
Code C++ - [expand] |
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
| #include<iostream>
#include<cmath>
using namespace std;
class Bisection
{
private:
float f, x;
public:
float fx1,x1,x2,x3;
int count, iter;
public:
float func(float x)
{
float fx1;
fx1 = pow(x,3) - pow(x,2) + (1.4*x) + 7.8;
return (fx1);
}
public:
void getInput()
{
cout <<"Enter x1 = ";
cin >> x1;
cout <<"Enter x2 = ";
cin >> x2;
cout <<"Enter number of iterations = ";
cin >> iter;
}
void calcResult()
{
do
{
if(count == iter)
{
break;
}
x3 = (x1 + x2)/2;
cout << endl <<"x1 = " << x1 <<" | x2="<< x2 <<" | x3=" << x3 <<" | " << " f(x1)=" << func(x1) << " | f(x2)=" << func(x2) << " | f(x3)=" << func(x3) << endl << endl;
//float temp1 = f(x1);
//float temp2 = f(x3);
if( func(x1) * func(x3) < 0 )
{
x2 = x3;
}
else
{
x1 = x3;
}
count++;
}
while ( abs(x1 - x2) < 0.0000001 || func(x3) == 0 );
}
};
int main(void)
{
Bisection bisect;
cout <<"Enter x1 = ";
cin >> bisect.x1;
cout <<"Enter x2 = ";
cin >> bisect.x2;
cout <<"Enter number of iterations = ";
cin >> bisect.iter;
do
{
if(bisect.count == bisect.iter)
{
break;
}
bisect.x3 = (bisect.x1 + bisect.x2)/2;
cout << endl <<"x1 = " << bisect.x1 <<" | x2="<< bisect.x2 <<" | x3=" << bisect.x3 <<" | " << " f(x1)=" << bisect.func(bisect.x1) << " | f(x2)=" << bisect.func(bisect.x2) << " | f(x3)=" << bisect.func(bisect.x3) << endl << endl;
//float temp1 = f(x1);
//float temp2 = f(x3);
if( bisect.func(bisect.x1) * bisect.func(bisect.x3) < 0 )
{
bisect.x2 = bisect.x3;
}
else
{
bisect.x1 = bisect.x3;
}
bisect.count++;
}
while ( abs(bisect.x1 - bisect.x2) < 0.0000001 || bisect.func(bisect.x3) == 0 );
return 0;
} |
Same results, compiled without warnings or errors:
Enter x1 = 9
Enter x2 = 5
Enter number of iterations = 100
x1 = 9 | x2=5 | x3=7 | f(x1)=668.4 | f(x2)=114.8 | f(x3)=311.6
However, in the example above, you will need to ensure the func() remains public so as to provide access to it from within main().
BigDog