# Thread: Why my code is not working? Anyone can look at my code plz?!

1. ## Why my code is not working? Anyone can look at my code plz?!

I am trying to use the Simpson's Method to calculate area under the curve using this function 2x^2+x but its not working?

Can some one help me and fix the problem? Its using Simpson's Method and want user to input a,b and N then calculate the area of the function given 2x^2+x

insert
Code:
```#include <stdio.h>
#include <conio.h>
#include <cstdlib>

double simpson(double a, double b, int n);
double fk(double x);
//**********************************
int main()
{

double a,f,b;
int n;
//Duomenys----------------
printf("Enter value of a, example a=0\n a:");
scanf("%lf", &a);
//------------------------
printf("Enter value of n, example n=7\n n:");
scanf("%d",&n);
printf ("Enter Value of b, example b=1\nb:");
scanf("%lf", &b);
f=simpson(a ,b , n); // Integralas
//Rezultatas----------------
printf("The result is f=%7.2f\n",f);

}
//**********************************
double simpson(double a, double b, int n)
{
double c= (a+b)/2.0;
double h3= abs(b-a)/6.0;
double result= h3*(fk(a)+4.0*fk(c)+fk(b));
return result;

}
//**********************************
double fk(double x)
{
return x * x;
}```

2. What isn't working about it?

Quzah.

3. Code:
```double fk(double x)
{
return x * x;
}```
That is not the function 2x^2 + x. It is simply x^2.

4. For doubles, use %lf, instead of %f. %f is for floats, not doubles.

For doubles, use %lf, instead of %f. %f is for floats, not doubles.
That is true for the scanf family of functions, which the OP is using correctly. It is not true for the printf family (which the OP is also using correctly), however. For printf, %f is for doubles, and the compiler will promote a float as needed. The l (ell) in %lf for printf has no effect.

6. ## Why my code is not working? Anyone can look at my code plz?!

I fixed it again and its still not working. ;(

Code:
```#include <stdio.h>
#include <conio.h>
#include <cstdlib>

double simpson(double a, double b, int n);
double fk(double x);
//**********************************
int main()
{

double a,f,b;
int n;
//Duomenys----------------
printf("Enter value of a, example a=0\n a:");
scanf("%lf", &a);
//------------------------
printf("Enter value of n, example n=7\n n:");
scanf("%d",&n);
printf ("Enter Value of b, example b=1\nb:");
scanf("%lf", &b);
f=simpson(a ,b , n); // Integralas
//Rezultatas----------------
printf("The result is f=%7.2f\n",f);

}
//**********************************
double simpson(double a, double b, int n)
{
double c= (a+b)/2.0;
double h3= abs(b-a)/6.0;
double result= h3*(fk(a)+4.0*fk(c)+fk(b));
return result;

}
//**********************************
double fk(double x)
{
return 2x * x+x;
}```

7. Did you even try to compile that code? It doesn't work. Getting rid of your bogus conio.h and cstdlib includes, I get the following when I compile:
Code:
```\$ gcc -Wall -o simpson simpson.c
simpson.c: In function ‘simpson’:
simpson.c:30: warning: implicit declaration of function ‘abs’
simpson.c:40:8: error: invalid suffix "x" on integer constant```
Line 30: abs is defined in stdlib.h, so you need a #include <stdlib.h> at the top
Line 40: You can't multiply by sticking a number and variable next to each other. You need a * between them: 2 * x * x + x;

Also, I don't see any use of absolute value in the Wikipedia page for Simpson's Rule, so I'm not sure whether you should use it in your code. Actually, it seems you shouldn't, since if I integrate by hand from 2 to 4, and from 4 to 2, I should get answers with the same magnitude but different signs.

8. Originally Posted by kevave
I fixed it again and its still not working. ;(
You still aren't explaining what doesn't work. Why should we help you when you don't bother to help us?

Quzah.

9. Originally Posted by anduril462
That is true for the scanf family of functions, which the OP is using correctly. It is not true for the printf family (which the OP is also using correctly), however. For printf, %f is for doubles, and the compiler will promote a float as needed. The l (ell) in %lf for printf has no effect.
Thanks!

Old habit.