Complex Integration, residues 
Evaluate the following integrals by the method of residues
i)$\int_0^{\infty}\frac{\cos x}{x^2+a^2}dx$, a real
ii)$\int_0^\infty \frac{x^\frac{1}{3}}{1+x^2}dx$

I'm a little lost to calculate integrals when they are improper, in my book there are some formulas, but I believe that there should be a general method to calculate them. Simplest integral I managed to solve these but I am not getting at all.
 A: For the second integral, we first cut the plane with a branch extending on the positive real axis and analyze the integral
$$I=\oint_C\frac{z^{1/3}}{1+z^2}dz$$
where $C$ is the "key-hole" contour formed by 
$(1)$ $C_{+}$, a path along the positive real-axis above the branch cut from $x=0$ to $x=R$, 
$(2)$ $C_{R}$, the circle $|z|=R$, 
$(3)$ $C_{-}$, a path along the positive real-axis below the branch cut from $x=R$ to $x=0$, and 
$(4)$ $C_{\epsilon}$, a semi-cirlce $|z|=\epsilon$ around the branch point.
We see that the contribution from $C_{R}$ vanishes as $R\to \infty$ since $\left|\frac{z^{1/3}}{1+z^2}dz\right| =O(R^{-2/3})$ as $R\to \infty$.  We also see that the contribution from $C_{\epsilon}$ vanishes as $\epsilon \to 0$ since $\left|\frac{z^{1/3}}{1+z^2}dz\right| =O(\epsilon^{4/3})$ as $\epsilon \to 0$.
Thus, we have 
$$\begin{align}
I&=\int_{C_+}\frac{z^{1/3}}{1+z^2}dz+\int_{C-}\frac{z^{1/3}}{1+z^2}dz\\\\
&=\int_0^{\infty} \frac{x^{1/3}}{1+x^2}dx+\int_{\infty}^0 \frac{e^{i2\pi/3}x^{1/3}}{1+x^2}dx\\\\
&=(1-e^{i2\pi/3})\int_0^{\infty} \frac{x^{1/3}}{1+x^2}dx\\\\
&=-2ie^{\pi/3}\sin(\pi/3)\int_0^{\infty} \frac{x^{1/3}}{1+x^2}dx
\end{align}$$
We also have from the residue theorem
$$\begin{align}
I&=2\pi i \left( \text{Res}\left(\frac{z^{1/3}}{1+z^2}, z=i\right)+\text{Res}\left(\frac{z^{1/3}}{1+z^2}, z=-i\right)\right)\\\\
&=\pi(e^{i\pi/6}-e^{i\pi/2})\\\\
&=-2i\pi e^{i\pi/3}\sin(\pi/6)
\end{align}$$
Thus, we finally have 
$$\int_0^{\infty} \frac{x^{1/3}}{1+x^2}dx=\pi \sin(\pi/6)/\sin(\pi/3)=\frac{\sqrt{3}\pi}{3}$$
A: See last page of Cauchy Principal Value
This technique extend the real integral into a contour integral on the complex plane. 
The idea is basically: real integral = complex integral on the real line + complex integral on the contour (that vanishes)
 
If you can show that the complex integral evaluates to zero on the contour (using  residue or cauchy integral - same thing), then what is left is the integral on the real line which precisely equal to real integral you need to evaluate. The integral on the real line is called Cauchy principal value. This technique works for both of your integrals but slightly harder for the first one.
You also need to divide by 2 after evaluating the integrals because your integral only covers half of the real line.
A: For integral i):
$$\int_0^\infty \frac{\cos x}{x^2+a^2}=\Re\left[\frac 12\int_{-\infty}^\infty \frac{e^{ix}}{(x-ia)(x+ia)}\right]$$
We may choose the semicircular contour, $\Gamma$ from $-\infty$ to $\infty$, and also a line segment connecting those points. The line segment is what we want to evaluate. Therefore we have $I=\int_\Gamma -\int_{arc}$. We can prove the integral vanishes along the arc, i.e., it equals zero, and then use the residue theorem to evaluate $\int_\Gamma$.
