Thread: Struct, points, square

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    32

    Struct, points, square

    Hi guys. I need to create a progam with struct of 4 points that define a square (and something more - that's why there are 3 structs, not one). The goal of the program is to check wheter those points form a real square.

    Code:
    #include <stdio.h> 
    #include <math.h> 
    
    void write();
    
    typedef struct point
    {
    int x;
    int y;
    }
    
    typedef struct square
    {
    point point1;
    point point2;
    point point3;
    point point4;
    }
    
    typedef struct segment
    {
    point point5;
    point point6;
    }
    
    int main() {
    
    void write() {
    printf("Enter x:\n");
    scanf("%d", x);
    Is it a good start?

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    After the declaration of a struct you must place a semicolon:

    Code:
    typedef struct point
    {
    int x;
    int y;
    };
    Also, both main() and write() have only opening accolades, {. they should have a } too.
    For the rest this seems a good start to me (but I'm not a reference)

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    OK, but still there are a lot of errors...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, since you are trying to typedef the struct type, you need a name to typedef it to.

    Quote Originally Posted by jacek
    OK, but still there are a lot of errors...
    Really? I don't believe you, since you were not able to post any of them
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Here's the list:
    c: In function `write':
    c:30: error: `x' undeclared (first use in this function)
    c:30: error: (Each undeclared identifier is reported only once
    c:30: error: for each function it appears in.)
    c:32:2: warning: no newline at end of file

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    What laserlight says, is this:

    Code:
    typedef struct
    {
    int x;
    int y;
    }point;
    
    point a, b;

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Make sure that for every
    Code:
    {
    you have a corresponding
    Code:
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Hey Jacek, did you think about the orientation of the square? I mean: does the bottom line has to be parallel with the X-axis? Or can the bottom line be at any angle?
    This makes a whole difference. The first variant isn't too difficult to implement. The second one is not that easy (not for me).

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Obviously the first one.

    The problem is that I have no idea how to put x1..x4 and y1..y4 into this (I have already algorithm checking wheter 4 points for a square).

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Hey, you make 4 objects of point, as you declared. Then you have to compare the x's and y's.

    If you have 4 points, point_1, point_2, point_3, point_4, then you can access the x and y like this:

    Code:
    if (point_1.x == point_2.x){
    /*do something*/
    }

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    so, before that for example:

    printf("Enter x1:\n");
    scanf("%d", point1.x);
    printf("Enter y1:\n");
    scanf("%d", point1.y);

    ?

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    The second argument of scanf() has to be the address of a variable.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    So what in my example?

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Code:
    scanf("%d", & point1.x);
    & is the address-operator.

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    ok. so far i have this. it compiles, but doesn't check those 4 poinst...

    Code:
    #include <stdio.h> 
    #include <math.h> 
    
    typedef struct
    {
      int x;
      int y;
    } point;
    
    typedef struct
    {
      point point1;
      point point2;
      point point3;
      point point4;
    } square;
    
    typedef struct
    {
      point point5;
      point point6;
    } segment;
    
    int main() {
    
      square s1;
    
      printf("Enter x1:\n");
      scanf("%d", & s1.point1.x);
      printf("Enter y1:\n");
      scanf("%d", & s1.point1.y);
    
      printf("Enter x2:\n");
      scanf("%d", & s1.point2.x);
      printf("Enter y2:\n");
      scanf("%d", & s1.point2.y);
    
      printf("Enter x4:\n");
      scanf("%d", & s1.point3.x);
      printf("Enter y3:\n");
      scanf("%d", & s1.point3.y);
    
      printf("Enter x4:\n");
      scanf("%d", & s1.point4.x);
      printf("Enter y4:\n");
      scanf("%d", & s1.point4.y);
    
    // point (x1,y1) distance to (x2, y2) should equal distance from (x2,y2) to (x3, y3)
    //assuming x1,y1 is the top left, x2,y2 is the bottom left
    int square = 0;
    //x3, y3 is the bottom right
    if ( (s1.point2.y-s1.point1.y) == (s1.point3.x-s1.point2.x) )
            square++;
    //x4, y4 is the top right
    if ( (s1.point4.y-s1.point3.y) == (s1.point4.x-s1.point1.x) )
            square++;
     
    if ( (s1.point4.y-s1.point3.y) == (s1.point2.y-s1.point1.y) )
            square++;
     
    if ( (s1.point3.x-s1.point2.x) == (s1.point4.x-s1.point1.x) )
            square++;
     
    if (square == 4)
            printf("It's a perfect square!");
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM