-
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.
-
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
-
Ok I see what i did. Under the Simpsons sub function i was typing ptr and not *ptr
Thanks
-
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);
}
}
-
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?
-
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.
-
How.
For example: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.