C++ Pointer Storing even numbers in an array and accessing them

Description

C++ Pointer Storing even numbers in an array and accessing them

#include <iostream>
#include <iomanip>

int main()/*from   w w w.j  a va2  s.com*/
{
  const int n {50};
  int evens[n];

  for (int i = 0; i < n; ++i)
    evens[i] = 2 * (i + 1);

  const int perline {10};
  std::cout << "The numbers are:\n";

  for (int i = 0; i < n; ++i){
    std::cout << std::setw(5) << *(evens + i);
    if ((i + 1) % perline)
       continue;// Uses the loop counter to decide when a newline is required
    std::cout << std::endl;
  }

  std::cout << "\nIn reverse order the numbers are:\n";

  for (int i = n - 1; i >= 0; --i){
    std::cout << std::setw(5) << *(evens + i);
    if (i % perline)
       continue;
    std::cout << std::endl;
  }
}



PreviousNext

Related