Created
May 7, 2010 09:39
-
-
Save alfanick/393234 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
#define abs(x) (x>0?x:-x) | |
double f(double x) | |
{ | |
return 2*x*x-3; | |
} | |
int main() | |
{ | |
pair<double, double> zakres; | |
double dokladnosc; | |
double zero = 300000; | |
double srodek; | |
int i = 0; | |
cin >> zakres.first >> zakres.second; | |
cin >> dokladnosc; | |
do | |
{ | |
srodek = (zakres.first+zakres.second)/2.0; | |
zero = f(srodek); | |
cout << "Obieg #" << ++i << " srodek x=" << srodek << " wynik f(x)=" << zero << endl; | |
if (f(zakres.first)*zero <= 0) | |
zakres.second = srodek; | |
else if (f(zakres.second)*zero <= 0) | |
zakres.first = srodek; | |
else { | |
cout << "Brak miejsc zerowych z podanym zakresie" << endl; | |
return EXIT_SUCCESS; | |
} | |
} while (abs(zero) > dokladnosc); | |
cout << srodek << endl; | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment