I wrote befor about the bisection method in C++. This time I managed to make two source's and compile them. But both give me some error that i don't understand. Here are the codes:
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <stdlib.h>
- #include <math.h>
- using namespace std;
- double f1( double x) {
- return x*x-pow(1.034554,2);
- }
- double f2( double x) {
- return 2*x;
- }
- double bisection(double (*f)(double),
- double xL=-100, double xR=100) {
- double xN;
- if( (*f)(xL)*(*f)(xR) >0) {
- cout << "Error!" << endl;
- return xL;
- }
- while (fabs(xL-xR)>0.0001) {
- xN = (xL+xR)/2;
- if( (*f)(xL)*(*f)(xN) <0)
- xR = xN; // i.e. xL and xN have different signs
- // so xRand xN have the same signs
- else
- xL = xN;
- }
- return (xL+xR)/2;
- }
- int main() {
- double tmp;
- cout << bisection(f1,-100,100) << endl;
- system("PAUSE");
- }
Expand|Select|Wrap|Line Numbers
- Error!
- -100
- Press any key to continue...
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <cmath>
- #include <stdlib.h>
- #include <math.h>
- using namespace std;
- int main()
- {
- int xR, xL,xM, epsilon;
- cout <<"Enter a value for the left side: " "\n";
- cin >> xL;
- cout <<"Enter a value for the right side: " "\n";
- cin >> xR;
- cout <<"Enter a value for epsilon: " "\n";
- cin >> epsilon;
- while ((xR - xL) > epsilon)
- xM = (xR + xL) / 2;
- if (xL * xM > 0)
- xL = xM;
- else
- xR = xM;
- system ("pause");
- return 0;
- }
Expand|Select|Wrap|Line Numbers
- Error!
- -100
- Press any key to continue...
I need this program working untill 12 o'clock tonight.
Please try to explain the things in a simple way because i am entirely new to programming.
Thank you!