structures

This is a discussion on structures within the C Programming forums, part of the General Programming Boards category; I am rereading about structures and some of it just doesn't make sense right now. What I need to do ...

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    structures

    I am rereading about structures and some of it just doesn't make sense right now.

    What I need to do is have two structures :

    Code:
    struct point {
                              int x, y;
                       };
    
    struct rectangle (
                                 struct point upper_left, lower_right;
                              };
    The point structure stores the x and y coordinates of a point on a screen. // this makes sense of what I recall of graphing points

    The rectangle structure stores the coordinates of the upper left and lower right corners of a rectangle // I have no idea what this means ; rectangles have 4 right angles thus 4 corners, why only coordinates of 2 corners ???

    I need to write several functions using the above and with r being passed as an argument in rectangle structure // have no idea what this means in laymen's terms.

    If I could get help with the first function I would appreciate it.

    (1) compute the area of r.
    Sue B.

    dazed and confused


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    > why only coordinates of 2 corners ?
    Because that's all you need to describe a rectangle.
    You can easily work out the other two corners -
    lower_left is upper_left.x and lower_right.y

  3. #3
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    Angry

    >You can easily work out the other two corners -
    >lower_left is upper_left.x and lower_right.y

    Well, it isn't so easy for me to work out as you suggest.
    I am new to C and structures by far are a bit disconcerting to
    get.

    But thinking back to my math days...
    if a point (input by user of course) is say (5,7) on a graph.
    lower_right corner would point to (5,0) and
    upper_left corner would point to (0,7)
    Right ??

    now I am not understanding the syntax of how to
    use the members of the rectangle structure labeled
    lower_right and upper_left.

    (how is it written out to use as variable???)

    rectangle.lower_right // does this work OR
    rectangle.lower_right.point.x // does this work

    I see that there is a structure as a member of a structure (nested structures as they're called)

    Please recall my structure for rectangle below
    Code:
    struct rectangle {
        struct point upper_left, lower_right;
         };
    and my structure for point (the point coordinates that will be input by user ) below:

    Code:
    struct point {  int x, y;        };
    now the first instruction is to :

    write a function that performs the operation (compute the area of r) on a rectangle structure r passed as an argument.

    What is r ? Where does it go in the rectangle structure?
    Is it a member of the structure?
    Code:
    struct rectangle {
                                 struct point upper_left, lower_right;
                                 int r;
                              };
    Or is it a variable of struct rectangle?
    Code:
    struct rectangle {
                                 struct point upper_left, lower_right;
                              } r ;
    If I could understand the instructions as stated and then the context they are to be used in a problem, it would help a great deal to go on with the rest of the full program.

    PLEASE HELP !!!

    and I do not know how yet to define own header files
    and I do not know anything about linked lists
    Sue B.

    dazed and confused


  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    In this case 'r' is an object. It is an instance of the structure that you define in main and pass as an arguement to the caller.

    struct rectangle r;

    Function(r);

    If you have a structure in a structure than you access the data in the internal structure through the object as in:

    r.upper_left.x = 2;
    r.upper_left.y = 3;

    This is if the object is passed to the function by value. If you pass by reference as in a pointer you need to use the '->' operator instead of the dot.

  5. #5
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    so if I ask user to input the x value (point structure member) and
    the y value (point structure member) as the coordinates of a point

    then to compute area of r

    int area;
    area = (x.point) * (y.point);
    r.rectangle = area;

    Is any of this referencing variables correctly???
    Sue B.

    dazed and confused


  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    To get area you would have to do this:

    int area = (r.upper_left.x) * (r.upper_left.y);

    This is doing the calculation through the rectangle object and would likely be the correct answer due to the fact that the point structure is declared in the rectangle structure.

    Actually how do you calculate the area. First you would have to tall me how to use the algorithm. This is just how to access the integers stored in the points. Actually the true area of the rectangle would be slightly different. Figure it out on paper first.
    Last edited by Troll_King; 10-14-2001 at 01:25 PM.

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    It would be more like:

    int area = (r.lower_right.x - r.upper_left.x) * (r.lower_right.y - r.upper_left.y);

    correct?

  8. #8
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    CODE so far

    Here's what code written so far;
    given 4 separate functions.

    Right now working on understanding the referencing the members and variables of the named structures.

    Trying to use area_of_r to be the function used to calculated the area of r ( r being the point referenced with the x and y values input by user). x an y are int variables. thus r coordinates will be something like (6,7)

    Please help with referencing these variables and struct members. PLEASE !!!

    Code:
        
    
    #include <stdio.h>
    
    struct point {
                     int x, y;
                  };
    
    
    
    struct rectangle {
                       struct point upper_left, lower_right;
                     } r;
    
    void area_of_r(int x, int y);
    void center_of_r(int x, int y);
    void move_r(int x, int y);
    void point_on_r(int x, int y);
    
    main()
    
    {
    
       /*  get value of x and y of point r on graph   */
    
       printf("input x value of point r : ");
       scanf("%d",x.point);
       printf("\n");
       printf("input y value of point r : ");
       scanf("%d",y.point);
       printf("\n");
    
       printf("point r coordinates are : (%d,%d)",x.point.y.point`);
    
    
    }
    
    
    void area_of_r(int x, int y)
    {
      int area;
      
       r.upper_left.y = y;
       r.lower_right.x = x;
      
      
      area = (r.upper_left.y * r.lower_right.x);
    }
    Sue B.

    dazed and confused


  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include <stdio.h>
    
    struct point {
                     int x, y;
                  };
    
    
    
    struct rectangle {
                       struct point upper_left, lower_right;
                     } ;
    
    void area_of_r(int x, int y);
    void center_of_r(int x, int y);
    void move_r(int x, int y);
    void point_on_r(int x, int y);
    
    int main()
    
    {
       int area;
       struct rectangle r;
       /*  get value of x and y of point r on graph   */
    
       printf("input upper left (x,y) x value of point r : ");
       scanf("%d",&r.upper_left.x);
      
       printf("\ninput upper left (x,y) y value of point r : ");
       scanf("%d",&r.upper_left.y);
    
    
       printf("\npoint r coordinates are : (%d,%d)",r.upper_left.x, r.upper_left.y);
    
       printf("input lower_right (x,y) x value of point r : ");
       scanf("%d",&r.lower_right.x);
      
       printf("\ninput lower_right (x,y) y value of point r : ");
       scanf("%d",&r.lower_right.y);
    
    
       printf("\npoint r coordinates are : (%d,%d)",r.lower_right.x, r.lower_right.y);
    
       area = area_of_r(r);
    
       return 0;
    }
    
    
    int area_of_r(struct rectangle r)
    {
      int area = (r.lower_right.x - r.upper_left.x) * (r.lower_right.y - r.upper_left.y);
      return area;
       
    }
    Now the upper left (x,y) and lower right (x,y) input has to make sense. Try something like (2,2) and (6,8).
    Last edited by Troll_King; 10-14-2001 at 02:47 PM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Code:
    #include <stdio.h>
    struct point {
        int x, y;
    };
    struct rectangle {
        struct point upper_left, lower_right;
    } r;
    
    int area_of_r(struct rectangle rect);
    
    int main() {
        printf( "input upper left corner of r : " );
        scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
        printf( "input lower right corner of r : " );
        scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
        printf( "area of r is %d\n", area_of_r(r) ); 
        return 0;
    }
    
    int area_of_r ( struct rectangle rect ) {
        int area;
        area = (rect.lower_right.x - rect.upper_left.x ) *
               (rect.lower_right.y - rect.upper_left.y );
        return area;
    }
    bah!
    beaten by a few mins...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Does it make sense now sballew?

  12. #12
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    yuneek

    hold it
    hold it


    let me see here

    struct point { int x, y; };

    is used to give coordinates of any point
    struct rectangle { struct point upper_left, lower_right; };
    is making a rectangle out of the points (coordinates) of more than one point that when connected forms a rectangle , eh?

    so the point structure is refered to more than once to find the 2 points that make up 2 corners of the rectangle (am I getting close here??)

    int main() {
    printf( "input upper left corner of r : " );
    scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
    printf( "input lower right corner of r : " );
    scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
    printf( "area of r is %d\n", area_of_r(r) );
    return 0;
    }
    and r is an actual rectangle not a variable of struct rectangle

    I swear I am trying to get the idea of what the problem is asking for and getting hung up on the "picture" of it in my head.

    If r is a rectangle,
    then you can

    1. compute the area of r
    2. find it's center (returning it as a point value)
    3. move r by x units in x-direction and y units in y-direction (where x and y are additional arguments to the function)
    4. determine whether a point p lies within r (the rectangle) - returning TRUE or FALSE (p being an additional argument of type struct point)
    Sue B.

    dazed and confused


  13. #13
    Unregistered
    Guest
    Reread Salem's answer.

  14. #14
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    r is an instance of rectangle. Ofcourse you call twice. One for the x member of the point structure, one for the y member of the point structure. Salem put those together with scanf, I put the scanf's separately. With Salems you enter: 2 2 for example with mine you enter 2 than 2, just not on the same line. It amounts to the same thing, just broken up. The program is self explanitory if you run it.

  15. #15
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ran Salem's code and keep getting
    area = negative number
    (eg. area of r = -16)

    How do I fix that???
    Sue B.

    dazed and confused


Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-06-2006, 12:06 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 09:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21