C++ Arrays

The C++ arrays are a collection of data objects of the same type that are stored in consecutive memory locations under a common name (array name).

The array elements are indicated with a subscript called the index value. Using the index we can access an array element just like any variable.

In the following section, you will learn to

  • Declare an array
  • Initialize an array
  • Access an array element

How to declare an array?

To declare an array use the following syntax.

<data type> < array name> [number of elements in the array];

The declaration contains the following information.

The data type of array

All array has only one data type and storing any other value other than what is declared will result in an error. The data type could be any built-in type or user-defined type such as structures.

The array name

The array name follows the same rule as any variable name. It cannot begin with a number or special character, cannot contain a whitespace.

Subscript

The subscript is the square bracket used to define the number of elements in the array. A single subscript is for one-dimensional array and two subscript represents two-dimensional array.

For example,

int mangoes[10];

The above declaration will reserve memory space for 10 integer elements.

Invalid ways to declare an array

Not following the rules of an array declaration is considered invalid and lead to error.

  • arr[10];  // It is invalid because no array type declared
  • int 9arr[10]; // It is invalid because array name starts with number.
  • int arr[10.2]; // An array cannot have floating point number in the index.
  • char [#];  // No array name and special character for the number of elements.
  • static float arr[-20]; // Static storage class not for arrays and negative values not allowed

How to initialize an array?

An array must be initialized similarly to any C++ variable. An initial value is assigned to each and every member of an array that is declared.

The syntax to initialize an array is given below.

<data type> <array name> [n number of elements] = { value1, value2, value3, ..., value n};

You may initialize the array after declaration also.

<data type> <array name> [n number of elements];

<array name> = { value 1, value2, value3, ..., value n};

Examples of array initialization

int oranges[5] = {1,3,4,5,6};

int mangoes[8];

mangoes[8] = { 3,6,2,0,8,1,4,9};

Suppose you initialize fewer elements than the available space in an array. The remaining elements will get

a value of 0 by default.

For  example

int tables[7] = { 2,4,5,6};

Then the values assigned to each element of the above array is

tables[0] = 2;

tables[1] = 4;

tables[2] = 5;

tables[3] = 6;

tables[4] = 0;

tables[5] = 0;

tables[6] = 0;

You can see that the last 3 elements have value of 0.

How to access and manipulate array elements?

Each array element has an index value, which starts from 0. If the array has 5 elements than the index values are 0, 1, 2, 3, 4. The array index has a range of 0 to n-1 for n elements.

For example,

int fruit[6] = { 2, 5, 7, 9, 1, 3};

The 5th element of the array is fruit[4] with a value of 1. Therefore, the nth element has a subscript value of n-1.

n = 6 , fruit[5]

n = 3, fruit[2]

The individual elements of the array are treated as any variable. They can be assigned new values, used in expressions and so on.

Reading array element

You can read array element using cin(standard input stream) in your program. It is difficult to read each element individually, so we use a loop to change the index value of array element and assign a value.

For example,

for(int i = 0; i < 5; i++)

{

     cout << "Enter a value:";

     cin << fruit[i];

}

The variable i represents the index and each iteration of the loop increment the index referring to a new array element. The loop terminates when the variable i exceeds constant value 5.

You can use array element in an expression such as

value = fruit[5] * fruit[2];

cout << value << endl;

Example Program:

/* Basics of Array */

#include <cstdlib>
#include <iostream>

#define MAX 10

using namespace std;

int main()
{
    // Array declaration
    
    int fruit_arr[MAX];
    
    //Reading Array from Console
    
    for(int i=0;i < MAX; i++)
    {
            cout<<"Enter a number:";
            cin >> fruit_arr[i];
    }
    
    //Print the Array elements with indexes
    
    for(int i = 0; i < MAX; i++)
    {
            cout << "Index" << "[" << i << "]" << "=" << fruit_arr[i] << endl;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

Enter a number:24
Enter a number:55
Enter a number:56
Enter a number:75
Enter a number:53
Enter a number:23
Enter a number:51
Enter a number:98
Enter a number:57
Enter a number:43
Index[0]=24
Index[1]=55
Index[2]=56
Index[3]=75
Index[4]=53
Index[5]=23
Index[6]=51
Index[7]=98
Index[8]=57
Index[9]=43

In the next article, you will learn about different types of the array and how to use them.

Please support us by disabling your adblocker or whitelist this site from your adblocker. Thanks!

turn of adblocker imag