C++Tutorials

C++ array

In this article, we will learn about C++ Array

Like other programming languages, array in C++ is a group of similar types of elements that have contiguous memory location.

In C++ std::array is a container that encapsulates fixed-size arrays. In C++, the array index starts from 0. We can store only a fixed set of elements in a C++ array.

Types of array in c++

There are 2 types of arrays in C++ programming:

  1. Single Dimensional Array
  2. Multidimensional Array

C++ Single Dimensional Array

An array of one dimension is known as a onedimensional array or 1-D array or single-dimensional array.

Syntax:

C++

Example:

C++

Output:

10
0
20
0
30

C++ MultiDimensional Array

An array of two-dimension is known as a twodimensional array or 2-D array or multidimensional array. for example

C++

Here, x is a two-dimensional array. It can hold a maximum of 12 elements.

We can think of this array as a table with 3 rows and each row has 4 columns as shown below.

C++ two dimensional array
Elements in two-dimensional array in C++ Programming

Example for Two Dimensional Array

C++

Output

test[0][0] = 2
test[0][1] = -5
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1

Advantages of C++ Array

  • Code Optimization (less code)
  • Random Access
  • Easy to traverse data
  • Easy to manipulate data
  • Easy to sort data etc.

Disadvantages of C++ Array

  • Fixed size

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button