do not know how to finish integrating $\int_{-\pi}^\pi \lvert x\rvert \cos(nx) dx$ I started integrating this but do not know how to finish it, here is what I got up to:
$$\int_{-\pi}^\pi \lvert x\rvert \cos(nx) dx$$ 
for $n=1, 2, 3, ...$
the first thing I did was break it up into two integrals:
$$\int_{-\pi}^0 -x \cos(nx) dx + \int_0^\pi x \cos(nx) dx$$
I began to solve the first integral by using integration by parts and got:
$$-x\cos(nx)-\int_{-\pi}^0 -\sin(nx)dx$$
after integrating $-\sin(nx)dx$ I get $-x\cos(nx)-\cos(nx)$, my first question is do I plug in the bounds $-\pi$ and $0$ for every $x$ or just for the second part ($-\cos(nx)$)?
and my second question is how do I represent the answer after I plug in $-\pi$ and $0$ for $x$ because $n=1, 2, 3, ...$, $\cos$ would be alternating between $-1$ and $1$?
I think the integral that goes from $0$ to $\pi$ would be solved in the same manner, so I just need help with the first ne.
 A: Since as @zhw pointed out, $|x|\cos(nx)$ is an even function, you get
\begin{align*}
  \int_{-\pi}^\pi \lvert x\rvert \cos(nx) dx
      &= 2\int_0^\pi x\cos(nx)\,dx \\
      &= 2\left(\frac{1}{n}x\sin nx\bigg\lvert_0^\pi 
              - \frac{1}{n}\int_0^{\pi}\sin nx\,dx\right) \\
      &= 2\left(0 - \frac{1}{n}\left(-\frac{1}{n}\cos nx\big\lvert_0^{\pi}\right)
            \right) \\
      &= \frac{2}{n^2}\cos nx\big\lvert_0^{\pi} \\
      &= \frac{2(\cos n\pi - 1)}{n^2} \\
      &= \frac{2((-1)^n-1)}{n^2}.
\end{align*}
A: $$\int_{-\pi}^\pi \lvert x\rvert \cos(nx) dx$$ 
$$ = \int_{-\pi}^0 -x \cos(nx) dx + \int_0^\pi x \cos(nx) dx$$
Let $u = - x$ in the left integral to get:
$$ = \int_{\pi}^0 - u \cos(-nu) du + \int_0^\pi x \cos(nx) dx$$
$$ = \int_0^{\pi} u \cos(-nu) du + \int_0^\pi x \cos(nx) dx$$
$$ = \int_0^{\pi} x \cos(-nx) dx + \int_0^\pi x \cos(nx) dx$$
$$ = \int_0^{\pi} x \cos(nx) dx + \int_0^\pi x \cos(nx) dx$$
$$ = 2\int_0^{\pi} x \cos(nx) dx$$
Now integrate by parts:
$u = x, dv = \cos(nx) dx$
Can you take it from here?
