finding max and min x,y coordiantes

My program should create 1000 random points
// Using structure to hold x and y of a point
// Random seed function to get values of x, y between 0 and 1
// Output all the points with the smallest x coordinates followed by all the points with the smallest y coordinates

My code, however, isn't giving me the smallest coordinates randomly.

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
  #include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;
struct Point
{
    double x;
    double y;
    
};

double random(unsigned long int & seed);
int main()
{
    const int SIZE = 1000;
    unsigned long int seed;
    srand(time(NULL));
    seed = rand();
    
    Point array[SIZE];
    double smallx= array[0].x;
    double smally= array[0].y;
    for(int i=0; i<SIZE; i++)
    {
        array[i].x = random(seed);
        array[i].y = random(seed);
        if(array[i].x < smallx && array[i].y < smally)
        {
            smallx= array[i].x;
            smally= array[i].y;
        }
    }
    cout  << smallx << endl;
    cout  << smally << endl;
    return 0;
}

double random(unsigned long int & seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
    return double (seed)/MODULUS;
    
}
My code, however, isn't giving me the smallest coordinates randomly.

what does that mean? does it not find the smallest, or, are the coordinates not random enough for you?

your choice for finding the smallest seems weird to me. I would have thought doing the distance (from 0,0) or the like would have been the way to do this?
Last edited on
For now, I need my function to give me the smallest points which it is not.
At present, you have:
1
2
3
4
5
        if(array[i].x < smallx && array[i].y < smally)
        {
            smallx= array[i].x;
            smally= array[i].y;
        }


If you want separately the smallest x and the smallest y then ... do the comparisons SEPARATELY, rather than, as present, requiring that you have minimum x and minimum y simultaneously.
Last edited on
define smallest point, then.
It gives you what you asked for, where the X AND the Y are smaller than all the other X AND Ys. This isnt the closest point to 0,0, of course.
look at some coordinates, say you got these in this order:

1, 10000 //first one is smallest by default
2,2 //not smaller, 2 is bigger than 1



Hello cash,

When I started running your program I found some problems.

You define the struct Point with two numeric variables that are uninitialized, but they do contain the garbage value of "-9.2559631349317831e+64", at least on my computer.

later you define an array of this struct and the array contains 1000 structs all with this garbage value.

On lines 23 and 24 you define and set "smallx" and "smally" to "array[0].?", so "smallx" and "smally" now contain the garbage value of "-9.2559631349317831e+64".

Inside the for loop you call the function which gives "x" and "y" a new value.

Then your if state starts with (array[i].x < smallx). So what you could have is "if (0.01234 < -9.2559631349317831e+64)" which would evaluate to false and the rhs of "&&" would never be checked making the if statement always false.

I tried initializing the array to zero, but "0.01234" is still greater than zero.

In the end I found that I could initialize the variables in the struct to one and that worked. This will create an array of 1000 structs with each "x" and "y" set to one, but this is not a problem because the for loop will change these numbers, but not before "smallx" and "smally" are set to one thus allowing the if statement to work.

Hope that helps,

Andy
Thanks, Andy it worked perfectly
You are welcome. Any time.
Topic archived. No new replies allowed.