Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
The purpose of the following code is to get a random number of 100 nodes and to distribute these nodes randomly in range 500*500 …(X,Y)


I need to improve this code to distribute these node not in random way
For example the (0,100) ,(0,200)……(0,500)…(1,100)…….(500,500)
I need to put 4 nodes in each (x,y) coordinate
Can anyone help me?
C++
#include<iostream>
#include <fstream>
#include<cmath>
using namespace std;

void main() 
{
	int first[100],secnd[100];
	for (int i=0; i<100 ;i++)
	{   
	    first[i]=rand()%500;  //random number from  to 499
	    secnd[i]=rand()%500;  //random number from  to 499
            cout<<first[i]<<" "<<secnd[i]<<endl;
	}
}
Posted
Updated 3-Nov-12 8:59am
v2
Comments
YvesDaoust 3-Nov-12 9:48am    
Nobody can help you as this question is perfectly unclear. Please rephrase.
_coder 3-Nov-12 16:28pm    
indeed

There is nothing really wrong in your code (provided you need uniform distribution of random points).
As suggested you might use a single array of structs for storing points, e.g.
C++
struct Point
{
  int x,y;
};

Point point[100];
//...
 
Share this answer
 
My understanding of your problem:

You want to fill 5x5 contiguous 2D cells of size 100x100 each with 4 random points.

The solution:

Every cell will be processed independently, using a double loop on X and Y (from 0 to 500 excluded, step 100).

For each cell, generate 4 random points with coordinates in the range 100x100 (i.e. the cell size).

Your 100 points are given by (X + x, Y + y).
 
Share this answer
 
Comments
laian2 4-Nov-12 12:20pm    
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;



int main()

{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";


int miny=0,max_y=99;
for(int i=0;i<5;i++)
{
int minx=0,max_x=99;
for(int j=0;j<5;j++)
{
for(int k=0;k<=4;k++)
{
//////THE Error is in this area/////
//// 1 IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type //////

int x=(minx+rand()*10(max_x +1)%(max_x - minx+1));
int y=(miny+rand()*10(max_y +1)%(max_y - miny+1));
}
minx=max_x+1;
max_x=max_x+100;
}
miny=max_y+1;
max_y=max_y+100;
}


}

That’s exactly what I mean ,
This is what i have done so far but i get an error
YvesDaoust 4-Nov-12 12:25pm    
Missing * operator.

There are also several errors related to coordinates computation. You made it more complicated that need be. Follow my guidelines.
laian2 4-Nov-12 13:13pm    
i fix the error thanx allot
but i try to run my code and i get this result,do you thinck i am right?


0 -0
0 -0
0 -0
0 -0
0 -0
100 -0
100 -0
100 -0
100 -0
100 -0
200 -0
200 -0
200 -0
200 -0
200 -0
300 -0
300 -0
300 -0
300 -0
300 -0
400 -0
400 -0
400 -0
400 -0
400 -0
0 -100
0 -100
0 -100
0 -100
0 -100
............
YvesDaoust 4-Nov-12 15:16pm    
Don't go too far.
If you only need 4 items then there is little point in creating a 500 element array. Also, you could use the Windows POINT[^] structure rather than a 2D array to define each node. I'm not sure what you mean by "distribute these node not in random way", but that is probably mathematics rather than programming.
 
Share this answer
 
Comments
[no name] 3-Nov-12 14:15pm    
Best guess: His teacher wants him to realize that arrays or lists are inadequate solution in this case. How about adding the points to a (quad)tree?

Edit: Does not naming the point 'nodes' already suggest to use a tree?
Richard MacCutchan 3-Nov-12 14:31pm    
Quite possibly, but as with so many questions we only get half the story.
[no name] 3-Nov-12 14:55pm    
Sure, but I think he does not really know how to be more precise yet. I believe him when he writes that he is still new to programming and the whole thing looks like homework.

He writes that he has to put four nodes into each point. That describes a quadtree (quite suitable for the problem), but also tells me that he has no clue what the 'nodes' are all about. It's hard to ask precise questions about something you don't understand.

I have written an answer that (hopefully) turns him into the right direction.
Richard MacCutchan 3-Nov-12 15:15pm    
Agree, and a nice set of suggestions.
Sorry, but I will not do your homework for you. It's better when you do the thinking yourself, so that you learn something.

Here are some hints:

You say you 'need to put 4 nodes in each (x,y) coordinate'. What does that mean?

You are supposed to put the coordinates into a data structure. You already called them nodes, so let's stick with that.

In addition, each node is supposed to have pointers to four other nodes. That describes a tree. There must not always be exactly four nodes attached to any other node. To be precise: This is a quadtree, used to sort two dimensional data, like your points.


So this is what you have to do:

1) Write a data structure or small class to be used for your points. They will need only two values: The X and the Y coordinate of a point. Call this structure 'node', even if it actually is not a node of the tree yet.

2) Add four pointers to nodes to the node structure (or class). Now they have all the data to act as nodes.

3) Do some reading about quadtrees. Simply start with Wikipedia[^].
You will have to add some methods that construct the tree.

Edit: Look at the pseudo code section in the Wikipedia article! It's almost exactly what your solution should look like. All you have to do is to 'translate' it to C/C++ and then you only have to do step 4.

4) Finish the exercise by creating one node randomly as the root of your quadtree and then creating 99 more nodes to have sorted into the tree. This is very simple, once you have understood step 3.
 
Share this answer
 
v3

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