C++ vector for each loop auto type

Description

C++ vector for each loop auto type

#include <iostream>
#include <cmath>
#include <vector>

int main()// w  ww  .  j  a v a2  s  .  com
{
  int n {};
  std::cout << "Enter the number of vector elements: ";
  std::cin >> n;
  auto values = new std::vector<double>(n);

  for (unsigned long long i {}; i < n; ++i)
    (*values)[static_cast<int>(i)] = 1.0 / ((i + 1)*(i + 1));

  double sum {};

  for (auto value : *(values))
    sum += value;

  std::cout << "Result is " << std::sqrt(6.0*sum) << std::endl;

  delete values;// It's not an array this time!
}



PreviousNext

Related