Thread: I Need Help Defining and Calling Functions

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Question I Need Help Defining and Calling Functions

    Here is a link to what I am trying to do... I'm not sure what I am doing wrong.

    http://ezekiel.vancouver.wsu.edu/~cs...ts/pdf/pdf.pdf

    Here is my source code thus far...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define INV_SQRT_2PI 0.39894228   /* 1/sqrt(2*pi) */
    #define epsilon 0.0000001
    double a, b, approx, u, U, O;
    int x;
    double o=epsilon;
    
    
    
    double f(int x)
    {	
    	double f(int x); = exp(((double)x*x)/2);
    	return f(x);
    }
    double S(double a, double b)
    {
    	double S(double a, double b); = ((b-a)/6)*(f(a)+4*f((a+b)/2)+f(b));
    	return S(a,b);
    }
    double asimpson(double a,double b,double approx, double o)
    {
    	double c=(a+b)/2;
    	double left=S(a,c);
    	double right=S(c,b);
    	double d=(left+right-approx)/15;
    	if(abs(d)<=epsilon)
    		return left+right+d;
    	return asimpson(a,c,left,o/2)+asimpson(c,b,right,o/2);
    }
    int main(void)
    {	
    	scanf("%lf", &x);
    	scanf("%lf", &U);
    	scanf("%lf", &O);
    	double p, u, o;
    	u=(x-U)/O;
    	p=(1/2)+INV_SQRT_2PI * asimpson(0,u,S(0,u),o);
    
    	printf("pdf= %f",p);
    
    return 0;
    }
    Values of x=72, U=69, and O=2.8 should give a p=0.85801

    You'll all probably find it disgustingly crude, but I've never done any programming before... please be kind and help a newbie.

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Question Still need some help please!!

    I think I might have it a little better now. I'm getting the error code "function-style initializer appears to be a function definition" on line 27, 31, and 35. How do I fix this?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define INV_SQRT_2PI 0.39894228   /* 1/sqrt(2*pi) */
    #define epsilon 0.0000001
    double a, b, u, U, O, c, left, right, d, approx;
    int x;
    double o=epsilon;
    double f(int x);
    double S(double a, double b);
    double asimpson(double a, double b, double approx, double o);
    
    int main(void)
    {	
    	scanf("%lf", &x);
    	scanf("%lf", &U);
    	scanf("%lf", &O);
    	double p, u;
    	u=(x-U)/O;
    	p=(1/2)+INV_SQRT_2PI * asimpson(0,u,S(0,u),epsilon);
    
    	printf("pdf= %f",p);
    
    return 0;
    }
    double f(x)
    {	
    	return exp(double(x*x)/2);
    }
    double S(a,b)
    {
    	return ((b-a)/6)*(f(a)+4*f((a+b)/2)+f(b));
    }
    double asimpson(a,b,approx,epsilon)
    {
    	approx= S(a,b);
    	c=(a+b)/2;
    	left=S(a,c);
    	right=S(c,b);
    	d=(left+right-approx)/15;
    	if(abs(d)<=epsilon)
    		return left+right+d;
    	return asimpson(a,c,left,o/2)+asimpson(c,b,right,o/2);
    }

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It helps to use colour or comments or something to indicate exactly which lines are 27 etc, that way we don't have to copy the code into an editor.

    Here's your problem.
    Code:
    double f(x)
    {
        /* ... */
    }
    You got the prototype right.
    Code:
    double f(int x);
    Now put that type into the function definition as well, and you're all set.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Question One more thing...

    Well, it compiles, and runs sucessfully now, but I don't quite think I have the calls done correctly. The only warnings I'm getting are due to the scanf's. The pdf needs to call on the double asimpson(double a,double b,double approx, double epsilon), which calls on the double S(double a,double b), and that calls on the double f(double x)

    If someone could tell me if I have all this coded correctly that would be great. I have until Midnight to try and get it working as best I can.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    double INV_SQRT_2PI= 0.39894228;						/*1/sqrt(2*pi)*/
    double a, b, u, U, O, c, left, right, d, approx, x;
    double epsilon= 0.0000001;								/*epsilon defined as 0.0000001*/
    double f(double x);										/*prototype functions*/
    double S(double a, double b);
    double asimpson(double a, double b, double approx, double epsilon);
    
    
    int main(void)
    {	
    	printf("Enter x Value: ");
    	scanf("%lf", &x);
    	printf("Enter Mean: ");
    	scanf("%lf", &U);
    	printf("Enter Standard Deviation: ");
    	scanf("%lf", &O);
    	double pdf, u;
    	u=((x-U)/O);
    	pdf=((1/2)+INV_SQRT_2PI * asimpson(0,u,S(0,u),epsilon));
    
    	printf("pdf= %f",pdf);
    
    return 0;
    }
    double f(double x){	
    	return (exp(double(x*x*(-1))/2));
    }
    double S(double a,double b){	
    	return ((b-a)/6)*(f(a)+(4*f((a+b)/2))+f(b));
    }
    double asimpson(double a,double b,double approx, double epsilon)
    {
    	approx= S(a,b);
    	c=((a+b)/2);
    	left=S(a,c);
    	right=S(c,b);
    	d=((left+right-approx)/15);
    	if(abs(d)<=epsilon)
    		return (left+right+d);
    	return (asimpson(a,c,left,epsilon/2)+asimpson(c,b,right,epsilon/2));}

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It would help if you didn't have a bunch of global variables with the same names as parameters to your functions.

    Also, apart from declaring variables in the middle of code, there is nothing here which makes this a C++ program (it's all C).

    > double INV_SQRT_2PI= 0.39894228;
    This isn't used - remove it.
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	pdf=((1/2)+INV_SQRT_2PI * asimpson(0,u,S(0,u),epsilon));
    But it should be made "const", since it's not changing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Apart from Daniweb, how many other forums is this on?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ confusion
    By Eirik in forum Windows Programming
    Replies: 14
    Last Post: 04-29-2009, 01:54 PM
  2. Replies: 9
    Last Post: 01-26-2008, 03:12 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Calling a user-defined function in C++
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2002, 12:09 PM
  5. calling methods
    By alcoholic in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2002, 11:23 AM