Thread: Need help with Structure Assignment

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    2

    Arrow Need help with Structure Assignment

    I am having a lot of difficulty's with this assignment for class, ive referenced my textbook aswell as previous programs and cant figure out how to complete it

    here is the assignment:

    Complete the program to add a prototype for and a definition of the swap_points function. This function should take pointers to two instances of struct Point, and modify them so that the first one contains the second one's original data, and the second one contains the first one's original data.

    here is the code provided to me, the program/resource automatically executes it without asking for input from the user

    Code:
    
    
    Code:
    #include <stdio.h>
    
    
     // Do not change the definition of this data type
     
        struct Point {
           double x, y;
        };
    
    
         // TODO: add a prototype for the swap_points function
    
    (The code i have completed and posted below)
    
    
         // IMPORTANT: do not modify the main function in any way
          
    int main(void) {
             struct Point p, q;
             scanf("%lf %lf %lf %lf", &p.x, &p.y, &q.x, &q.y);
             swap_points(&p, &q);
             printf("Result: p.x=%lf, p.y=%lf, q.x=%lf, q.y=%lf\n", p.x, p.y, q.x, q.y);
             return 0;
         }
    
    
         // TODO: add a definition for the swap_points function
    Here is the code with what i could figure out:
    Code:
    // TODO: add a prototype for the swap_points function
    struct Point swap_points(){
        struct Point hold;
        struct Point p;
        struct Point q;
        hold.x = p.x;
        p.x = p.y;
        p.y = hold.x;
        hold.y = q.x;
        q.x = q.y;
        q.y = hold.y;
        return hold;
    }
    The program compiles, but returns whatever was inputted, none of the computations are being reached by the call to the function or something of the sorts, any help or explanation of the program/ assignment would be appreciated

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    swap_points should have a prototype that looks like this
    Code:
    void swap_points(struct Point *p, struct Point *q);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-04-2017, 07:42 AM
  2. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  3. Assignment to a structure array member
    By sj999 in forum C Programming
    Replies: 4
    Last Post: 05-22-2008, 11:13 PM
  4. help with class structure assignment
    By Crcullen3916 in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2007, 02:51 PM

Tags for this Thread