Don't understand why this doesn't work

This is supposed to be the trapezoidal rule, approximating the integral of 1/x from 1 to 2. I don't understand why it returns the wrong values. Help please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <conio>
#include <math>

main(){

gotoxy(1,1);
cout<<"k";
gotoxy(10,1);
cout<<"T_k";

float a=1,b=2,sum0,sum0a,t0;

do{
sum0a=0;
sum0=(1/a)+(1/(a+1));
sum0a=sum0a+sum0;
a=a+1;
}while(a<b);
t0=(.5)*(1)*(sum0a);
gotoxy(1,2);
cout<<"0";
gotoxy(10,2);
cout<<t0;

do{
sum0=(1/a)+(1/(a+.5));
sum0a=sum0a+sum0;
a=a+.5;
}while(a<b);
t0=(.5)*(.5)*(sum0a);
gotoxy(1,3);
cout<<"1";
gotoxy(10,3);
cout<<t0;

do{
sum0=(1/a)+(1/(a+.25));
sum0a=sum0a+sum0;
a=a+.25;
}while(a<b);
t0=(.5)*(.25)*(sum0a);
gotoxy(1,4);
cout<<"2";
gotoxy(10,4);
cout<<t0;

do{
sum0=(1/a)+(1/(a+.125));
sum0a=sum0a+sum0;
a=a+.125;
}while(a<b);
t0=(.5)*(.125)*(sum0a);
gotoxy(1,5);
cout<<"3";
gotoxy(10,5);
cout<<t0;

do{
sum0=(1/a)+(1/(a+.0625));
sum0a=sum0a+sum0;
a=a+.0625;
}while(a<b);
t0=(.5)*(.0625)*(sum0a);
gotoxy(1,6);
cout<<"4";
gotoxy(10,6);
cout<<t0;

getch();
return 0;
}
Topic archived. No new replies allowed.