Getting Started With Array in C++

Introduction to C++ Array

An array is a group of data of similar types stored in the following memory location. It's a linear system, wherever information is stored in progressive one after the opposite. The components in an array are retrieved using an index.
For example, in an array of n components, the first element has index zero and the last element has an index (n-1). Components with the following index (i.e. i and i+1) are stored in a consecutive memory location in the system.
The array is divided into the following types:

  1. One Dimensional Array
  2. Two-Dimensional Array

One Dimensional Array: An array in which data are organized linearly in just one dimension is called one-dimensional array. It is usually called as 1-D array
Syntax and Declaration of One-Dimensional Array:
datatype array_name[size];

Two-Dimensional Array: Two-dimensional array is wherever the information is kept in an exceedingly list containing the 1-D array.
Syntax and Declaration of Two-Dimensional Array:
datatype array_name[d1][d2];

Way to declare an array in C++ :
datatype arrayName[arraySize];

Way to initialize an array in C++ :
It's likely to initialize an array through declaration. For example,
int arr[4] = {12, 2, 5, 6};
int arr[] = {12, 2, 5, 6};

Array in C++ Example:
C++ program to hold and calculate the total of 10 numbers entered by the user using arrays.

#include <iostream>
using namespace std;
int main() 	
{
    int numbers[10], sum = 0;
cout<< "Enter 10 numbers: ";
    for (int i = 0; i < 10; ++i) 
    {
cin>> numbers[i];
        sum += numbers[i];
    }
cout<< "Sum = " << sum <<endl;  
    return 0;
}

Output:
Enter 10 numbers: 3
4
5
4
2
6
4
5
2
8
Sum = 43
Comments (0)