Skip to main content
36 votes

Car crash reaction time

Some of the numbers might seem plucked out of thin air but they aren't Still better have them explained. I can understand 3.6 and even ...
vnp's user avatar
  • 56.5k
29 votes
Accepted

Simple n-body class in C++

The Great! You're not making the basic beginner error of using using namespace std;! The main() function is only 3 lines of code....
pacmaninbw's user avatar
  • 24k
22 votes
Accepted

Planetary simulation in python

Use a vector type to store positions, velocities and forces Instead of writing out all the calculations for the x and y direction separately, consider using a library that provides you with vector ...
G. Sliepen's user avatar
  • 58.3k
18 votes

Car crash reaction time

I suggest breaking the code into functions, so you can easily import your code into a Python shell and test that your computation is exact (and not just thinking it ...
301_Moved_Permanently's user avatar
15 votes

Ising model simulation

There are many, many things wrong with your code; I'll list some, and then I advise you to fix as many of them as you can (and more) and then repost a new question with the revised code. First of all,...
Quuxplusone's user avatar
  • 19.4k
15 votes

Simple n-body class in C++

First Of All You are doing a great job as a beginner. I have been programming for 10 years and my code for a long time was much, much less readable that what you have written. That said: What needs ...
Marc Olberding's user avatar
13 votes

Car crash reaction time

It crashes if user input for speed_ms is not integer If user presses any key before t = input("GO!! ") his reaction time is ...
Lau's user avatar
  • 231
12 votes

Car crash reaction time

If it's wet, the friction_coefficient will be lower. Which means that Bd will be lower (which is probably wrong). It's ...
Reinstate Monica's user avatar
12 votes

Car crash reaction time

player_1 is read and assigned but never used. If the user misspells wet (e.g. as Wet or as <...
Toby Speight's user avatar
  • 77.1k
11 votes
Accepted

Simulating a two-body collision problem to find digits of Pi

First, this is an awesome video! Upvoted for that reason alone. :) If n=2, the program finishes within milliseconds, whereas for n=3, it takes a whopping 115 seconds. Do you know about big-O ...
Quuxplusone's user avatar
  • 19.4k
11 votes
Accepted

Simulation of a falling ball

using namespace std; is a bad habit to get into, as it can lead to name-collisions and other issues. It's best to explicitly qualify the names we need where ...
user673679's user avatar
  • 11.8k
10 votes
Accepted

Metaballs and Gravity game;

Refactoring code structure As of right now, you have: A struct named Object. A global std::vector<Object> of ...
Ethan Bierlein's user avatar
10 votes

Complex scientific formula for nuclear magnetic resonance

I would consider the practice ok in this case. Your variable try appear to be roughly the "ASCII" equivalent of the ones used in the equation. You cite the paper where you got the equations from. (...
Dair's user avatar
  • 5,906
10 votes

"Do I understand the gravity of the situation?"

I spent quite some time looking at this: High Level I love what you did here, a great showcase of using JavaScript in a powerful way. But from a readability and maintainability perspective, this ...
konijn's user avatar
  • 33.2k
10 votes

Ising model simulation

I'll build off of Quuxplusone's answer. You have issues with tab. int tab[N]; tab[0] = N-1; tab[N+1] = 0; // out of bounds ...
kmdreko's user avatar
  • 426
10 votes

Simple n-body class in C++

Your code is written in a hybrid C/C++ style. For instance your destructor has a delete (I can't find where the corresponding new...
Victor Eijkhout's user avatar
10 votes

Simple n-body class in C++

In addition to the other answers: Use unsigned integer type for DIM, Nbody.step, and Nbody.n...
eanmos's user avatar
  • 405
10 votes

High performance physics simulation - core class design

Missing #includes You are using std::vector, but did not #include <vector>. The code ...
G. Sliepen's user avatar
  • 58.3k
9 votes
Accepted

Calculating a table of deBroglie wavelengths for various electron energies

First, simplify: factor out \$C^4\$ out of square root: \$\lambda(E_k) = h\left/C\sqrt{(\frac{E_k}{C^2}+m_e)^2-m_e^2}\right.\$ Simplify even more: factor out \$m_e^2\$: \$\lambda(E_k) = h\left/C ...
vnp's user avatar
  • 56.5k
9 votes

Simple n-body class in C++

In addition to the other answers: The init_data function does not belong in the Nbody class. Nowhere in the definition of the N-...
Roland Illig's user avatar
  • 21.3k
9 votes

Planetary simulation in python

TL;DR: I created a Github project with everything I suggested and more (like taking all PyGame-related logic away from the Planet class, leaving it only with planet-related logic). Take a look at the ...
KiriSakow's user avatar
  • 191
9 votes
Accepted

Molecular simulation: Minimum Image Convention

periodic_boundary_int = 5 # size of the periodic box in A temperature_float = 2 # temperature of the simulation Thank you for the first comment, that's lovely, ...
J_H's user avatar
  • 29.3k
8 votes
Accepted

Calculators for gas law equations

First things first: Do not use namespace std; This is bad style. Create a class to properly encapsulate your code and simplify passing of variables. I would suggest, that you use enumerations to ...
miscco's user avatar
  • 4,271
7 votes
Accepted

Golf game boilerplate

Some of this is nit-pickery, some is more fundamental: Import Order PEP-8 suggests an ordering to imports. No reason not to use it: Imports should be grouped in the following order: ...
aghast's user avatar
  • 12.4k
7 votes

Simple n-body class in C++

Use a vector math library Find a suitable library that implements coordinate vectors, so you don't have to implement them as arrays of doubles. Ideally your ...
G. Sliepen's user avatar
  • 58.3k
7 votes

Simulating a Solar System Using Command-Line Graphics

Finally, I am well aware that the value of the gravitational constant 𝐺 is 6.67408×10−11 and not 0.5, as I have implemented it here; using the correct value of 𝐺 results in a simulation that runs ...
Eric Lippert's user avatar
  • 14.9k
7 votes
Accepted

Simulating the orbits of the Earth, Moon and Sun together in Fortran

This question is an oldie but goodie. Nice work! It really deserves some love. An edit bumped it recently to the front page, and that got me interested in how I’d refactor the program. The Modern ...
Davislor's user avatar
  • 7,956
7 votes
Accepted

C program that finds local acceleration due to gravity (g)

There's no need for the forward declaration of local_g() (especially as it's immediately followed by the definition). But it should be declared with ...
Toby Speight's user avatar
  • 77.1k
7 votes

VBA functions to calculate sun and moon positions

At a bare minimum declare your variable types when Dimming variables*. If you don't, they're automatically Variant. The VBA ...
FreeMan's user avatar
  • 1,290
7 votes
Accepted

Membrane protein diffusion in different confinement models

Spelling: FATOR -> FACTOR, boudnary -> boundary. ...
Reinderien's user avatar
  • 62.5k

Only top scored, non community-wiki answers of a minimum length are eligible