https://rentry.org/PPP2_p122b

// Code derived from Stroustrup's PPP2 book
// § 4.6.3 A numeric example
//  -and beginning on p 122

#include <algorithm>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::vector;

template <typename C>
void sort(C& c)
{
  std::sort(begin(c), end(c));
}

// compute mean and median temperatures
int main()
{
  cout << "Please enter temperatures: ";
  vector<double> temps;            // temperatures
  for (double temp; cin >> temp;)  // read into temp
    temps.push_back(temp);         // put temp into vector

  // compute mean temperature:

  double sum = 0;
  for (double x : temps)
    sum += x;

  cout << "Average temperature: "
       << ((temps.size() > 0) ? (sum / temps.size()) : 0) << '\n';

  // compute median temperature:

  sort(temps);  // sort temperatures

  cout << "Median temperature: "
       << ((temps.size() > 0) ? (temps[temps.size() / 2]) : 0) << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p122b.cpp && ./a.out

PrevUpNext

Edit
Pub: 08 Feb 2023 02:24 UTC
Edit: 29 Apr 2023 08:45 UTC
Views: 372