# Thread: The Trapezoidal Rule for C programming C++ question. Anyone knows?

1. ## The Trapezoidal Rule for C programming C++ question. Anyone knows?

I am new to C programming. Anyone has the code for this problem below or how to do it?

I am trying to write a program in Putty that using Trapezoidal rule that reads a,b and N then calculates approx area for this function f(x)= 2x^2 +x

2. Read our Homework Policy. We help people find and repair bugs in their code, we don't hand out free code like candy. Attempt the problem yourself and then, if you get stuck, come back and we will help you.

3. Hi, I wrote that program but its wrong when I calculate it by hand. What else should I do? anything missing?
Code:
```/* trapezoid.c */include <stdio.h>include <math.h>float f(float);
float a; float b; float x; float h; float sum; int n; int i;
int main() {
printf("Enter value for a: "); scanf("%f", &a);
printf("Enter value for b: "); scanf("%f", &b);
printf("Enter number of rectangles: "); scanf("%d", &n);
h = (b - a) / n;
sum = (0.5 * h) * (f(a) + f(b)); printf("%f\n", sum);
for (i = 1; i < n; i++) {
sum = sum + h * f(a + (i * h)); printf("%f\n", sum);
}
printf("The value of the integral is: %f\n", sum);
}
float f(float x) {
float value;
/* define function here */ value = 2(x*x) + x;
return value;
}```
Whats wrong?

4. Originally Posted by TheBigH
we don't hand out free code like candy.
Maybe for money tho...

5. Okay, the first problem is your 2(x*x)+x. You need to stick another * between the 2 and the brackets.

6. What should a sample input and answer be?

7. Thats the only one? or I am wrong in the whole calculation?

8. well, I added the * between 2 and (x*x) and ran the program again. I am still not getting the answer. I entered 2 for a then 3 for b then 5 for n and I got the answer 14.68 but I have 15.something when I do it on calculator. Do you know whats my problem? Maybe I am doing wrong on calculator. idk

9. The answers looked reasonable to me. Tell us what input you gave, what output you get and what output you expect to get. We can't very well tell you what's wrong otherwise.

Also, I took the liberty of adding the fix TheBigH suggested and cleaning up some of your formatting so your code is a little more readable. Keep to one statement per line, and make sure subordinate lines of code are indented. Really, that goes a long way in spotting errors or potential errors (not that I happened to see any in this case, but for the future)
Code:
```/* trapezoid.c */
#include <stdio.h>

#include <math.h>

float f(float);
float a;
float b;
float x;
float h;
float sum;
int n;
int i;

int main() {
printf("Enter value for a: ");
scanf("%f", &a);
printf("Enter value for b: ");
scanf("%f", &b);
printf("Enter number of rectangles: ");
scanf("%d", &n);

h = (b - a) / n;
sum = (0.5 * h) * (f(a) + f(b));
printf("%f\n", sum);
for (i = 1; i < n; i++) {
sum = sum + h * f(a + (i * h));
printf("%f\n", sum);
}
printf("The value of the integral is: %f\n", sum);
}

float f(float x) {
float value;

value = 2*(x*x) + x;

return value;
}```
A few other notes:
• Avoid global variables whenever possible. You can (and should) move all of those variables inside main(). Read more about it: Global Variables Are Bad.
• You said main would return an int, do so. Zero is typical for success.
• What is the purpose of the line in red? That looks like area of a triangle, and inside your loop you add up the area of rectangles. I think you need to ditch the line in red and use the area of a trapezoid formula inside your loop, 0.5 * (b1 + b2) * h.

10. Originally Posted by kevave
well, I added the * between 2 and (x*x) and ran the program again. I am still not getting the answer. I entered 2 for a then 3 for b then 5 for n and I got the answer 14.68 but I have 15.something when I do it on calculator. Do you know whats my problem? Maybe I am doing wrong on calculator. idk
The trapezoid method, or whatever method you're using in your code, is just an approximation. Your calculator answer may be off a tiny bit. Try changing n to 5000 in your code and see what happens.