Generate by random a set of n points in 2D Cartesian Coordinates System

Dec 26, 2014 at 1:14am
closed account (375jz8AR)
I have a problem with this code, when i try to run it the result is a lot of 0.00000. I need to generate random set of 2d points. I am new, so maybe what i wrote has no sense. Can someone help me?
#include <stdio.h>
#include <math.h>
#include <string.h>
#include<time.h>
#include <stdlib.h>
void main()
{
srand((unsigned)time(NULL));

int x[] = { rand() % 10 + 0 / (double)RAND_MAX + 0};
int y[] = { rand() % 10 + 0 / (double)RAND_MAX + 0};
for (double n = 0; n < 10; n++)
{

printf("%lf\n", x, y);

}
}
Last edited on Dec 26, 2014 at 1:14am
Dec 26, 2014 at 7:07am
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main ()
{
	srand(time(NULL));
	
	int x, y;
	bool xsign, ysign; // true if negative
	
	int arrX[5];
	int arrY[5];
	
	for(int i=0; i<5; i++)
	{
		x = rand()%100;
		y = rand()%100;
		xsign = rand()%2;
		ysign = rand()%2;
		
		if(xsign) x *= -1;
		if(ysign) y *= -1;
		
		arrX[i] = x;
		arrY[i] = y;		
	}	
	
	for (int i=0;i<5; i++) 	
		cout << arrX[i] << ", " << arrY[i] << endl;
	
	
return 0;	
} 
Dec 26, 2014 at 8:08pm
closed account (375jz8AR)
Thank you very much, but can you explain me what did you do in
: if(xsign) x *= -1;
if(ysign) y *= -1;
thank you
Dec 26, 2014 at 8:13pm
It's very simple. If xsign or ysign is true by random chance(really pseudo chance), you turn the coordinate(whether it's x or y) to a negative. You also don't want to modify the number so you simply multiply it by 1.
Dec 26, 2014 at 8:23pm
converts positive to negative.
Dec 26, 2014 at 8:36pm
closed account (375jz8AR)
thank you, how can i put this expresion into printf(...), because i try, but it gives me big values.
cout << arrX[i] << ", " << arrY[i] << endl;
Dec 26, 2014 at 8:43pm
Can you show us how you tried to implement it? In other words, show your code so then maybe we can see where you went wrong.
Last edited on Dec 26, 2014 at 8:53pm
Dec 26, 2014 at 8:52pm
closed account (375jz8AR)
in this case i only have one column of points.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
srand(time(NULL));

int x, y;
bool xsign, ysign; // true if negative

int arrX[5];
int arrY[5];

for (int i = 0; i<5; i++)
{
x = rand() % 100;
y = rand() % 100;
xsign = rand() % 2;
ysign = rand() % 2;

if (xsign) x *= -1;
if (ysign) y *= -1;

arrX[i] = x;
arrY[i] = y;
}

for (int i = 0; i < 5; i++)
printf("%d\n", arrX[i],arrY[i]);



return 0;
Dec 26, 2014 at 9:03pm
Since you're outputting two coordinates, you need two arguments in your printf function. In this case, two %d %d. The 3 is to set the width of the output to make it look neat.



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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
srand(time(NULL));

int x, y;
bool xsign, ysign; // true if negative

int arrX[5];
int arrY[5];

for (int i = 0; i<5; i++)
{
x = rand() % 100;
y = rand() % 100;
xsign = rand() % 2;
ysign = rand() % 2;

if (xsign) x *= -1;
if (ysign) y *= -1;

arrX[i] = x;
arrY[i] = y;
}

for (int i = 0; i < 5; i++)
printf("%3d %3d\n", arrX[i],arrY[i]);



return 0;
}
Last edited on Dec 26, 2014 at 9:04pm
Dec 26, 2014 at 9:10pm
closed account (375jz8AR)
Thank you very much guys, you are great!!
Topic archived. No new replies allowed.