Skip to content

Commit 35365e8

Browse files
committed
keep results in a struct rather than a pair
1 parent fc192e2 commit 35365e8

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

contents/verlet_integration/code/c++/verlet.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include <iomanip>
22
#include <iostream>
3-
#include <utility>
43

5-
using vpair = std::pair<double, double>;
4+
struct timestep {
5+
double time;
6+
double vel;
7+
};
68

79
double verlet(double pos, double acc, double dt) {
810

@@ -19,7 +21,7 @@ double verlet(double pos, double acc, double dt) {
1921
return time;
2022
}
2123

22-
vpair stormer_verlet(double pos, double acc, double dt) {
24+
timestep stormer_verlet(double pos, double acc, double dt) {
2325

2426
double prev_pos = pos;
2527
double time = 0;
@@ -35,10 +37,10 @@ vpair stormer_verlet(double pos, double acc, double dt) {
3537
vel += acc * dt;
3638
}
3739

38-
return std::make_pair(time, vel);
40+
return timestep { time, vel };
3941
}
4042

41-
vpair velocity_verlet(double pos, double acc, double dt) {
43+
timestep velocity_verlet(double pos, double acc, double dt) {
4244

4345
double time = 0;
4446
double vel = 0;
@@ -48,12 +50,10 @@ vpair velocity_verlet(double pos, double acc, double dt) {
4850
vel += acc * dt;
4951
}
5052

51-
return std::make_pair(time, vel);
53+
return timestep { time, vel };
5254
}
5355

5456
int main() {
55-
double time, vel;
56-
5757
std::cout << std::fixed << std::setprecision(8);
5858

5959
// Note that depending on the simulation, you might want to have the
@@ -63,21 +63,21 @@ int main() {
6363
// you might need to also change the acceleration to be read into
6464
// each of these functions.
6565

66-
time = verlet(5.0, -10, 0.01);
66+
double time = verlet(5.0, -10, 0.01);
6767
std::cout << "Time for Verlet integration is: " \
6868
<< time << std::endl;
6969

70-
std::tie(time, vel) = stormer_verlet(5.0, -10, 0.01);
70+
timestep timestep_sv = stormer_verlet(5.0, -10, 0.01);
7171
std::cout << "Time for Stormer Verlet integration is: " \
72-
<< time << std::endl;
72+
<< timestep_sv.time << std::endl;
7373
std::cout << "Velocity for Stormer Verlet integration is: " \
74-
<< vel << std::endl;
74+
<< timestep_sv.vel << std::endl;
7575

76-
std::tie(time, vel) = velocity_verlet(5.0, -10, 0.01);
76+
timestep timestep_vv = velocity_verlet(5.0, -10, 0.01);
7777
std::cout << "Time for velocity Verlet integration is: " \
78-
<< time << std::endl;
78+
<< timestep_vv.time << std::endl;
7979
std::cout << "Velocity for velocity Verlet integration is: " \
80-
<< vel << std::endl;
80+
<< timestep_vv.vel << std::endl;
8181

8282
return 0;
8383

contents/verlet_integration/verlet_integration.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Here is what it looks like in code:
3333
{% sample lang="jl" %}
3434
[import:1-13, lang:"julia"](code/julia/verlet.jl)
3535
{% sample lang="cpp" %}
36-
[import:7-20, lang:"c_cpp"](code/c++/verlet.cpp)
36+
[import:9-22, lang:"c_cpp"](code/c++/verlet.cpp)
3737
{% sample lang="c" %}
3838
[import:3-14, lang:"c_cpp"](code/c/verlet.c)
3939
{% sample lang="java" %}
@@ -79,7 +79,7 @@ Here's what it looks like in code:
7979
{% sample lang="jl" %}
8080
[import:15-31, lang:"julia"](code/julia/verlet.jl)
8181
{% sample lang="cpp" %}
82-
[import:22-39, lang:"c_cpp"](code/c++/verlet.cpp)
82+
[import:24-41, lang:"c_cpp"](code/c++/verlet.cpp)
8383
{% sample lang="c" %}
8484
[import:16-31, lang:"c_cpp"](code/c/verlet.c)
8585
{% sample lang="java" %}
@@ -136,7 +136,7 @@ Here is the velocity Verlet method in code:
136136
{% sample lang="jl" %}
137137
[import:33-45, lang:"julia"](code/julia/verlet.jl)
138138
{% sample lang="cpp" %}
139-
[import:41-52, lang:"c_cpp"](code/c++/verlet.cpp)
139+
[import:43-54, lang:"c_cpp"](code/c++/verlet.cpp)
140140
{% sample lang="c" %}
141141
[import:33-43, lang:"c_cpp"](code/c/verlet.c)
142142
{% sample lang="java" %}

0 commit comments

Comments
 (0)