SImpson's Rule

This is a discussion on SImpson's Rule within the C Programming forums, part of the General Programming Boards category; Hey guys, my instructions are to create a program that approximates the area using Simpson's Rule. I set it up ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    SImpson's Rule

    Hey guys, my instructions are to create a program that approximates the area using Simpson's Rule. I set it up the way my book shows, however it is giving me the following error message:
    'function' cannot convert from 'double *' to 'double'
    and
    a warning that says: different types for formal and actual parameter 1

    What do these mean?

    Here is the part of the code that it applies to:

    Code:
    double f(double *ptr, double x, int degree)
    {
    	double sum = 0;
    	int i;
    	
    		for(i = 0; i <= degree; i++)
    		{	
    			sum = sum + ptr[i] * pow(x,i);
    			return(sum);
    
    		}
    
    
    }
    
    double rectangular(double *ptr, double a, double b, double n,int degree)
    {
    		double area = 0;
    		double totarea = 0;
    		double k = 0;
    		double width = (b - a) / n;
    		double widthplus = (b - a) / n;
    		double height = 0;
    
    		for(k = n; k > 0; k--)
    		{
    				for(degree = degree; degree >= 0; degree--)
    				{
    					if(degree != 0)
    					{
    						height = height + (pow(ptr[degree],degree));
    					}
    					else
    					{
    						height = height + (ptr[degree]);
    					}
    
    				}
    
    				area = height * width;
    				totarea = totarea + area;
    				widthplus = widthplus + width;
    
    		}
    		return(totarea);
    	}
    
    double simpsons(double *ptr, double a, double b, double n, int degree)
    {
    		
    	double answer = 0;
    	double h;
    	double x;
    	int i;
    
    		h = (b - a) / n;
    
    		answer = f(ptr,a,degree);
    			
    			for(i = 1; i <= n; i++)
    			{
    				x = a + i*h;
    				
    				answer = answer + 4 * f(ptr,x - (h/2),degree) + 2 * f(ptr,x,degree);
    				
    			}
    
    			answer = answer - f(ptr,b,degree);
    
    			return(h*answer/6);
    
    
    			
    
    }
    Thanks for the help.
    Last edited by Brent; 03-20-2006 at 08:55 PM.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Can you point to where the two things occur? The errors are simple, I just can't locate them

    The error means you are passing a pointer to a double to a function that is expecting a double, not a pointer

    The warning means you've defined one of your functions diferently then you've actually implemented it. I assume you've done that in a header file or maybe at the top of a source file, which isn't included in your post

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Ok I see what i did. Under the Simpsons sub function i was typing ptr and not *ptr


    Thanks

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Ok i have a new question. The program builds without error, however when run the program and use the following sub function it stops and tells me to debug. It tells me "Anexception 'System.NullReferenceException' has occurred inproject2.exe ( that is the name of my program) It then says"Additional information: Object reference not set to an instance of an object" When i choose to debug is points to the line that says sum = sum +....

    What do these mean?

    Code:
    double f(double *ptr, double x, int degree)
    {
    	double sum = 0;
    	int i;
    	
    		for(i = 0; i <= degree; i++)
    		{	
    			sum = sum + ptr[i] * pow(x,i);
    			return(sum);
    
    		}
    
    
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    This always looks suspect to me.
    Code:
    for(i = 0; i <= degree; i++)
    How is the array passed to these functions declared? Does it have one "extra" member? [edit]And what is the relation of degree to this array's size?
    Last edited by Dave_Sinkula; 03-20-2006 at 09:30 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.*

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Im sorry I dont understand the questions. Are you wanting to know where the array is declared? And what is an extra member?

    Sorry again.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    How.

    For example:
    Code:
    int array[10];
    The array may be indexed from 0-9. Therefore the usual idiom is like this.
    Code:
    for ( i = 0; i < 10; ++i )
    Using <= would go one beyond the end of the array.

    [edit]And ya know, returning in the middle of the for loop should have been obvious to me earlier, but I've been quite distracted. Should it ever be possible that your loop gets past the first iteration and makes it through the entire loop, you ought to return something.
    Last edited by Dave_Sinkula; 03-20-2006 at 10:22 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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Misra Rule 11.2
    By hammer1234 in forum C Programming
    Replies: 1
    Last Post: 04-06-2006, 08:28 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Rule of 72
    By sonict in forum C++ Programming
    Replies: 12
    Last Post: 01-23-2003, 08:31 PM
  4. Who should rule the world?
    By CoderBob in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-07-2002, 07:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21