#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0

    What does the line stand for


    Hi everyone. I have a programe and I'm trying to understand what's going on... in one of the lines.

    the code is the following:
    Code:
    int main()
    {
        srand((unsigned) time(0));
    
    	double PI = 3.14159265358979323846;
    
        point points[N], randompoint;
        int i = 0; 
    	int j;
    
        //generate N different points
        while (i < N) {
            randompoint.x = (double) (rand() % XLIM);
            randompoint.y = (double) (rand() % YLIM);
            int exist = 0;
            for (j = 0; j < i; ++j)
                if (points[j].x == randompoint.x && points[j].y == randompoint.y) {
                    exist = 1;
                    break;
                }
            if (!exist) {
                points[i].x = randompoint.x;
                points[i].y = randompoint.y;
                ++i;
            }
        }
    My question, is about the line: point points[N], randompoint;

    What does it do? Or what kind of "thing" is it?
    Maybe I should include the rest of the int main, let me know if this is not enough information.
    Thanks in advance :)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    i think program is incomplete or u may have missed something..
    point is a user defined datatype... it will be mostly a structure and its variables are x and y......
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    what is xlim ylim... even some header file may be missing and N must have a value...

    structure may look like this

    struct p{
    int x,y;
    }point;
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Code:
    point points[N], randompoint;
    Declares two variables, an array of points and a single point. It is the same as:

    Code:
    point points[N] ;
    point randompoint ;
    but is written by terminally lazy coders.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    N, XLIM, and YLIM are undoubtedly macros created by #define statements; eg:
    Code:
    #define N 100
    #define XLIM  42
    #define YLIM  42
    When you compile the program, first the pre-processor will scan through the source code and replace every occurance of a macro name with the string that it's defined as being; eg, assuming the example #define's above, this
    Code:
        point points[N], randompoint;
        int i = 0; 
    	int j;
    
        //generate N different points
        while (i < N) {
            randompoint.x = (double) (rand() % XLIM);
            randompoint.y = (double) (rand() % YLIM);
    will become
    Code:
        point points[100], randompoint;
        int i = 0; 
    	int j;
    
        //generate N different points
        while (i < 100) {
            randompoint.x = (double) (rand() % 42);
            randompoint.y = (double) (rand() % 42);
    Like the missing declaration of the struct point, those missing macro definitions are probably above main in the upper portion of the program that was not included.

    BTW, it is common coding convention to write all macro names in all-caps, so that when reading the source code you can immediately recognize the macros. And since by this convention no other identifiers (variable, typedef, or function names) are written in all caps, that allows you to "reuse" those names. Therefore, while N is a macro, you can still use n as a variable name.

    Comments on this post

    • swapy agrees
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0
    thank you for the clear and simple answer! :)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0
    Thank you for your response.
    Yes, indeed, those macros are above ind the program. :)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    if i am not wrong point is a structure right?

    sorry i got it
    Last edited by swapy; January 23rd, 2013 at 03:02 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0
    Yes, it is. It's my mistake. Sorry

IMN logo majestic logo threadwatch logo seochat tools logo