# Question about compact set with a function attains its extreme values

I want to prove the following:

Let $(X,d)$ be a compact metric space , and let $f:X \rightarrow \mathbf{R}$ be a continuous function. Then, there $f$ is bounded. Furthermore, $f$ attains its maximum at some point $x_{max} \in X$, and also attain its minimum at some points $x_{min} \in X.$

Let $(X,d)$ be a compact metric space. let $f:X \rightarrow \mathbf{R}$ be a continuous function.
Since $f$ is compact, $f(X)$ is compact.Now, We have that $(\mathbf{R},d)$ is a metric space, $d$ is the usual metric, and therefore by Hiene- Borel Theorem $f(X)$ is closed and bounded since it $X$ is compact. Hence, it is bounded.

I am struggling with proving that $f$ attains its extreme values. What I have done is, since $X$ is compact and $(X,d)$ is a metric space, and $X \subseteq X$ $\Rightarrow$ $X$ is closed and bounded.

So the question that I have is can we write $X$ as a closed interval?, and so it will clearly follow that $f$ attains its maximum and min. If we can not how to finish this prove.

Thank you in advance for any help.

$f(X)$ isn't necessarily an interval, because $X$ is not necessarily connected. However, since it's bounded (and non-empty, I presume), it has a supremum, and since it's closed it contains its supremum. Same with infimum.