Define g(x) as a function of $x$ Let $f(x)= \begin{cases} 
      -1 & ,-2\leq x\leq 0 \\
       \\
      |x-1| & ,0<x\leq2 
   \end{cases}$
and $g(x)=\int_{-2}^{x}f(t) dt$.Define g(x) as a function of $x$.
I tried to solve it.I redefined $f(x)= \begin{cases} 
      -1 & ,-2\leq x\leq 0 \\
      1-x & ,0<x<1      \\
      x-1 & ,1\leq x\leq2
   \end{cases}$
$g(x)=\int_{-2}^{x}f(t) dt=\begin{cases} 
      \int_{-2}^{x}-1dx & ,-2\leq x\leq 0 \\
      \int_{-2}^{x}(1-x)dx & ,0<x<1      \\
      \int_{-2}^{x}(x-1)dx & ,1\leq x\leq2
   \end{cases}$
$g(x)= \begin{cases} 
      -(x+2) & ,-2\leq x\leq 0 \\
      x-\frac{x^2}{2}+4 & ,0<x<1      \\
      \frac{x^2}{2}-x-4 & ,1\leq x\leq2
   \end{cases}$
But the answer is given to be
$g(x)= \begin{cases} 
      -(x+2) & ,-2\leq x\leq 0 \\
      x-\frac{x^2}{2}-2 & ,0<x<1      \\
      \frac{x^2}{2}-x-1 & ,1\leq x\leq2
   \end{cases}$
Have i done it wrong,what should be the correct method.Please guide me. 
 A: For $0<x<1$ we have
\begin{align*}
g(x)&=\int_{-2}^{x}f(t)dt\\
&=\int_{-2}^0f(t)dt+\int_0^xf(t)dt\\
&=\int_{-2}^0(-1)dt+\int_0^x(1-t)dt\\
&=(-1)(0+2)+x-\frac{1}{2}x^2-0\\
g(x)&=-\frac{1}{2}x^2+x-2,\qquad \text{ for }\;\;0<x<1
\end{align*}
On the other hand, for $1\le x\le 2$ it follows
\begin{align*}
g(x)&=\int_{-2}^{x}f(t)dt\\
&=\int_{-2}^0f(t)dt+\int_0^1f(t)dt+\int_1^xf(t)dt\\
&=\int_{-2}^0(-1)dt+\int_0^1(1-t)dt+\int_1^x(t-1)dt\\
&=(-1)(0+2)+1-\frac{1}{2}(1)^2-0+\frac{1}{2}x^2-x-\left[\frac{1}{2}-1\right]\\
g(x)&=\frac{1}{2}x^2-x-1,\qquad \text{ for }\;\;1\le x\le 2
\end{align*}
Thus
\begin{equation*}
\color{blue}{g(x)=
\begin{cases}
-(x+2)&-2\le x\le 0\\
-\frac{1}{2}x^2+x-2& 0<x<1\\
\frac{1}{2}x^2-x-1& 1\le x\le 2
\end{cases}
}
\end{equation*}
A: Note that
$$g(1) = \int_{-2}^1 f(x)\ dx = \int_{-2}^0 f(x)\ dx + \int_{0}^1 f(x)\ dx \neq \int_{-2}^1 (1-x)\ dx$$
A: In a slightly different approach from the direct, and brute force one, we recognize that inasmuch as $g$ is differentiable, it is, of course, continuous.  Then, we have that 
$$
g(x)=
\begin{cases}
-x+C_1 ,&x<0\\\\
x-\frac12x^2+C_2 ,&0<x<1\\\\
\frac12x^2-x+C_3 ,&1<x
\end{cases}
$$
Now, exploiting the continuity of $g$ is as simple as One, Two, Three.
One, we enforce $g(-2)=0$, which reveals immediately that $C_1=-2$.  
Two, we enforce continuity of $g$ at $x=0$.  This shows that $C_2=C_1$ and thus $C_2=-2$ also.
Three, we enforce continuity of $g$ at $x=1$.  This shows that $\frac12+C_2=-\frac12+C_3$, which implies that $C_3=-1$.
Putting it together gives 
$$
g(x)=
\begin{cases}
-(x+2) ,&x<0\\\\
-\frac12x^2+x-2 ,&0<x<1\\\\
\frac12x^2-x-1 ,&1<x
\end{cases}
$$
as expected!
