.

Arrays in C++ Help

An array is the collection of similar data type that is stored in the memory location. This memory address is used to hold the data.

Declaring an array in C++

The array can be declared in three different methods

    Method 1:
    int arr[5];
    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30;
    arr[3] = 40;
    arr[4] = 50;
    Method 2:
    int arr[] = 10, 20, 30, 40, 50;
    Method 3:
    int arr[5] = 10, 20, 30, 40, 50;
    

Initializing Arrays

The initialization can be done in two different ways

Array initialization during the declaration

int age[5]=22,25,30,32,35;

Initializing separately in the closed loop

int newArray[5];

int n = 0;

Initializing the array separately

arrays img1

Figure : A Pictorial Representation of the Array

Accessing Array Elements

The index of array starts with 0 that is the first element inside the array will be filled at index 0 and consecutively stores the next values.

Example

The above program can be rewritten like this

Arrays in C++

An array can be defined to store the data in the memory location in the sequential collection. All the data stored in the array is of similar data type. An array is the data structure which stores the data in the single variable which is a derived data type.

Advantages

There are four different advantages in array. They are Easy to traverse data, Easy to sort data, Random Access and Code Optimization.

Disadvantages

The size cannot be changed after initialization. Once the size fixed during the initialization step to store the data in the memory, and it will not be altered at any cause during the execution process.

Declaring array

Datatype arrayname [size];

Initializing array

    Method 1
    int arr [4];
    arr[0]=10;
    arr[1]=20;
    arr[2]=30;
    arr[3]=40;
    arr[4]=50;
    Method 2
    Int arr [] = 10, 20, 30, 40, 50;
    

There are two different types of array

Single Dimensional Array

An array can be defined to store the data in the memory location in the sequential collection. All the data stored in the array is of similar data type. An array is the data structure which stores the data in the single variable which is a derived data type.

The array element can be accessed with the index value of the element.

Example

Two Dimensional Arrays

The two dimensional array consists of rows and columns. The data are arranged in form rows and columns where they require mentioning in declaration part using two subscript to represent rows and columns. Array name mentioned in the initialization part gives the main memory and followed by rows and columns.

Datatype arrayname[size][size];

When writing the program these points are to be remembered. Size of the array must be greater than 0, during initialization if the index number is not specified, then the element will be initialized to 0.

Example

Passing Array to Function

Passing array to the function as a argument can be defined in the below example.

Syntax

function_name(array_name);

Example

.