MeehoweCK

Untitled

Apr 2nd, 2021
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using namespace std;
  5.  
  6. double (*funkcja)(double);
  7.  
  8. double fx(double x)     // x + 2
  9. {
  10.     return x + 2;
  11. }
  12.  
  13. double gx(double x)     // x - 5
  14. {
  15.     return x - 5;
  16. }
  17.  
  18. double hx(double x)     // x^2
  19. {
  20.     return x*x;
  21. }
  22.  
  23. struct Punkt
  24. {
  25.     double x;
  26.     double y;
  27. };
  28.  
  29. double(*tabfun[3])(double) = {fx, gx, hx};
  30.  
  31. Punkt minimum(double(*f)(double), double a, double b)
  32. {
  33.     double szukany_x = a;
  34.     double wartosc = f(a);
  35.     for(double x = a; x <= b; x += .000001)
  36.         if(f(x) < wartosc)
  37.         {
  38.             szukany_x = x;
  39.             wartosc = f(x);
  40.         }
  41.     Punkt wynik;
  42.     wynik.x = szukany_x;
  43.     wynik.y = wartosc;
  44.     return wynik;
  45. }
  46.  
  47. int main(int argc, char* argv[])
  48. {
  49.     if(argc != 4)
  50.     {
  51.         cout << "Niepoprawna liczba argumentow. Program zakonczy prace.\n";
  52.         _getch();
  53.         return -1;
  54.     }
  55.    
  56.     Punkt wynik = minimum(fx, -5, 5);
  57.     cout << wynik.x << ',' << wynik.y << endl;
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment