Skip to content

Instantly share code, notes, and snippets.

@alfanick
Created May 7, 2010 09:39
Show Gist options
  • Save alfanick/393234 to your computer and use it in GitHub Desktop.
Save alfanick/393234 to your computer and use it in GitHub Desktop.
#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