Storing double type numbers in a vector - C++ STL

C++ examples for STL:vector

Description

Storing double type numbers in a vector

Demo Code

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

int main()//  w w  w .  ja v  a 2  s  . c o  m
{
  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!
}

Result


Related Tutorials