Click here to Skip to main content
15,747,637 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I need a C++ code that generates 100 Cartesian points at random with a uniform distribution between 1 and 100. As output it must print the points ordered by X coordinate in ascending order and indicate the closest points in the distribution. 

What I have tried:

I have tried creating the vcode myself, but couldn't.
Updated 24-Sep-22 15:14pm
Rick York 4-Oct-20 16:01pm    
Start by separating the problem into smaller tasks and do them one at a time with an eye toward generating the data for the next step. As I see, your first task is to generate 100 points in a "uniform distribution", what ever that means. It is best to keep them in some kind of collection since you will have to sort them and determine associations for output.

Try writing code to do that and we'll help you as we can. We will not write that for you though as you will learn little if we do.

You may find many tutorials freely available on the different task you should perform, namely:
  • generate random numbers
  • sort collections
  • find the minimum value of a function of pair of (different) values belonging to a collection.

You may also try the following code:
#include <iostream>
#include <tuple>
#include <vector>
#include <random>
#include <algorithm>
#include <limits>
using namespace std;

using Point = tuple<double, double>;

double generate_scaled( mt19937 & generator );
double distance( const Point & p1, const Point & p2);

int main()
  mt19937 generator{};

  vector < Point  > vpoint;

  while ( vpoint.size() < 100)
    vpoint.emplace_back( make_tuple<double, double>( generate_scaled(generator), generate_scaled(generator) ) );

  sort(vpoint.begin(), vpoint.end(), [](const Point & p1, const Point & p2) { return get<0>(p1) < get<0>(p2); } );

  double min_i, min_j;
  double min_distance = numeric_limits<double>::max();
  for (size_t i=0; i<vpoint.size()-1; ++i)
    for (size_t j=i+1; j<vpoint.size(); ++j)
      auto d = distance( vpoint[i], vpoint[j]);
      if ( min_distance > d)
        min_distance = d;
        min_i = i;
        min_j = j;

  for (const auto & t : vpoint)
    cout << get<0>(t) << ", " << get<1>(t) << endl;

  cout << "closest points are\n";
  cout <<  "p[" << min_i << "] = (" << get<0>(vpoint[min_i]) << "," << get<1>(vpoint[min_i]) << ")\n";
  cout <<  "p[" << min_j << "] = (" << get<0>(vpoint[min_j]) << "," << get<1>(vpoint[min_j]) << ")\n";
  cout << "with distance = " << sqrt(min_distance) << endl;


double generate_scaled( mt19937 & generator )
  double x = generator();
  x = 1.0 + (x - generator.min()) * 99.0 / (generator.max() - generator.min());
  return x;

double distance( const Point & p1, const Point & p2)
  double dx =  get<0>(p1)-get<0>(p2);
  double dy =  get<1>(p1)-get<1>(p2);
  return (dx*dx + dy*dy);
Share this answer
merano99 24-Sep-22 21:15pm    
nice, +5
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Just saying "I tried" doesn't cut it, I'm afraid.
Share this answer
CPallini 5-Oct-20 3:01am    
A Cartesian coordinate system is an orthogonal coordinate system. Points can lie in two or three dimensional space (x,y,z). If we start with a two-dimensional system, the first thing to do is to randomly generate 100 points P(x,y).
The points can have positive or negative x and y values. The task sets the range to positive numbers between 1 and 100. The data type can be defined by the user.
I would suggest here integers, which should be generated by a random generator. So you call the random generator 100 times and get suitable values for x and y. If you use a standard C++ container, you can collect and sort the coordinates. To specify the nearest points in the distribution for each point you have to calculate the distance to all other points for each point and remember the shortest distance and point to be able to output the point.

You could start like this:
#define MAX_POINTS 100
#define MAX_RANGE  100

typedef struct{
  int x, y;

int myrandom(int min, int max)
  int n=0;
  // TODO: generate a number in range
  return n;

int main()
  vector <KOORD> p;

  for (int i = 0; i < MAX_POINTS; i++) {
    KOORD newpoint;
    newpoint.x = ...

I have deliberately kept the ball flat here. Furthermore, I deliberately do not present a complete program here, because otherwise hardly any learning effect would come out.

If an more advanced C++ solution is desired, Palini's suggestion would be appropriate.

Note: The output with the nearest points is not formulated clearly enough and can be interpreted differently. While Palini outputs the two points that are closest together, one could also output the closest to each point. Or it is meant still differently.
Share this answer
Dave Kreskowiak 24-Sep-22 22:08pm    
Look at the date on the question.
merano99 25-Sep-22 4:20am    
Oops, thanks for the hint. I guess it was too late to recognize the corpse. I'll leave it here anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900