I'm assuming here that the cylinder is "infinitely long", or at least very long so that $h >> r$. Otherwise, there are complicated non-integrable "end effects", but it doesn't look like you're interested in those.
As you said, you need Gauss's law. You're losing something important if you drop the vector notation, though, so I've added it back in.
$$
\phi_E=\int_S \vec{E} \cdot d\vec{A}= \frac Q{\epsilon_0}
$$
The critical part, which you've already done, is to choose a surface on which $E$ is constant, so the integral is easy to evaluate.
You should pick a cylindrical surface of radius $R$ and height $L$, centered on the axis of the charged cylinder. An illustration of that surface (in green) is shown here:![enter image description here](https://i.stack.imgur.com/dC013.png)
With that choice of surface, simply by symmetry, the $E$ field must have the same value, and the same direction relative to the normal vector $d\vec{A}$, everywhere on the curved part of the surface!
On the flat ends of your cylindrical surface, the $E$ field is not constant, but it is parallel to the surface, and therefore perpendicular to the surface vector $d\vec{A}$, so the dot product $\vec{E} \cdot d\vec{A}$ is zero on those surfaces, and we can ignore them in the integral.
Here I've split the integral into the two parts - the integral over the curved part of the surface, and the flat ends, and I've evaluated both parts of the integral.
$$\int_{curved} \vec{E} \cdot d\vec{A} + \int_{ends} \vec{E} \cdot d\vec{A}= \frac Q{\epsilon_0}$$
$$\int_{curved} E dA + 0= \frac Q{\epsilon_0}$$
$$E \int_{curved} dA= \frac Q{\epsilon_0}$$
$$E ~A_{curved} = \frac Q{\epsilon_0}$$
$$E ~2 \pi R L = \frac Q{\epsilon_0}$$
Notice that on the curved part, since $\vec{E}$ and $d\vec{A}$ are in the same direction, their dot product is simply $E dA$, and since the magnitude $E$ is the same everywhere, we can remove it from the integral as a constant. Then we're simply integrating $dA$, which simply gives us the area of that part of the surface.
The last job we have is to find how much charge ($Q$) is inside our surface. The charge density is $\sigma = \frac{q}{2 \pi r h}$
There are two cases.
- If $R > r$, then the $Q = 2 \pi r L \sigma = q \frac{L}{h}$.
- If $R < r$, then no charge is inside our surface, so $Q = 0$.
So, using our final version of Gauss's law:
$$E ~2 \pi R L = \frac Q{\epsilon_0}$$
$$E = \frac{Q}{2 \pi \epsilon_0 R L}$$
And our final answer is:
$R<r$;$$E(R) = 0$$
$R>r$;$$E(R) = \frac{q}{2 \pi \epsilon_0 R h}$$
First of all, let's see what Gauss's divergence theorem tells: the outward flux of a vector field through a closed surface is equal to the volume integral of the divergence over the region inside the surface. Intuitively, it states that the sum of all sources minus the sum of all sinks gives the net flow out of a region.
Now, let's look at the Gauss's law in electrostatics:
In differential form, it reads
$$\nabla\cdot\vec{E}=\frac{\rho_{enc}}{\epsilon_0}$$
This means the net outward flux of the electric field lines normal to the surface enclosing the charge is equal to the net charge enclosed by the surface. On integrating the above equation over a spherical volume enclosing the charge,
$$\int_V \nabla\cdot\vec{E}d\tau'=\frac{1}{\epsilon_0}\int_V \rho_{enc}d\tau'$$
By Gauss's divergence theorem, this volume integral of $\vec{E}$ is equal to the outward flux of $\vec{E}$ throgh a closed surface enclosing the charge:
$$\int_V \nabla\cdot\vec{E}d\tau'=\int_{\sigma}\vec{E}\cdot d\vec{\sigma}$$
Hence
$$\int_{\sigma}\vec{E}\cdot d\vec{\sigma}=\frac{1}{\epsilon_0}\int_V \rho_{enc}d\tau'=\frac{q_{enc}}{\epsilon_0}$$
where we have assumed that the volume charge density is continuous and constant. This is Gauss's law in integral form.
So, to use Gauss's law, you should choose the integrating region to be a surface that encloses the charge.
Now, let's look at your problem.
To find the electric field at some point outside the sphere of radius $R$:
We have
$$\int_{\sigma}\vec{E}\cdot d\vec{\sigma}=\frac{q_{enc}}{\epsilon_0}$$
where the integration is over a Gaussian spherical surface enclosing the charged sphere of radius $r$ such that $r>R$ Since the electric field is symmetrical about a spherical surface, we can take it out of the integral. Also the electric field pointing outside the surface is in the same direction as area vector of the spherical surface. Taking $q_{enc}=q$, the total charge enclosed by the charged sphere:
$$E\int_{\sigma}d\sigma=E.4\pi r^2=\frac{q}{\epsilon_0}$$
$$E=\frac{1}{4\pi\epsilon_0}\frac{q}{r^2}$$
In particular, at the surface ($r=R$),
$$E=\frac{1}{4\pi\epsilon_0}\frac{q}{R^2}$$
To find the electric field at some point inside the sphere of radius $R$:
Here our Gaussian sphere is inside the charged sphere. i.e., $r<R$. Since the Gaussian surface is inside the charged sphere, our Gaussian surface do not enclose the entire charge distribution, but only less. Hence the electric field at any point inside the charged sphere will be less than that at the surface.
First we need to find out what is $q_{enc}$.
$$q_{enc}=\int_0^r \rho d\tau'=q\left(\frac{r}{R}\right)^3$$.
Hence the electric field inside the sphere is
$$E.4\pi r^2=\frac{1}{\epsilon_0}q\left(\frac{r}{R}\right)^3$$
$$E=\frac{1}{4\pi\epsilon_0}\frac{qr}{R^3} $$
So,
$$
\vec{E}=
\begin{cases}
\Large{\frac{1}{4\pi\epsilon_0}\frac{q}{r^2}\hat{r}}, &\text{if $r>R$; outside the charged sphere}\\[2ex]
\Large{\frac{1}{4\pi\epsilon_0}\frac{q}{R^2}\hat{r}}, &\text{if $r=R$; on the charged sphere}\\[2ex]
\Large{\frac{1}{4\pi\epsilon_0}\frac{qr}{R^3}\hat{r}}, &\text{if $r<R$; inside the charged sphere}
\end{cases}
$$
Best Answer
Your solution is correct, except that you get the magnitude of the electric field only.
That $R$ does not occur, I would argue as follows: