C++ array Allocating an array at runtime

Description

C++ array Allocating an array at runtime

#include <iostream>

int main() {/*from   w ww.j  av  a 2  s .c o m*/
  int count {};
  std::cout << "How many heights will you enter? ";
  std::cin >> count;
  unsigned int height[count];                 // Create the array of count elements

  // Read the heights
  int entered {};
  while(entered < count)
  {
    std::cout <<"Enter a height: ";
    std::cin >> height[entered];
    if(height[entered])                     // Make sure value is positive
    {
      ++entered;
    }
    else
    {
      std::cout << "A height must be positive - try again.\n";
    }
  }

  // Calculate the sum of the heights
  unsigned int total {};
  for(int i {} ; i<count ; ++i)
  {
    total += height[i];
  }
  std::cout << "The average height is " << total/count << std::endl;
}



PreviousNext

Related