Iteration with randomly generated values

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.
I don't understand your question. What is it you are trying to do with your list of 3D points?

[edit] Oh, I think I get it. You want to produce a matrix with the distance between every point?

First, since you are using C++, please write in C++ and get rid of the C stuff. You are horribly limiting yourself.

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
// 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.
using namespace 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!
Last edited on
Im trying to calculate the potential energy
but i need to first find the distance between the points
You'll find them on line 90.
I'm trying to calculate the potential energy

Really?

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
Topic archived. No new replies allowed.