-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError.cpp
More file actions
55 lines (44 loc) · 1.18 KB
/
Error.cpp
File metadata and controls
55 lines (44 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <math.h>
using namespace std;
// function to calculate f(x) for given x
double calcF(double x) {
return sqrt(1 - x*x);
}
// Returning multiple values using structures
struct PiResults{
float approx;
float error;
};
// structure function
PiResults pi_approx(int N){
// initializing the limits
double a = 0;
double b = 1;
// partition the interval [a,b]
double x[N];
for (int i = 0; i < N; i++) {
x[i] = a + (b - a) / N * i;
}
// initializing with 0
long double approx = 0;
long double error = 0;
// calculation of given integral
for (int i=1; i < N; i++) {
approx += (x[i] - x[i-1]) * ((calcF(x[i]) + calcF(x[i-1])) / 2);
}
PiResults result;
result.approx = approx*4; // multiply by 4 to get Pi
result.error = abs(approx*4 - M_PI); // Error calculation
return result;
}
// Main function
int main() {
// Asking for number of intervals N
int N;
cout << "Enter the number of intervals (N): ";
cin >> N;
PiResults result = pi_approx(N);
cout << "Approximated Pi: " << result.approx << endl;
cout << "Error: " << result.error << endl;
}