Javatpoint Logo
Javatpoint Logo

C++ Arrays

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++, array index starts from 0. We can store only fixed set of elements in C++ array.

A collection of related data items stored in adjacent memory places is referred to as an array in the C/C++ programming language or any other programming language for that matter. Elements of an array can be accessed arbitrarily using its indices. They can be used to store a collection of any type of primitive data type, including int, float, double, char, etc. An array in C/C++ can also store derived data types like structures, pointers, and other data types, which is an addition. The array representation in a picture is provided below.

Java C array 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

C++ Array Types

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

  1. Single Dimensional Array
  2. Multidimensional Array

C++ Single Dimensional Array

Let's see a simple example of C++ array, where we are going to create, initialize and traverse array.

Output:

10
0
20
0
30

C++ Array Example: Traversal using foreach loop

We can also traverse the array elements using foreach loop. It returns array element one by one.

Output:

10
20
30
40
50

Why do we need arrays?

With a limited number of objects, we can use regular variables (v1, v2, v3,..), but when we need to hold many instances, managing them with normal variables becomes challenging. To represent numerous instances in one variable, we use an array.

What happens if we try to access out of bound array?

The array's elements will be numbered 0 through 9 if we define an array of size 10.

We will have Undefined Behaviour if we attempt to access an element at an index higher than 10, though.

C++ array with empty members

The maximum number of elements that can be stored in an array in C++ is n. What will happen, though, if we store fewer than n elements?

For example,

The array x in this case is 6 elements wide. But we've only given it a three-element initialization.

When that happens, the compiler fills in the empty spaces with random values. This random value frequently appears as 0.

Important things to remember while using arrays in C++

  1. The array's indexes begin at 0. Meaning that the first item saved at index 0 is x[0].
  2. The final element of an array with size n is kept at index (n-1). This example's final element is x[5].
  3. An array's elements have sequential addresses. Consider the scenario where x[0beginning ]'s address is 2120.

The address of the subsequent element, x[1], will then be 2124, followed by x[2], 2128, and so forth.

Each element in this case has a four-fold increase in size. This is due to the fact that int has a 4 byte capacity.

What is two-dimensional array?

Each element in this kind of array is described by two indexes, the first of which denotes a row and the second of which denotes a column.

As you can see, the components are arranged in a two-dimensional array using rows and columns; there are I number of rows and j number of columns.

What is a multi-dimensional array?

A two-dimensional array is the most basic type of multidimensional array; it also qualifies as a multidimensional array. There are no restrictions on the array's dimensions.

How to insert it in array?

How to display the sum and average of array elements?

Output:

The numbers are: 7  5  6  12  35  27
Their Sum = 92
Their Average = 15.3333

How to display array elements?

Output:

The numbers are: 7  5  6  12  35
The numbers are: 7  5  6  12  35






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA