Introduction to Arrays

Introduction to Arrays in C++ Language

On this page we will discuss about introduction to arrays in C++ . Arrays are the kind of data structure which can be defined as the collection of similar type of elements arranged in contiguous memory allocation.

introduction to arrays in V

Introduction To Arrays In C++ Programming

An array is a data structure that stores a collection of elements of the same data type, arranged in a contiguous block of memory. Each element in the array can be accessed using an index, which represents its position in the array.

Arrays are commonly used in programming because they allow us to store and manipulate large amounts of data efficiently. They also enable us to perform operations on multiple elements of the array at once, using loops or other control structures.

Arrays can be declared with a fixed size, which is determined at the time of creation, or they can be dynamic, which means that their size can be adjusted during runtime. In many programming languages, arrays are zero-indexed, which means that the first element is stored at index 0.

Why should we use array in Programming

  1. Efficient storage: Arrays allow you to store large amounts of data efficiently in a contiguous block of memory. This means that you can access and manipulate the data quickly and efficiently.

  2. Random access: Arrays provide random access to individual elements, which means that you can access any element in the array directly by its index. This makes it easy to access and modify specific elements of the data set.

  3. Iteration: Arrays allow you to iterate over all the elements in the array using a loop or other control structure. This makes it easy to perform operations on all the elements in the array at once.

  4. Multi-dimensional: Arrays can be multidimensional, meaning that they can have multiple rows and columns of data. This makes them useful for storing and manipulating complex data sets, such as matrices or tables.

  5. Ease of use: Many programming languages provide built-in support for arrays, making them easy to use and integrate into your programs.

Introduction to Arrays in C++

Declaration of Arrays in C++ Programming Language

 Declaration of Array can be done by the following way in C Languge:

    Syntax : Data_type  array_name  [Array_size] ;

Example :
    integer Array : int student [50];
    char Array : char student [50]; (Character Array is also known as String in C Programming Language)
    float Array : float student [50];
    double Array : double student [50];

Initialization of Arrays in C++ Programming Language

 Array can be initialized in C++ programming language in the compile time or in the run time.

Initialization of Array in Run Time

Initialization of Array at Run Time means declaring the elements of array after running the code. Here, elements are taken from user.
we can also set the the size of array when we initialize array at run time.

#include <iostream>

using namespace std;

int main()
{
    int n;                          //size of array
    cin>>n;
    int student[n];                 //array
    for(int i=0; i>student[i];
    return 0;
}

Initialization of Array in Compile Time

Initialization of Array at compile means declaring the elements of array before running the code at the time of declaration which can be done as follow:

#include <iostream>

using namespace std;

int main()
{
    //integer array
    int student1[5] = {60, 70, 65, 80, 85};
    //float array
    float student2[5] = {60.0, 75.5, 80.0, 67.0, 83.5};
    return 0;
}

C++ Code Accessing the elements of Arrays

Run
#include <iostream>

using namespace std;

int
main ()
{
  // Declare an array of integers
  int arr[] = { 1, 2, 3, 4, 5 };

  // Calculate the length of the array
  int arrLen = sizeof (arr) / sizeof (int);

  // Access all the elements of the array using a loop
  for (int i = 0; i < arrLen; i++)
    {
      cout << "Element " << i << " = " << arr[i] << endl;
    }

  return 0;
}

Output

Element 0 = 1
Element 1 = 2
Element 2 = 3
Element 3 = 4
Element 4 = 5
Introduction to Arrays in C

Proving Array follows Contagious Memory Allocation

 The program given below proves that the Array follows contagious memory allocation

Run
#include <iostream>

using namespace std;

int main ()
{
  // Declare an array of integers
  int arr[] = { 1, 2, 3, 4, 5 };

  // Print the memory address of the first element
  cout << "Address of first element: " << &arr[0] << endl;

  // Print the memory address of the second element
  cout << "Address of second element: " << &arr[1] << endl;

  // Print the memory address of the third element
  cout << "Address of third element: " << &arr[2] << endl;

  return 0;
}

Output

Address of first element: 0x7ffe81b40310
Address of second element: 0x7ffe81b40314
Address of third element: 0x7ffe81b40318

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java