Thread: more with my 1st structure program

  1. #31
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > can't typecast the varibles in function ?
    Well you can, but that isn't going to help since the final result is still an integer (centre.x is an int), so you'll automatically lose any precious fractions at that point.
    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.

  2. #32
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ok I shall ask about the value in class

    now my next issue is the moving of the rectangle to new
    coordinates along the x-y axis

    I am guessing here, but would I not ASK USER to give me the the amount to move in x direction, the amount to move in y direction and then printout the points of the new rectangle, IF the problem says: "move r by x units in the x direction and y units in the y direction; x and y are additional arguments to the function" ???

    What is the prototype of such function??

    I created a bitmap file to show my rectangles, but see I cannot attach bitmaps to this board. Argh!

    Code:
    #include <stdio.h>
    
    /********************************/
    /* declare structures           */
    /********************************/
    
    struct point {
       int x, y;
    };
    
    struct rectangle {
       struct point upper_left, lower_right;
    } ;
    
    /*********************************/
    /* function prototypes           */
    /*********************************/
    
    int area_of_r(struct rectangle rect);
    struct point center_of_r(struct rectangle rect);
    struct point move_r(struct rectangle rect, int x, int y);
    
    int main() {
    
      struct rectangle r;
      struct point center;
      int area;
    
      int coord_x, coord_y;
    
      printf( "input upper left corner of rectangle r "
               "as 0 0\n");
      scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
      printf( "input lower right corner of rectangle r  "
              "as 0 0\n");
      scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
    
      area = area_of_r(r);
      printf( "\nThe area of rectangle r is "
              "%d\n", area);
    
      center = center_of_r(r);
      printf("\nThe center point of rectangle r is "
             "%d %d\n\n",center.x, center.y);
    
      return 0;
    }
    
    int area_of_r ( struct rectangle rect )
    {
      int area;
      area = (rect.lower_right.x - rect.upper_left.x) *
             (rect.upper_left.y - rect.lower_right.y);
      return area;
    }
    
    struct point center_of_r(struct rectangle rect)
    {
        struct point center;
    
        center.x = ((rect.lower_right.x) +
                                (rect.upper_left.x))/2;
        center.y = ((rect.upper_left.y) +
                               (rect.lower_right.y))/2;
    
        return center;
    }
    
    
    
    /*  starting on this function to move rectangle to new coordinates */
    
    struct point move_r(struct rectangle rect, int x, int y)
    {
      struct point modified_r;
    
      modified_r.upper_left.x = ((rect.upper_left.x)+x);
    
    }
    
    
    Sue B.

    dazed and confused


  3. #33
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but would I not ASK USER to give me the the amount to move in x direction, the amount to move in y direction
    That's a good start

    > What is the prototype of such function?
    Your inputs are
    - a rectangle
    - a new x position
    - a new y position
    Your outputs are
    - a moved rectangle

    So this seems like a good idea
    struct rectangle move_r(struct rectangle rect, int x, int y);
    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.

  4. #34
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    not trying to be an ignoramus here, but why
    would I use the prototype::
    struct rectangle move_r(struct rectangle rect, int x, int y);

    why wouldn't I use ::
    struct point move_r(struct rectangle rect, int x, int y); ????

    assuming using rectangle structure as suggested:
    in the main i would have to declare some variables to use in the call of the above function, right? (say move_x and move_y)
    and another rectangle structure (say new_r)

    then to prompt user for input:
    Code:
    printf("please enter distance to move on x axis");
    printf("please enter distance to move on y axis");
    printf("enter whole number values as # #\n");
    scanf("%d %d",&move_x,&move_y);
    then output could be this:
    Code:
    printf("coords of moved rectangle are : \n ");
    printf("upper left corner : (%d,%d)\n",new_r.upper_left.x,new_r.upper_left.y);
    printf("lower right corner : (%d,%d)\n",new_r.lower_right.x,new_r.lower_right.y);
    Is this even close?????
    Sue B.

    dazed and confused


  5. #35
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Expanding on your code segment...
    Code:
    printf("please enter distance to move on x axis");
    printf("please enter distance to move on y axis");
    printf("enter whole number values as # #\n");
    scanf("%d %d",&move_x,&move_y);
    
    new_r = move_r(struct rectangle rect, int x, int y);
    
    printf("coords of moved rectangle are : \n ");
    printf("upper left corner : (%d,%d)\n", new_r.upper_left.x,new_r.upper_left.y);
    printf("lower right corner : (%d,%d)\n",new_r.lower_right.x,new_r.lower_right.y);
    The way that you are referencing new_r (like new_r.upper_left.x), you are treating it like it's a rectangle, so that's really what it should be. Hence, move_r needs to return a rectangle.

  6. #36
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    I got the program almost done.

    now I need to find if a point (p) lies in the rectangle r (the original rectangle now)

    instructions say to return TRUE or FALSE
    and p is an additional argument of type struct point

    now what is a cool way to do this???

    Shall I use typedef BOOL or what??
    Last edited by sballew; 10-21-2001 at 07:22 PM.
    Sue B.

    dazed and confused


  7. #37
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    well forgot to give current coded program
    btw, someone not on the board helped me with function finding
    center so that points with float values will print.

    here the code




    Code:
    #include <stdio.h>
    
    /********************************/
    /* declare structures           */
    /********************************/
    
    struct point {
       int x, y;
    };
    
    struct rectangle {
       struct point upper_left, lower_right;
    } ;
    
    /*********************************/
    /* function prototypes           */
    /*********************************/
    
    int area_of_r(struct rectangle rect);
    float center_of_r(struct rectangle rect);
    void move_r(struct rectangle rect, int x, int y);
    
    int main() {
    
      struct rectangle r;
      int area;
      int move_x, move_y;
    
      printf( "input upper left corner of rectangle r "
               "as 0 0\n");
      scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
      printf( "input lower right corner of rectangle r  "
              "as 0 0\n");
      scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
    
      area = area_of_r(r);
      printf( "\nThe area of rectangle r is "
              "%d\n", area);
    
      center_of_r(r);
    
      printf("input the amount to move on the x and y axes\n");
      printf("input as # #\n");
      scanf("%d %d",&move_x,&move_y);
      move_r(r, move_x,move_y);
    
      return 0;
    }
    
    int area_of_r ( struct rectangle rect )
    {
      int area;
      area = (rect.lower_right.x - rect.upper_left.x) *
             (rect.upper_left.y - rect.lower_right.y);
      return area;
    }
    
    float center_of_r(struct rectangle rect)
    {
        float center_x, center_y;
    
        center_x = (rect.lower_right.x + rect.upper_left.x)/2;
        center_y = (rect.upper_left.y + rect.lower_right.y)/2;
    
        if ((rect.lower_right.x-rect.upper_left.x)%2 == 1)
             center_x += .5;
        if ((rect.upper_left.y-rect.lower_right.y)%2 == 1)
             center_y += .5;
    
        printf("The center point of rectangle r is : "
               "(%3.2f,%3.2f)\n",center_x,center_y);
    
    }
    
    void move_r(struct rectangle rect, int x, int y)
    {
       printf("The new coordinates of moved rectangle:\n");
       printf("Upper left point  = (%d,%d)\n",rect.upper_left.x+x,
                  rect.upper_left.y+y);
       printf("Upper right point = (%d,%d)\n",rect.lower_right.x+x,
                  rect.upper_left.y+y);
       printf("Lower left point  = (%d,%d)\n",rect.upper_left.x+x,
                  rect.lower_right.y+y);
       printf("Lower right point = (%d,%d)\n",rect.lower_right.x+x,
                  rect.lower_right.y+y);
    
       return ;
    }
    Sue B.

    dazed and confused


  8. #38
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    #define TRUE 1
    #define FALSE 0





    int is_point_within( struct rectangle r, struct point p)
    {
    if( p.x > r.upper_left.x && p.x < r.lower_right.x &&
    p.y > r.upper_left.y && p.y < r.lower_right.y)
    {
    return TRUE;
    }
    return FALSE; // <- this happens if the above doesn't return TRUE
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #39
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    A couple more things:

    You should definately change ALL of the int variables in your structs to floats!!!

    Why? Because you need that accuracy to properly use a rectangle!




    You should definately make the area and center point variables of the struct!!!

    Why? Because if you don't, you will HAVE TO DECLARE new area and center point variables FOR EVERY NEW RECTANGLE!!!



    So:



    struct point
    {
    float x;
    float y;
    };



    struct rectangle
    {
    float area;
    struct point upper_left;
    struct point lower_right;
    struct point center;
    }




    Don't you think?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #40
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    I got everything to work as I want it (the only function I needed as a float to work was the center_of_r function because it is the only one that can give float values. (I am not doing more than the program specifically asks for here)

    What I would like to do is put in a loop so user can check more than one point to see if it lies in rectangle r (the rectangle before it is moved).

    I tried a couple things; cannot get it to work right.

    here's what I have (in blue); but it is causing an infinite loop.
    I don't know what to change.

    Code:
    #include <stdio.h>
    
    /********************************/
    /* declare structures           */
    /********************************/
    
    struct point {
       int x, y;
    };
    
    struct rectangle {
       struct point upper_left, lower_right;
    } ;
    
    /*********************************/
    /* function prototypes           */
    /*********************************/
    
    int area_of_r(struct rectangle rect);
    float center_of_r(struct rectangle rect);
    void move_r(struct rectangle rect, int x, int y);
    void is_point_in_r(struct rectangle rect, struct point p);
    
    int main() {
    
      struct rectangle r;
      int area;
      int move_x, move_y;
      int x,y;
      struct point p;
    
    /******************************************/
    /* prompt user for two points on graph to */
    /* create rectangle r                     */
    /******************************************/
    
      printf( "input upper left corner of rectangle r "
               "as # # : ");
      scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
      printf("\n");
      printf( "input lower right corner of rectangle r "
              "as # # : ");
      scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
      printf("\n");
    
    /*******************************/
    /* call function area_of_r     */
    /* and print result            */
    /*******************************/
    
      area = area_of_r(r);
      printf( "\nThe area of rectangle r is "
              "%d\n\n", area);
    
    /*******************************/
    /* call function center_of_r   */
    /*******************************/
    
      center_of_r(r);
    
    /**************************************/
    /* prompt user to input the amount    */
    /* of movement along the x and y axes */
    /* to move rectangle r                */
    /**************************************/
    
      printf("Input the amount to move on the x and y axes\n");
      printf("Input as # #\n");
      scanf("%d %d",&move_x,&move_y);
    
    
    /*******************************/
    /* call function move_r        */
    /*******************************/
    
      move_r(r, move_x,move_y);
    
    /******************************************/
    /* prompt user for coordinates of a point */
    /* on a x-y graph to later find if point  */
    /* lies inside rectangle r                */
    /******************************************/
    
      printf("Input coordinates of a point to see if it lies\n");
      printf("within rectangle.");
      printf("Input as # #  :  " );
      scanf("%d %d",&p.x,&p.y);
    
    /*******************************/
    /* call function is_point_in_r */
    /*******************************/
    
      is_point_in_r(r,p);
    
    /*  not sure if this should go here or in the function at the end of program
    */ 
       printf("To check another point give coordinates as # #\n");
       printf("Input 0 0 if done : ");
       scanf("%d %d",&x,&y);
    
        while (x!=0 && y!=0)  {
    
            is_point_in_r(r,p);
        }
    
      return 0;
    }
    
    /*************************************************/
    /* function to calculate the area of rectangle r */
    /*************************************************/
    
    int area_of_r ( struct rectangle rect )
    {
      int area;
      area = (rect.lower_right.x - rect.upper_left.x) *
             (rect.upper_left.y - rect.lower_right.y);
      return area;
    }
    
    /*************************************************/
    /* function to find center point of rectangle r  */
    /* made as float function to take into account   */
    /* center could point could be inbetween two     */
    /* integer values                                */
    /*************************************************/
    
    float center_of_r(struct rectangle rect)
    {
        float center_x, center_y;
    
        center_x = (rect.lower_right.x + rect.upper_left.x)/2;
        center_y = (rect.upper_left.y + rect.lower_right.y)/2;
    
        if ((rect.lower_right.x-rect.upper_left.x)%2 == 1)
             center_x += .5;
        if ((rect.upper_left.y-rect.lower_right.y)%2 == 1)
             center_y += .5;
    
        printf("The center point of rectangle r is : "
               "(%3.2f,%3.2f)\n",center_x,center_y);
    
    }
    
    /******************************************************/
    /* function to move points of original rectangle r to */
    /* new location on graph; function prints out         */
    /* coordinates of new location corner points          */
    /******************************************************/
    
    void move_r(struct rectangle rect, int x, int y)
    {
       printf("The new coordinates of moved rectangle:\n");
       printf("Upper left point  = (%d,%d)\n",rect.upper_left.x+x,
                 rect.upper_left.y+y);
       printf("Upper right point = (%d,%d)\n",
                   rect.lower_right.x+x, rect.upper_left.y+y);
       printf("Lower left point  = (%d,%d)\n",rect.upper_left.x+x,
                 rect.lower_right.y+y);
       printf("Lower right point = (%d,%d)\n",
                   rect.lower_right.x+x,rect.lower_right.y+y);
    
       return ;
    }
    
    /***************************************************/
    /* function passes point coordinates input by user */
    /* and finds if point lies within rectangle r      */
    /* returns TRUE is it does; returns FALSE if not   */
    /***************************************************/
    
    void is_point_in_r(struct rectangle rect, struct point p)
    {
       int x,y;
       if(rect.upper_left.x <= p.x && p.x <= rect.lower_right.x
         && rect.lower_right.y <= p.y && p.y <= rect.upper_left.y)
          printf("TRUE\n");
       else
          printf("FALSE\n");
    
    
       return ;
    }
    
    Sue B.

    dazed and confused


  11. #41
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    I tried changing the while loop, but it breaks and never lets me try another round of coordinates WHY?

    Code:
        
    /******************************************/
    /* prompt user for coordinates of a point */
    /* on a x-y graph to later find if point  */
    /* lies inside rectangle r                */
    /******************************************/
    
      printf("Input coordinates of a point to see if it lies\n");
      printf("within rectangle.");
      printf("Input as # #  :  " );
      scanf("%d %d",&p.x,&p.y);
    
    /*******************************/
    /* call function is_point_in_r */
    /*******************************/
    
      is_point_in_r(r,p);     <----------- 1st call works
    
      while (x!=0 && y!=0)  {         <---------------- this loop is not working  
        printf("To check another point give coordinates as "
               "# #\n");
        printf("Input 0 0 if done : ");
        scanf("%d %d",&x,&y);
          if (x==0 && y==0)
              continue;     <------ tried break; too, doesn;t work
          else
             is_point_in_r(r,p);
    
      }
    
      printf("You are DONE !!");
    
      return 0;
    }
    Sue B.

    dazed and confused


  12. #42
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Just need a good old forever loop and a break statement. continue does not break out of loops, replace that statement with break.

    Also, the code you just posted sems to be using x and y where it should be using p.x and p.y

  13. #43
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Finally, finally, got this program done.
    Thanks.
    I caught the p.x and p.y problem...after much trial and error
    Sue B.

    dazed and confused


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Again
    By christianne in forum C Programming
    Replies: 14
    Last Post: 04-06-2006, 12:39 AM
  2. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  3. Simple program structure.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-15-2002, 04:36 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. structure program
    By prlove71 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2002, 09:01 PM