Storing even numbers in an array and accessing them using pointer notation - C++ Data Type

C++ examples for Data Type:Pointer

Description

Storing even numbers in an array and accessing them using pointer notation

Demo Code

#include <iostream>
#include <iomanip>

int main()/*w w w .  j  a  v a2  s .c om*/
{
  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;
  }
}

Result


Related Tutorials