Arrays in c++

In this tutorial, we will learn about arrays and different types of arrays in C++.

INTRODUCTION:

Firstly, what is an array?
An array is a collection of a group of similar data items which can be referred to as a single element. Array elements are stored at contiguous memory allocation and array elements can be randomly accessed using indices of the array element. Arrays can be used to store

elements of primitive data types like int, float, double, char, etc of any particular type.

You can clearly understand the array representation by seeing the above picture.

When we have a smaller number of objects, we can use normal variables. But if we want to store a large number of instances, by using normal variables, the code length becomes very large and it would be more difficult to access the elements. So to overcome this drawback, arrays are used to represent many instances in a single variable.

Declaration :

Declaration of an array can be done like this.Elements can be assigned to an array in different methods.

int arr[] = { //list of elements sepearated by a comma};
int arr[10];
int arr[10] = {0}; //initializes all 10 elements with 0's

Initiation of elements to an array

Elements can be assigned to an array in different methods.

#include <iostream>
using namespace std;
int main()
{
int arr1[10];
int n = 10;
int arr2[n];
int arr3[] = { 10, 20, 30, 40};
int arr4[6] = { 10, 20, 30, 40 };
return 0;
}

Arrays can be initialized by the above different methods. In all the above cases, an array will be created with the specific size of the elements.

See also  C++ | Jump Statements

Accessing array elements

Each element in an array is represented by a number, which is called the index of the number. Elements of the array can be easily accessed using the indices of the element.

#include <iostream>
using namespace std;
int main() {
  int numbers[5] = {7, 5, 6, 12, 35};
  cout << "The numbers are: ";
  for (int i = 0; i < 5; ++i) {
    cout << numbers[i] << "  ";
  }
  return 0;
}

Output:

7 5 6 12 35

Advantages of array

  • Optimization of the code will be increased, and retrieving or sorting of the data can be done efficiently.
  • We can randomly access elements present in the array with their indices numbers.
  • Code length will be decreased as multiple elements get stored in a single array.
  • Elements of the array can be accessed easily.
  • Traversing through an array can be easily done using a single loop.

Disadvantages of array

  • The size of the array is fixed. I.e; we cannot modify the size of the array. The size of the array is decided during the array declaration.
  • Insertion and deletion of elements in the array can be costly as the elements are managed accordingly to the new memory allocation.

That’s it for this article! In the next article, we will learn about the types of arrays with examples.

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.