std::min, std::max, and std::accumulate would be the fancy C++ way of doing it, but I gather you need to learn the fundamentals of programming itself rather than how to use the C++ standard library....