Using a for loop with an array - C++ Statement

C++ examples for Statement:for

Description

Using a for loop with an array

Demo Code

#include <iostream>

int main() {/* w ww. j  av  a2  s . c om*/
  const int size{ 6 };                              // Array size
  unsigned int height[size]{ 26, 37, 47, 55, 62, 75 }; // An array of heights
  unsigned int total{};                              // Sum of heights

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

  unsigned int count{};
  for (unsigned int i{}; i < size; ++i)
  {
    if (height[i] < average) 
      ++count;
  }
  std::cout << count << " people are below average height." << std::endl;
}

Result


Related Tutorials