What is Array?

What is Array?

Introduction

An Array is the linear collection of finite numbers of homogenous data elements. Here the term linear means the relationship of adjacency among the elements i.e., 1st, 2nd, 3rd..., nth element of any array can be identified in sequence. The sequence of the array are homogenous which means all elements should be of same data type. The elements of an array are stored in consecutive memory locations.

Types Of Array

One Dimensional Array:

In a one-dimensional Array the elements are stored in contiguous memory locations where each element is accessed by using a single index value. It is a linear data structure storing all the elements in sequence. This is one of the most basic types of an Array. They are relatively simple to utilize in programs and define. A One-Dimensional Array is a particularly practical Data structure type since the values it stores are easily initialized and Change.

Declaration Syntax of One-Dimensional Array

data_type array_name [array_size];

Formula to calculate the Address (Loc) of the Elements:

Address (LA[k]) =BaseAddress+WordLength*(k-Lowerbound)

Example:

Base Address (Array)=1500,

W=2,

Lower bound(lb)=1995,

K=1998,

Array [1998] =?

Solution:

Address (Array [1988]) =1500+2*(1998-1995)

\=1500+6

\=1506

The Address of 1998 is 1506.

Two-Dimensional Array

A two-dimensional array in C++ is the simplest form of a multi-dimensional array. It can be visualized as an array of arrays. The image below depicts a two-dimensional array.

Declaration Syntax of Two-Dimensional Array

datatype array_name[ROW][COL];

Formula to calculate the Address (Loc) of the Elements:

Row Major Order:

Address of (a[i][j])=BaseAddress+w[col(i-lowerbound_row)+(j-lowerbound_col)]

Column Major Order:

Address of (a[i][j])=BaseAddress+w(i-lowerbound_row)+row(j-lowerbound_col)]

Basic Operations To Perform with Array:

Traversing Array

Given an integer array of size N, the task is to traverse and print the elements in the array.
Examples:

Input: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3

Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1

Approach:-

1. Start a loop from 0 to N-1, where N is the size of array.

for(i = 0; i < N; i++)

2. Access every element of array with help of

arr[index]

3. Print the elements.

printf("%d ", arr[i])

Deleting Array

To delete an element from an Array, such as:

  1. Deleting Elements from an Array when it is Unsorted

  2. Deleting Elements from an Array when it is Sorted

Deleting Elements in an Array when it is Unsorted

In the delete operation, the element to be deleted is searched using the linear search, and then the delete operation is performed followed by shifting the elements.

Deleting Elements in an Array when it is Sorted

In the delete operation, the element to be deleted is searched using binary search, and then the delete operation is performed followed by shifting the elements.

Ways to insert into an Array, such as:

  1. Inserting Elements in an Array at the End

  2. Inserting Elements in an Array at any Position in the Middle

  3. Inserting Elements in a Sorted Array

1. Inserting Elements in an Array at the End

In an unsorted array, the insert operation is faster as compared to a sorted array because we don’t have to care about the position at which the element is to be placed.

2.Inserting Elements in an Array at any Position

Insert operation in an array at any position can be performed by shifting elements to the right, which are on the right side of the required position

3. Inserting Elements in a Sorted Array

In a sorted array, a search operation is performed for the possible position of the given element by using Binary search, and then an insert operation is performed followed by shifting the elements.

Program:

#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>

void traverseArray(int LA[], int lb, int ub)
{
    cout << "Elements in the array: ";
    int k=lb;
    while (k<=ub)
    {
        cout << LA[k] << " ";
        k++;
    }
    cout << "\n\n";
}


int insertElement(int LA[], int n, int item, int k)
{
    int j=n;
    while(j>=k)
    {
    LA[j + 1] = LA[j];
    j--;
    }
    LA[k] = item;
    cout << "Element inserted successfully." << endl;
    return n + 1;
}

int deleteElement(int LA[], int n, int k)
{
    int j=k;
//     int item=arr[k]
    while(j<=n)
    {
    LA[j] = LA[j+1];
    j++;
    }
    cout << "Element deleted successfully." << endl;
    return n - 1;
}


int main() {
    clrscr();
    int n=0;
    int k;
    int lb;
    int ub;
  cout << "Enter the size of the array: ";
  cin >> n;

  cout<<"Enter The Lower Bound: ";
  cin>>lb;

  ub=n+lb-1;
  //n=ub-lb+1;
  cout<<"The Upper Bound: "<<ub;

  int LA[100];
  cout << "\nEnter the elements of the array: ";
  for (int i = lb; i <= ub; i++) {
  cin >> LA[i];
  }


    int choice;
    do {
   // clrscr();
    cout << "1. Traverse Array" << endl;
    cout << "2. Insert Element" << endl;
    cout << "3. Delete Element" << endl;
    cout << "4. Exit" << endl;
    cout << "Enter your choice: ";
    cin >> choice;

    switch (choice) {
        case 1:
        traverseArray(LA, lb, ub);
        break;
        case 2:
        int item, k;
        cout << "Enter the element to insert: ";
        cin >> item;
        cout << "Enter the position to insert at: ";
        cin >> k;
        ub = insertElement(LA, ub, item, k);
        //ub=n+lb-1;
        n=ub-lb+1;
        break;
        case 3:
        cout << "Enter the position to delete from: ";
        cin >> k;
        ub = deleteElement(LA, ub, k);
        //ub=n+lb-1;
        n=ub-lb+1;
        break;
        case 4:
        cout << "Exiting..." << endl;
        break;
        default:
        cout << "Invalid choice. Please try again." << endl;
        break;
    }
    } while (choice != 4);

    return 0;
}

Output: