C++ new operator Storing numbers in a dynamic array

Description

C++ new operator Storing numbers in a dynamic array

#include <iostream>
#include <cmath>

int main()/*from ww w. ja v  a2  s . co m*/
{
  int n = 0 ;
  std::cout << "Enter the number of array elements: ";
  std::cin >> n;

  auto values = new (double[n]);

  for (int i = 0; i < n; ++i)
    *(values+i) = 1.0 / ((i + 1)*(i + 1));

  double sum {};

  for (int i {}; i < n; ++i)
    sum += *(values + i);

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

  delete[] values;// Don't forget to free the memory!
}



PreviousNext

Related