Thread: problem with function

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    11

    problem with function

    Hi, everything running smooth but there is 1 mistake cuz my output is always 0.00000

    The function should basically return the distance between the points
    idk whats wrong tho.

    Thanks for any advice!

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    //Distance funktion
    
    
    double distance(double x1, double y1, double x2, double y2, double erg1){
    				return sqrt(pow(x2,-x1)+pow(y2,-y1));
    }
    
    
    int main()
    
    
    {
    double x1,y1,x2,y2,erg1;
    
    
    printf("x1 y1 x2 y2");
    scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
    printf("The distance is %lf", distance);
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,451
    Isn't distance a function?

    A decent compiler would at least warn you about the problem, if it is setup correctly.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    11

    y

    Quote Originally Posted by jimblumberg View Post
    Isn't distance a function?

    A decent compiler would at least warn you about the problem, if it is setup correctly.

    we have to use gcc, and i'm supposed to write the function myself ^^
    no warnings, but the result is just 0.000

  4. #4
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    814
    You need to enable warnings for GCC, e.g. add the -Wall and -Wextra flags.

    Code:
    warning: format '%lf' expects argument of type 'double', but argument 2 has type 'double (*)(double,  double,  double,  double,  double)

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should probably also look up what pow does, because once you do get an answer you're still not going to like the answer you get.

    And what is the purpose of erg1?

  6. #6
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    29
    Hi Lyrockzz!

    Adrian is right, you need 4 arguments used by 'distance' in line 22.

    erg1 is unused and can receive the return value of 'distance'
    example:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    //Distance funktion
    double distance(double x1, double y1, double x2, double y2){
     return sqrt(pow((x2-x1), 2) + pow((y2-y1), 2));
    }
    
    int main()
    {
    double x1,y1,x2,y2, erg1;
    
    printf("x1 y1 x2 y2: ");
    scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
    erg1 = distance(x1, y1, x2, y2);
    
    printf("The distance is %lf", erg1);
    return 0;
    }
    example for input and output on the screen:
    x1 y1 x2 y2: 0 0 50 86.6025403
    The distance is: 100.000000

  7. #7
    Registered User
    Join Date
    Nov 2018
    Posts
    11
    Quote Originally Posted by -Adrian View Post
    You need to enable warnings for GCC, e.g. add the -Wall and -Wextra flags.

    Code:
    warning: format '%lf' expects argument of type 'double', but argument 2 has type 'double (*)(double,  double,  double,  double,  double)
    but my type is "double" i dont get the error ^^

  8. #8
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    814
    I was referring to the code you originally posted, where distance is a function (function-pointer), not a double. Anyway, looks like you've been shown a solution.

  9. #9
    Registered User
    Join Date
    Nov 2018
    Posts
    11
    Thanks, Now i get it, sorry im still figuring out how to properly use functions and everything, helped alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-02-2016, 07:12 AM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM

Tags for this Thread