Specifying an Array's Size with a Constant Variable and Setting Array Elements with Calculations - C++ Data Type

C++ examples for Data Type:Array

Description

Specifying an Array's Size with a Constant Variable and Setting Array Elements with Calculations

Demo Code

#include <iostream> 
#include <iomanip> 
using namespace std; 

int main() //from   www  . ja  va  2s.  c  o  m
{ 
    const int arraySize = 10; 

    int s[ arraySize ]; // array s has 10 elements 

    // Set array s to the even integers from 2 to 20. 
    for ( int i = 0; i < arraySize; ++i ) // set the values 
       s[ i ] = 2 + 2 * i; 

    cout << "Element" << setw( 13 ) << "Value" << endl; 

    // output contents of array s in tabular format 
    for ( int j = 0; j < arraySize; ++j ) 
       cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl; 
}

Result


Related Tutorials