Obtaining the number of array elements - C++ Statement

C++ examples for Statement:for

Description

Obtaining the number of array elements

Demo Code

#include <iostream>

int main()//from   w ww .j  ava 2s  . c om
{
  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(unsigned int i {} ; i < sizeof (values)/sizeof (values[0]) ; ++i)
  {
    sum += values[i];
  }
  std::cout << "The sum of the array elements is " << sum << std::endl;
}

Result


Related Tutorials