C++ sizeof operator Obtaining the number of array elements

Description

C++ sizeof operator Obtaining the number of array elements

#include <iostream>

int main()/*w ww  . ja v a2s  .  co  m*/
{
  int values[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
  std::cout << "There are " << sizeof (values)/sizeof(values[0])
            << " elements in the array." << std::endl;

  int sum {};
  for(int i {} ; i < sizeof (values)/sizeof (values[0]) ; ++i)
  {
    sum += values[i];
  }
  std::cout << "The sum of the array elements is " << sum << std::endl;
}



PreviousNext

Related