Thread: struct help please

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    15

    struct help please

    I am very new to programming, infact this is my first class envolving C programming and i am very stumped on struct. I have to do this problem..

    A straight line is an object connecting tow points. Therefore, a line can be represented by a nested structure having two structures of the type POINT, as defined in Problem 32.

    Code:
    typedef  struct
    {
     POINT beg;
     POINT end;
    } LINE;
    write a function that accepts two parameters of type POINT and returns a structure of type LINE representing the line connecting the two points.

    void printLine (LINE l); /* This prints the two points in a line */

    void printPoint (POINT p); /* this prints the x and y coordinates in a point */

    Your code should allow you to enter two points (a total of 4 integers) and print out the line (the same 4 integers), You don't need to do any line conversions (like y=mx+b remember that)



    ok now you see my assignment can anyone please give me some help on this subject. coding, advice, anything to get me going on this. I will be forever thankful.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    What do you need help on, passing the structs to functions or drawing the lines, or making the LINE struct from two POINTs?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    15
    to be honest i need help on all of what you said. I know that is probably terrible, but I am completely confused with this topic.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    This site helped me out with basic C programming. Give it a try

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    15
    does anyone have some coding along the lines of this project please. it would mean a great deal to me.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok LINE contains two POINTS which contain what? I'm going to assume an X and a Y integer.

    So:
    Code:
    LINE L1;
    L1.beg.X = 5;
    L1.beg.Y = 10;
    Now we have a point for the beginning at (5,10). I'll let you figure out how to load the second point. But I have shown you how to access a struct inside of another struct so you shouldn't be able to do enough to at least produce some of your own code. Once you've done that please post it so we may help you further.

  7. #7
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    #1 First create a type called POINT. To create the POINT copy the LINE structure you show above and change the names etc. You should have int x; and int y; in the POINT structure.

    #2 Declare two variables of type POINT in your main. POINT p1, p2;

    #3 Declare one variable of type LINE in your main.

    #4 Write code in the main to prompt user to enter points and store the points entered to your point variables p1 and p2.

    #5 Write a function declared as LINE createline(POINT p1, POINT p2). Then write the definition of this function. In the function create a variable that is a LINE (static LINE line). Lastly assign your passed in variables p1, and p2 to the variables in the LINE using (memcpy(&line.beg,&p1,sizeof(POINT));etc...). Or you can assign directly into the variables using something like line.beg.x = p1.x; -> If you don't want to use memcpy or are not allowed. Lastly, return line.

    #6 In your main - call the function like myline = createline(p1, p2); Assuming myline is the line you created in main.

    #7 Lastly call the other functions shown above to print the line (passing the line you created - myline). I don't know if the teacher provided those or if you have to write them also.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    15
    man this is really getting hard. all this talk from penny and i still dont understand. Penny if you are still on could you please give me a simple coding of what you are talking about.

    thank you.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    i'd help you but i haven't learned points , but i do know structs :dunno:

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    15
    Code:
    typedef struct
    {
     POINT beg;
     POINT end;
    } LINE;
    
    
    typedef struct
    {
     int x;
     int y;
    } POINT;
     
    int main(){
    
    POINT p1,p2;
    LINE myline;
    
    printf("Please give me the first number point");
    scanf("%d", &p1);
    
    printf("Please give me the second number point");
    scanf("%d", &p2);

    this is what i came up with up until the askin for the values. after that i dont understand what you were writing, can someone give something to help.. coding is always a plus.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You should probably define POINT before you try to describe a LINE being made up of POINTs.

    The "%d" specifier is for an int; p1 is not an int. The 'x' and 'y' members of 'p1' are ints. So you need to access these struct members.
    Code:
    #include <stdio.h> 
    
    typedef struct
    {
       int x;
       int y;
    } POINT;
    
    typedef struct
    {
       POINT beg;
       POINT end;
    } LINE;
    
    int main(void)
    {
       POINT p1,p2;
       LINE myline;
    
       printf("Please give me the x coordinate of p1: ");
       scanf("%d", &p1.x);
    
       printf("Please give me the y coordinate of p1: ");
       scanf("%d", &p2.y);
    
       printf("p1: (%d,%d)\n", p1.x, p1.y);
       return 0; 
    }
    And continue building from there.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    15
    #include <stdio.h>

    typedef struct
    {
    int x;
    int y;
    } POINT;

    typedef struct
    {
    POINT beg;
    POINT end;
    } LINE;

    int main(void)
    {
    POINT p1,p2;
    LINE myline;

    printf("Please give me the x coordinate of p1: ");
    scanf("%d", &p1.x);

    printf("Please give me the y coordinate of p1: ");
    scanf("%d", &p1.y);

    printf("p1: (%d,%d)\n", p1.x, p1.y);

    printf("Please give me the x coordinate of p2: ");
    scanf("%d", &p2.x);

    printf("Please give me the y coordinate of p2: ");
    scanf("%d", &p2.y);

    printf("p1: (%d,%d)\n", p2.x, p2.y);

    (memcpy(&LINE.beg, &p1, sizeof(POINT));
    (memcpy(&LINE.end, &p2, sizeof(POINT));

    myline = createline(p1, p2)
    return;
    }


    got this far and now i can't get it to accept the memcpy line. Keeps saying parse error for some reason. any ideas or suggestions on how it is going now?

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Or, skip the middleman altogether.
    Code:
    int main(void)
    {
       LINE myline;
    
       printf("Please give me the x coordinate of beg: ");
       scanf("%d", &myline.beg.x);
    
       printf("Please give me the y coordinate of beg: ");
       scanf("%d", &myline.beg.y);
    
       printf("Please give me the x coordinate of end: ");
       scanf("%d", &myline.end.x);
    
       printf("Please give me the y coordinate of end: ");
       scanf("%d", &myline.end.y);
    
       printf("myline: beg(%d,%d), end(%d,%d)\n", 
              myline.beg.x, myline.beg.y, myline.end.x, myline.end.y);
    
       return 0; 
    }
    [edit]
    But if you must, why not just copy structures?
    Code:
    int main(void)
    {
       POINT p1, p2;
       LINE myline;
    
       printf("Please give me the x coordinate of p1: ");
       scanf("%d", &p1.x);
    
       printf("Please give me the y coordinate of p1: ");
       scanf("%d", &p1.y);
    
       printf("Please give me the x coordinate of p2: ");
       scanf("%d", &p2.x);
    
       printf("Please give me the y coordinate of p2: ");
       scanf("%d", &p2.y);
    
       myline.beg = p1;
       myline.end = p2;
    
       printf("myline: beg(%d,%d), end(%d,%d)\n", 
              myline.beg.x, myline.beg.y, myline.end.x, myline.end.y);
    
       return 0; 
    }
    [/edit]
    Last edited by Dave_Sinkula; 04-29-2004 at 08:01 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    15
    thank you for helping me. It all works out great now. I'm really glad that i turned to this message board to get some very good help.

    once again thank you.

  15. #15
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    You almost got it but you still did not fulfill the assignment by writing a createLine function: Here's a snippet of what I believe the teacher is asking for. Sorry for the late response - took the day off it was beautiful here in PA:
    Code:
    typedef struct{
        int x,y;
    }POINT;
    typedef struct{
        POINT p1,p2;
    }LINE;
    LINE createline(POINT p1, POINT p2);
    int main(void)
    {
      POINT p1, p2;
      LINE myline;
      printf("Please give me the x coordinate of p1: ");
       scanf("%d", &p1.x);
    
       printf("Please give me the y coordinate of p1: ");
       scanf("%d", &p1.y);
    
       printf("Please give me the x coordinate of p2: ");
       scanf("%d", &p2.x);
    
       printf("Please give me the y coordinate of p2: ");
       scanf("%d", &p2.y);
    
      myline = createLine(p1,p2);
      /* You should probably call the teacher provided print functions above (printPoint, and printLine) and this printf is what would be in your own printLine function if you had to write it */
      printf( "line = %d, %d, %d, %d\n",
              myline.p1.x,myline.p1.y,myline.p2.x,myline.p2.y);
      /* I did not write these - either your teacher must provide or you must write */
      printPoint(p1);
      printPoint(p2);
      printLine(myline);
    }
    /* Here's the createline function that you were supposed to write */
    LINE createLine(POINT p1, POINT p2)
    {
        static LINE aline;
        memcpy(&aline.p1,&p1,sizeof(POINT));
        memcpy(&aline.p2,&p2,sizeof(POINT));
        return aline;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM