This code generates random values for the x, y, and z position of each particle up until the number of particle inputted by the user.
#include <cstdlib>
#include <iostream>
int main(){
int N , x, y, z;
printf("Enter the number of particles: \n");
scanf("%d", &N);
random [N];
printf("The position of the particles are\nx: y: z:\n");
srand(time(NULL));
for(int i = 0; i < N; i++){
int x = rand() % 100;
int y = rand() % 100;
int z = rand() % 100;
printf("%d %d %d\n", x, y, z);
}
return 0;
}
Now i'm trying to take the output which is this:
Enter the number of particles:
3
The position of the particles are
x: y: z:
51 12 56
66 27 33
44 31 78
And Iterate over each body (i)and then within that loop, iterate over every other body(j). And finally calculate the distance between i and j (dij ).
How would i do this? Do i need to assign the output to a array? I know i should use a continue but i don't know how to iterate when the values are random everytime and there could be a different number of x, y, and z's depending on the user input.
// ditch the printf() and scanf() stuff. Use C++'s iostreams for an easy life.
#include <iomanip>
#include <iostream>
// We'll do some math...
#include <cmath>
// And store lists of things...
#include <vector>
// And we want random number generation
#include <chrono>
#include <random>
// Sigh.
usingnamespace std;
// Life is easier when you have an actual point structure.
// Here's one that's compatible with your array of three integers.
struct point
{
int x, y, z;
int& operator [] ( int n ) { return (&x)[ n ]; }
};
// This'll get us the distance between two 3D points.
// Pythagoras's theorem will do.
double distance( point a, point b )
{
double x = a[0] - b[0];
double y = a[1] - b[1];
double z = a[2] - b[2];
return sqrt( x*x + y*y + z*z );
}
// And for a list of points, our go-to, bread-and-butter object for lists of things:
typedef std::vector <point> points;
// We also want to generate a matrix of scalar values, which is easy enough:
typedef std::vector <double> matrix_row;
typedef std::vector <matrix_row> matrix;
// Here's the function that does it: given a list of points, find the distances
// between each one. (Not surprisingly, the resulting diagonal will be zeros.)
matrix distances( const points& ps )
{
matrix m;
for (auto a : ps)
{
matrix_row ds;
for (auto b : ps)
{
ds.push_back( distance( a, b ) );
}
m.push_back( ds );
}
return m;
}
// Here's the main program:
int main()
{
// Ask for the number of particles (points) to generate
int num_particles;
cout << "Enter the number of particles: ";
cin >> num_particles;
// Initialize our random number generator
std::ranlux48 rng( chrono::high_resolution_clock::now().time_since_epoch().count() );
// Create a list of random particles (points) in a 0..99 cube
points ps;
while (num_particles--)
{
int x = std::uniform_int_distribution <int> ( 0, 99 )( rng );
int y = std::uniform_int_distribution <int> ( 0, 99 )( rng );
int z = std::uniform_int_distribution <int> ( 0, 99 )( rng );
ps.emplace_back( point{ x, y, z } );
}
// Get a matrix with the distance between each corresponding point
auto m = distances( ps );
// Print it out
for (auto row : m)
{
for (auto d : row)
std::cout << std::setw( 10 ) << d << " ";
std::cout << "\n";
}
}
Good luck!
[edit] Fixed a really dumb typo that may have been messing you up. Sorry!
I mean, will you create a totally new approach compared to all the existing applications that already do compute "energy" or somesuch? Truly novel math?
If you simply wanted the energy, then you would use the existing programs. Judging by other threads you have at least one (written in Fortran) available to you.
If you neither simply need the result* nor are sketching a new algorithm**, then what is it that you seek? How to program? How to use C++?
*Requires no programming at all
**Benefits from/requires strong programming skills