Writing a program using the trapezoidal rule 
Write a program to evaluate $I=\int_a^bf(x)dx$ using the trapezoidal
  rule with $n$ subdivisions, calling the result $I_n$. Use the program
  to calculate the following integrals with $n=2,4,8,16,32,64,128,256$.
  Analyze empirically the rate of convergences of $I_n$ to $I$ by
  calculating the ratios $R_n=\frac{I_{2n}-I_n}{I_{4n}-I_{2n}}$.
a.$\int_0^{2\pi}\frac{dx}{2+cos(x)}$
b.$\int_{-4}^4\frac{dx}{1+x^2}$
c.$\int_0^1x^{5/2}dx$

My attempt at a pseudo-code:
Initialize $1-$dimensional arrays $I(8)$ and $R(6)$.
input function $\int_a^b f(x)$ 
for $m=1$ to $8$
$n = 2^m$
$h = \frac{(b - a)}{n}$
$t = f(a) + f(b)$
for $x = a + h$ to $b - h/2$ step $h$
$t = t + 2*f(x)$
next $x$
$t = t*(h/2)$
$I(m)=t$
next $m$
for $r = 1$ to $6$
$R(r)= (I(r+1) - I(r))/(I(r+2) - I(r+1))$
next $r$
Display values in arrays $I$ and $R$.
 A: Lets work an example of the Trapezoidal Rule algorithm.
Once you have working code, adding larger sample sizes (increasing $n$) is very easy, but lets first work out an example. 
Using the Trapezoidal rule, with $n = 4$, we have:
$\displaystyle a = 0, b = 2 \pi ~~\text{and}~~ f(x) = \frac{1}{2 + \cos x}$, so for $\displaystyle n = 4 \Rightarrow h = \frac{b-a}{n} = \frac{2 \pi}{4} = \frac{\pi}{2} ~~\text{and}~~ x_i = a + ih$
$$\begin{array}{c|c|c}
\text{i} & \text{0} & \text{1} & \text{2} & \text{3} & \text{4}\\ 
\hline
\\x_i & 0 & \frac{\pi}{2} & \frac{\pi}{1} & \frac{3 \pi}{2} & \frac{2 \pi}{1}
\\f(x_i) & 0.333333 & 0.5 & 1 & 0.5 & 0.333333
\end{array}$$
So,
$\displaystyle \int_0^{2\pi}\frac{dx}{2+cos(x)} \approx \frac{h}{2}[f(x_0) + 2f(x_1) + 2f(x_2) + 2f(x_3) + f(x_4)] = \frac{\pi}{2 \times 2}[0.333333 + 1 + 2 + 1 + 0.333333] = 3.665191.$
Using WA, we get:
$$\int_0^{2\pi}\frac{dx}{2+cos(x)} = 3.6275987 $$
If you take more samples, the error should settle and you'll get better approximations.
There are some working code snippets on Wiki as this is quite an easy program to implement.
Once you have working code, you can compare to this online calculator for the various $n$ values.
Does that all make sense?
Update
Here is a nice write-up on error calculations. For your question on $R_n$, all you are doing is filling out a row of calculations at a time and keeping an error estimate at each step and you actually documented this in your algorithm. It is Just comparing your calculated value to the actual value ($I_n$ to $I$), using the formula you gave.
Have fun!
