Computing mean and median I have written a program to compute mean and median. Please review my code and give suggestions.

I have a confusion about this temps[temps.size()/2] when temps.size() is an odd number. How does the "vector::operator[]" handle floating point numbers? From the behavior in my code, I guessed it rounds to the nearest integer.  

Note: I am self learning programming by reading "Programming: Principles, and Practice" by Bjarne Stroustrup. Currently, I am on chapter 4.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

int main()
{
    std::vector&lt;double&gt; temps;          // temperatures
    for (double temp; std::cin&gt;&gt;temp; ) // read into temp
        temps.push_back(temp);      // put temp into vector

    // Compute Mean temperature:
    double sum = 0;
    for (int x : temps) sum += x;
    std::cout &lt;&lt; "Average temperature: " &lt;&lt; sum/temps.size() &lt;&lt; '\n';

    // sort temperatures
    std::sort(temps.begin(),temps.end());

    // Compute Median temperature
    if(temps.size()%2==1) //Number of elements are odd
    {
        std::cout &lt;&lt; "Median temperature: " &lt;&lt; temps[temps.size()/2] &lt;&lt; '\n';
    }
    else // Number of elements are even
    {
        int index = temps.size()/2;
        std::cout&lt;&lt; "Median temperature: " &lt;&lt; (temps[index-1] + temps[index])/2;
    }

}
I have a confusion about this temps[temps.size()/2] when temps.size() is an odd number. How does the "vector::operator[]" handle floating point numbers?
  


There are well defined rules to implicit conversions.  temps.size() returns an unsigned integral type that represents the total count of elements in the container.  Dividing an unsigned integral by 2 (a signed integral), the implicit conversion rules for mixed expressions promotes the signed 2 to be unsigned. Division between two unsigned integrals results in an unsigned integral and any remaining fractional part is truncated.  So \$3/2 = 1\$ and the remaining \$.5\$ is truncated and discarded.



    std::vector&lt;double&gt; temps;
    for (double temp; std::cin&gt;&gt;temp; )
        temps.push_back(temp);
    // temps = ???


Be aware of the possible states a type may represent.  What happens if someone runs the program and inputs no data?  Boom.  The mean cannot be calculated because division-by-zero is undefined behavior.  The median cannot be calculated as an empty set is consider an even-sized set.  temps[0-1] accesses results in an out-of-bound access, which is also undefined behavior.  Explicitly assert/validate your pre- and post-conditions.  Do not rely on assumptions.



    for (int x : temps) sum += x;


Use auto to avoid redundant repetition of type names.  temps represents a container of doubles.  You define x to be of type int, which results in a narrowing conversion that's likely to be a source of a bug (Test! Test! Test!).  The compiler already knows what type x should be.  Use auto and let the compiler do its job.



Don't state in comments what can be clearly stated through code.  When programming, the code states what is being done.  If you need to provide/document any reasoning regarding your intent, use comments then.

Write functions. Lots of functions.  You've already split your program into single logical operations via comments.  Break up that monolithic main into parameterized functions.

int main() {
    auto temperatures = read_temperatures(std::cin);
    const auto mean_temperature = mean(temperatures);
    const auto median_temperature = median(temperatures);

    report(std::cout, mean_temperature, median_temperature);
}


Now you can write the short and simple functions that are easy to read, test, and maintain.

Get familiar with the standard algorithms found in &lt;algorithm&gt; and &lt;numeric&gt;.  Do you really need to sort the entire data set?  Would partial sorting (std::nth_element) suffice?  For even-sized data sets, you could pair up std::nth_element with std::min_element to find the two values. As Vnp mentioned, std::accumulate is a candidate for summing up a range.



Use strongly defined types to represent your temperatures.  A raw double representing temperatures is not safe.  Numeric types that are conceptually incompatible may cause catastrophic mathematical errors.  The Mars Climate Orbiter failure is a famous catastrophe caused by taking an impulse result from the US system and using that result in SI calculations without any conversions.  So if you want to store temperatures, are you representing Celsius, Fahrenheit, Kelvin, or Rankine?  Note - You'll need an understanding of programming with objects to accomplish this.  If you haven't learned about objects yet, keep it in mind for when you do encounter them.