Core C++

Arrays

One-Dimensional Arrays


Video Tutorials

Lesson 10: One-Dimensional Arrays

Example 1

// Accessing the fifth array element
int iCopyFifthInt = iaArray[4];

Example 2

// Default initialization of ten-element array
int iaArray[10];
// Initialize all entries to zero with a loop
for (int i = 0; i < 10; ++i) {
    iaArray[i] = 0;
}

Example 3

// Initialization with constant values
double daArray[5] = {8.3, 5.4, -2.7, 9.1, 1.6};

Example 4

// Initialization with constant values and unspecified size
double daArray[] = {8.3, 5.4, -2.7, 9.1, 1.6};

Example 5

// Initialization of char array with a constant string
char caName[] = "XoaX.net";

Example 6

// Initialization of char array with size and constant string
char caName[10] = "XoaX.net";

Example 7

// Passing an array to a function
int iaMyArray[] = {6, 2, 3};
MyFunction(iaMyArray);

// Function declaration
void MyFunction(int iaArray[]);

Example 8

// Passing an array to a function with specified size
int iaMyArray[] = {6, 2, 3};
MyFunction(iaMyArray);

// Function declaration
void MyFunction(int iaArray[3]);

Multi-Dimensional Arrays


Video Tutorials

Lesson 28: Multi-Dimensional Arrays

Example 1

// Accessing the element in the fourth row and second column
int iCopyOfInteger = iaaArray[3][1];

Example 2

// Default initialization
int iaaArray[5][3];
// Initializing the array to all zeros
for (int iRow = 0; iRow < 5; ++iRow) {
    for (int iCol = 0; iCol < 3; ++iCol) {
        iaaArray[iRow][iCol] = 0;
    }
}

Example 3

// Initialization with constant values
double daaArray[2][3] = {{5.5, 7.4, 3.8}, {4.2, 0.5, 9.1}};

Example 4

// Initialization of a three-dimensioanl array without
// the first size parameter: 4
int iaaaArray[][2][3] = {   {{5, 8, 8}, {6, 0, 3}}, {{3, 7, 4}, {4, 2, 9}},
                            {{0, 3, 2}, {3, 7, 2}}, {{0, 2, 0}, {9, 3, 1}}  };

Example 5

int iaaaArray[4][2][3];
// Passing a three-dimensional array into a function
MyFunction(iaaaArray);

// Function declaration – first dimension size not needed
void MyFunction(int iaaaArray[][2][3]);
 

© 2007–2024 XoaX.net LLC. All rights reserved.