Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- using namespace std;
- float Fun(float x)
- {
- float y = x*x*x - 1;
- return y;
- }
- bool ZeroN(float a, float b, int n, float &x)
- {
- int i = 0;
- if (Fun(a)*Fun(b) > 0) return false;
- while (i < n)
- {
- float srodek = (a + b) / 2; //wyznaczamy srodek przedzialu
- if (Fun(srodek) == 0) //sprawdzamy, czy srodek przedzialu jest miejscem zerowym funkcji
- {
- x = srodek;
- return true;
- }
- if (Fun(a)*Fun(srodek) < 0) b = srodek;
- else a = srodek;
- i++;
- }
- x = a + b / 2;
- return true;
- }
- int main() {
- float a,b,x;
- int n;
- cout << "Podaj przedzial, ktory chcesz badac: ";
- cin >> a >> b;
- cout << "Podaj maksymalna ilosc wykonan: ";
- cin >> n;
- if (ZeroN(a, b, n, x)) cout << "Miejsce zerowe to: " << x;
- else cout << "Nie da sie wyznaczyc miejsca zerowego.";
- cin.ignore();
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement