C++ TUTORIALS - C++ Arrays
C++ Arrays
ADVERTISEMENTS
C++ Arrays in Detail:
Concept | Description |
---|---|
Multi-dimensional arrays | C++ supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. |
Pointer to an array | You can generate a pointer to the first element of an array by simply specifying the array name, without any index. |
Passing arrays to functions | You can pass to the function a pointer to an array by specifying the array's name without an index. |
Return array from functions | C++ allows a function to return an array. |
ADVERTISEMENTS
Declaring Arrays:
type arrayName [ arraySize ];ADVERTISEMENTS
double balance[10];Initializing Arrays:
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
balance[4] = 50.0;Accessing Array Elements:
double salary = balance[9];
#include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int n[ 10 ]; // n is an array of 10 integers // initialize elements of array n to 0 for ( int i = 0; i < 10; i++ ) { n[ i ] = i + 100; // set element at location i to i + 100 } cout << "Element" << setw( 13 ) << "Value" << endl; // output each array element's value for ( int j = 0; j < 10; j++ ) { cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl; } return 0; }
Element Value 0 100 1 101 2 102 3 103 4 104 5 105 6 106 7 107 8 108 9 109