This example or exercise comes from a book, but it is only available in Polish which is why I don't provide link to the same. Here is the description of the problem.
Description
The problem is to approximate a square function $f(t)$ with a period is $T$ and amplitude $A$. Function preview
(1) We approximate using the function $g(t)=C \sin(w_0t)$, where $C$ is unknown.
The error in this approximation is described as below:
(2) $\displaystyle\delta(C) = \frac{1}{T}\int_{\frac{-T}{2}}^{\frac{T}{2}}[f(t)-g(t)]^2dt= \frac{2}{T}\int_0^{\frac{T}{2}}[A-C \sin(\omega_0t)]^2 \ \mathrm{d}t$.
We are looking for minimal error along variable $C$ for which we calculate the derivative and equate it to $0$.
(3) $\displaystyle \frac{\mathrm{d}}{\mathrm{d} C} \delta(C) = -\frac{4}{T}\int_{0}^{\frac{T}{2}}[A-C\sin(\omega_0t)]\sin(\omega_0t) \ \mathrm{d}t = 0$
And now the part that I am confused with. They came up with the following formula for $C$.
(4) $\displaystyle C = \frac{A\int_0^{\frac{T}{2}}\sin(\omega_0t)dt} {\int_0^{\frac{T}{2}}\sin^2(\omega_0t)dt}$
Question
Question: How did they get formula (4) ? Was right side differentiated in step (3) or result of it is in step 4?
Extra question: Why there is $\frac{1}{T}$ in first equation? I guess it is just a trick to reduce $T$ later?
Answer
Evaluation of (4) gives the result $\displaystyle C = \frac{2A/\omega_0}{T/4} = \frac{4A}{\pi}$.
This follows from the fact that integration is linear. In particular, we can break up the integral at the minus sign and factor out constants. Here ar the details: \begin{align*} -\frac{4}{T} \int _0^{T/2}[A-C\sin(\omega_0 t)]\sin(\omega_0 t)dt &=0 \\ \int _0^{T/2}[A-C\sin(\omega_0 t)]\sin(\omega_0 t)dt &=0 \\ \int _0^{T/2}[A\sin(\omega_0 t)-C\sin^2(\omega_0 t)]dt &=0 \\ \int _0^{T/2}A\sin(\omega_0 t) dt- \int _0^{T/2}C\sin^2(\omega_0 t)dt &=0 \\ \int _0^{T/2}A\sin(\omega_0 t) dt &= \int _0^{T/2}C\sin^2(\omega_0 t)dt\\ A\int _0^{T/2}\sin(\omega_0 t) dt &= C\int _0^{T/2}\sin^2(\omega_0 t)dt\\ \frac{A\int _0^{T/2}\sin(\omega_0 t)dt}{\int _0^{T/2}\sin^2(\omega_0 t)dt} &= C\\ \end{align*}