#include <iostream>
#include <cmath>
using namespace std;
// Funkcja f(x) = sqrt(1 - x^2)
double f(double x) {
return sqrt(1.0 - x * x);
}
int main() {
int n;
cout << "Podaj liczbe podprzedzialow: ";
cin >> n;
double a = -1.0;
double b = 1.0;
double h = (b - a) / n;
// ------------------------
// Metoda prostokatow (srodkow)
// ------------------------
double suma_prostokaty = 0.0;
for(int i = 0; i < n; i++) {
double x = a + (i + 0.5) * h; // srodek przedzialu
suma_prostokaty += f(x);
}
double calka_prostokaty = suma_prostokaty * h;
double pi_prostokaty = 2.0 * calka_prostokaty;
// ------------------------
// Metoda trapezow
// ------------------------
double suma_trapezy = 0.5 * (f(a) + f(b));
for(int i = 1; i < n; i++) {
double x = a + i * h;
suma_trapezy += f(x);
}
double calka_trapezy = suma_trapezy * h;
double pi_trapezy = 2.0 * calka_trapezy;
// Wyniki
cout << "\nPrzyblizenie liczby pi:" << endl;
cout << "Metoda prostokatow: " << pi_prostokaty << endl;
cout << "Metoda trapezow: " << pi_trapezy << endl;
cout << "Wartosc z biblioteki: " << M_PI << endl;
return 0;
}