-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheme_hw2.cpp
More file actions
111 lines (90 loc) · 4.73 KB
/
cheme_hw2.cpp
File metadata and controls
111 lines (90 loc) · 4.73 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Includes: Libraries needed for different tasks
#include <iostream> // For printing output to the console
#include <vector> // For using arrays that can grow/shrink in size
#include <random> // For random number generation
#include <chrono> // For measuring time intervals
#include <cmath> // For math functions like abs()
#include <iomanip> // For formatting output (decimal places)
using namespace std; // Makes C++ syntax a bit simpler for beginners
int main() {
// The known value of pi (same as in your Python code)
const double pi = 3.14159265359;
// Vectors (dynamic arrays) to store results like your Python lists
vector<double> pis; // Estimates of pi
vector<double> percent_errors; // Percent errors
vector<double> times; // Times for each experiment
// List of trial counts (sample sizes), matching your Python list
// Note: In C++, underscores in numbers are not allowed
vector<long long> counts = {100, 100, 100, 100, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
// Set up random number generation: This is equivalent to Python's random.random()
random_device rd; // Used for seeding: "real" randomness from device
mt19937 gen(rd()); // Mersenne Twister engine (standard random engine)
uniform_real_distribution<> dis(-1.0, 1.0); // Generates real numbers between -1.0 & +1.0
// Go through each trial count as in your Python for loop
for (auto count : counts) {
long long inside = 0; // Counter for points inside the quarter-circle
// Start timer (records current time before trials begin)
auto start_time = chrono::high_resolution_clock::now();
// Loop over all counts
for (long long i = 0; i < count; ++i) {
// Generate random x, y between -1 and 1 (uniformly distributed)
double x = dis(gen);
double y = dis(gen);
// If point is inside the unit circle, increment "inside"
if (x*x + y*y <= 1.0)
++inside;
}
// End timer (records current time after trials)
auto end_time = chrono::high_resolution_clock::now();
// Calculate the fraction inside and estimate for pi, as in your code
double frac = static_cast<double>(inside) / static_cast<double>(count); // Must convert to double!
double val = frac * 4.0; // Monte Carlo estimate of pi
pis.push_back(val); // Store result
double pe = fabs(pi - val) / pi * 100.0; // fabs() is "float absolute value"
percent_errors.push_back(pe);
// Calculate elapsed time in *seconds*
chrono::duration<double> elapsed = end_time - start_time;
times.push_back(elapsed.count());
}
// Print results to console in a readable way (similar to your Python prints)
cout << "pi:\n";
for (size_t i = 0; i < pis.size(); ++i)
cout << fixed << setprecision(8) << pis[i] << " ";
cout << endl;
cout << "percent errors:\n";
for (size_t i = 0; i < percent_errors.size(); ++i)
cout << fixed << setprecision(6) << percent_errors[i] << " ";
cout << endl;
cout << "times (seconds):\n";
for (size_t i = 0; i < times.size(); ++i)
cout << fixed << setprecision(6) << times[i] << " ";
cout << endl;
// For plotting, output data to files or formatted text for later use in Python/Excel/Google Sheets/etc
// Create pis_mod as in your code (average of first five, then the rest unchanged)
vector<double> pis_mod;
// Calculate the mean of the first five entries
double mean_first_five = 0.0;
for (int i = 0; i < 5; ++i) mean_first_five += pis[i];
mean_first_five /= 5.0;
pis_mod.push_back(mean_first_five);
// Add the remaining values unchanged
for (size_t i = 5; i < pis.size(); ++i)
pis_mod.push_back(pis[i]);
// Print a table for easy copy-pasting into Python for plotting
cout << "\nFor plotting in Python, copy the following:\n";
cout << "counts = [100, 1000, 10000, 100000, 1000000, 10000000, 100000000]\n";
cout << "pis_mod = [";
for (size_t i = 0; i < pis_mod.size(); ++i) {
cout << pis_mod[i];
if (i < pis_mod.size()-1) cout << ", ";
}
cout << "]\n";
cout << "times = [";
for (size_t i = 4; i < times.size(); ++i) { // times[4:] in python
cout << times[i];
if (i < times.size()-1) cout << ", ";
}
cout << "]\n";
// If you want to output to files, you could use <fstream> library (ask me if you want this).
return 0; // Main function ends
}