Thread: pointers swap function

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    6

    pointers swap function

    Need to write a function with 2 floats that make the first argument "a - b" and the second one "b - a", then print the 2 with .2 precision.
    Compiles but output is not correct.
    Can anyone tell me why? and maybe suggest me a book with a good pointers chapter? My slides are confusing.

    This is my code.
    Code:
    #include <stdio.h>
    
    
    float diff_abs(float*, float*);
    
    
    float diff_abs(float *a, float *b)
    {
        *a = *a - *b;
        *b = *b - *a;
    }
    
    
    int main(void)
    {
        float x;
        float y;
        scanf("%f", &x);
        scanf("%f", &y);
        diff_abs(&x,&y);
        printf("%.2f\n", x);
        printf("%.2f", y);
        return 0;
    }
    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    I would guess that the problem is that you modify *a before using it, hence the computation for *b is incorrect. If so, then a solution is to save the original value of *a before assigning to it, then use that original value to compute the new value of *b.

    It would be more helpful if you gave a descriptive overview of what diff_abs was supposed to do (rather than just restate how you implemented it), along with your test input, expected output, and actual output.
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    > Can anyone tell me why?
    Because you're treating computational calculations (on finite machines) as being equivalent to mathematical operations.

    Mathematically, you can add 10100 and 10-100 with perfect accuracy, but this doesn't happen on real finite machines. Small numbers just get lost in the noise.

    Code:
        float t1 = *a - *b;
        float t2 = *b - *a;
        *a = t1;
        *b = t2;
    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. #4
    Registered User
    Join Date
    Jul 2020
    Posts
    6
    Thanks, now it works. I didn't explain much more because it didn't have much more in it, except for test input|output. I'll get back to the theory some more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-06-2015, 10:41 AM
  2. swap char pointers
    By Saurabh Mehta in forum C Programming
    Replies: 5
    Last Post: 04-23-2013, 03:27 PM
  3. Swap function and pointers
    By Jdls210 in forum C Programming
    Replies: 5
    Last Post: 12-10-2011, 09:29 AM
  4. Swap 2 words by pointers
    By jaxlle in forum C Programming
    Replies: 2
    Last Post: 01-15-2007, 02:49 PM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM

Tags for this Thread