Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Array Data Structure

The document provides an overview of arrays as a linear data structure that stores elements of the same data type in contiguous memory locations. It discusses the need for arrays, their types (one-dimensional and multi-dimensional), declaration, initialization, and basic operations such as insertion, deletion, searching, updating, and displaying elements. Additionally, it highlights the advantages and disadvantages of using arrays in programming.

Uploaded by

abdullah.abaid78
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Array Data Structure

The document provides an overview of arrays as a linear data structure that stores elements of the same data type in contiguous memory locations. It discusses the need for arrays, their types (one-dimensional and multi-dimensional), declaration, initialization, and basic operations such as insertion, deletion, searching, updating, and displaying elements. Additionally, it highlights the advantages and disadvantages of using arrays in programming.

Uploaded by

abdullah.abaid78
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Data Structure and Algorithm

Topperworld.in

Arrays

An array is a linear data structure that collects elements of the same data
type and stores them in contiguous and adjacent memory locations.

Arrays work on an index system starting from 0 to (n-1), where n is the


size of the array.

➔Need of Array :

Let's suppose a class consists of ten students, and the class has to publish
their results. If you had declared all ten variables individually, it would be
challenging to manipulate and maintain the data.

©Topperworld
Data Structure and Algorithm

If more students were to join, it would become more difficult to declare all
the variables and keep track of it. To overcome this problem, arrays came
into the picture.

➔Types of Arrays :

There are majorly two types of arrays, they are:

• One-Dimensional Arrays
• Multi-Dimensional Arrays

➔ One-Dimensional Arrays:

You can imagine a 1d array as a row, where elements are stored one after
another.

➔Multi-Dimensional Arrays:

These multi-dimensional arrays are again of two types. They are:

1. Two-Dimensional Arrays :

©Topperworld
Data Structure and Algorithm

You can imagine it like a table where each cell contains elements.

2. Three-Dimensional Arrays:

You can imagine it like a cuboid made up of smaller cuboids where each
cuboid can contain an element.

In this "arrays in data structures" tutorial, you will work around one-
dimensional arrays.

➔Declaration of Array

Arrays are typically defined with square brackets with the size of the arrays
as its argument.

Here is the syntax for arrays:

• 1D Arrays: int arr[n];

©Topperworld
Data Structure and Algorithm

• 2D Arrays: int arr[m][n];

• 3D Arrays: int arr[m][n][o];

➔Initialization of an Array

You can initialize an array in four different ways:

• Method 1:

int a[6] = {2, 3, 5, 7, 11, 13};

• Method 2:

int arr[]= {2, 3, 5, 7, 11};

• Method 3:

int n;
scanf(“%d”,&n);
int arr[n];
for(int i=0;i<5;i++)
{
scanf(“%d”,&arr[i]);
}

• Method 4:

int arr[4];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;

©Topperworld
Data Structure and Algorithm

➔Accessing Elements of Arrays in Data Structures

You can access elements with the help of the index at which you stored
them. Let's discuss it with a code:

#include<stdio.h>
int main()
{
int a[5] = {2, 3, 5, 7, 11};
printf(“%d\n”,a[0]); // we are accessing
printf(“%d\n”,a[1]);
printf(“%d\n”,a[2]);
printf(“%d\n”,a[3]);
printf(“%d”,a[4]);
return 0;
}
OUTPUT :-

©Topperworld
Data Structure and Algorithm

➔Advantages of Arrays

• Arrays store multiple elements of the same type with the same name.

• You can randomly access elements in the array using an index


number.

• Array memory is predefined, so there is no extra memory loss.

• Arrays avoid memory overflow.

• 2D arrays can efficiently represent the tabular data.

➔Disadvantages of Arrays

• The number of elements in an array should be predefined

• An array is static. It cannot alter its size after declaration.

• Insertion and deletion operation in an array is quite tricky as the array


stores elements in continuous form.

• Allocating excess memory than required may lead to memory


wastage

➔Basic Operations in the Arrays


The basic operations in the Arrays are insertion, deletion, searching,
display, traverse, and update. These operations are usually performed to
either modify the data in the array or to report the status of the array.
Following are the basic operations supported by an array.

• Traverse − print all the array elements one by one.


• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the
value.
• Update − Updates an element at the given index.
• Display − Displays the contents of the array.

©Topperworld
Data Structure and Algorithm

➔Traversal Operation
This operation traverses through all the elements of an array. We use loop
statements to carry this out.

Example:-
public class ArrayDemo {
public static void main(String []args) {
int A[] = new int[5];
System.out.println("The array elements are: ");
for(int i = 0; i < 5; i++) {
A[i] = i + 2;
System.out.println("A[" + i + "] = " + A[i]);
}
}
}

OUTPUT:-

➔Insertion Operation
In the insertion operation, we are adding one or more elements to the
array. Based on the requirement, a new element can be added at the
beginning, end, or any given index of array. This is done using input
statements of the programming languages.

Example:-
public class ArrayDemo {
public static void main(String []args) {
int A[] = new int[3];
System.out.println("Array Before Insertion:");
for(int i = 0; i < 3; i++)
System.out.println("A[" + i + "] = " + A[i]); //prints
empty array
System.out.println("Inserting Elements..");

©Topperworld
Data Structure and Algorithm

// Printing Array after Insertion


System.out.println("Array After Insertion:");
for(int i = 0; i < 3; i++) {
A[i] = i+3;
System.out.println("A[" + i + "] = " + A[i]);
}
}
}

OUTPUT:-

➔Deletion Operation
In this array operation, we delete an element from the particular index of
an array. This deletion operation takes place as we assign the value in the
consequent index to the current index.

Example:-
public class ArrayDemo {
public static void main(String []args) {
int A[] = new int[3];
int n = A.length;
System.out.println("Array Before Deletion:");
for(int i = 0; i < n; i++) {
A[i] = i + 3;
System.out.println("A[" + i + "] = " + A[i]);
}
for(int i = 1; i<n-1; i++) {
A[i] = A[i+1];
n = n - 1;
}
System.out.println("Array After Deletion:");
for(int i = 0; i < n; i++) {
System.out.println("A[" + i + "] = " + A[i]);
}
}
}

©Topperworld
Data Structure and Algorithm

OUTPUT:-

➔Search Operation
Searching an element in the array using a key; The key element
sequentially compares every value in the array to check if the key is present
in the array or not.

Example:-
public class ArrayDemo{
public static void main(String []args){
int A[] = new int[5];
System.out.println("Array:");
for(int i = 0; i < 5; i++) {
A[i] = i + 3;
System.out.println("A[" + i + "] = " + A[i]);
}
for(int i = 0; i < 5; i++) {
if(A[i] == 6)
System.out.println("Element " + 6 + " is found at index " + i);
}
}
}

OUTPUT:-

➔Update Operation
Update operation refers to updating an existing element from the array at
a given index.

©Topperworld
Data Structure and Algorithm

Example:-

public class ArrayDemo{


public static void main(String []args) {
int A[] = new int[5];
int item = 15;
System.out.println("The array elements are: ");
for(int i = 0; i < 5; i++) {
A[i] = i + 2;
System.out.println("A[" + i + "] = " + A[i]);
}
A[3] = item;
System.out.println("The array elements after updation are: ");
for(int i = 0; i < 5; i++)
System.out.println("A[" + i + "] = " + A[i]);
}
}

OUTPUT:-

©Topperworld
Data Structure and Algorithm

➔Display Operation
This operation displays all the elements in the entire array using a print
statement.

Example:-
public class ArrayDemo {
public static void main(String []args) {
int A[] = new int[5];
System.out.println("The array elements are: ");
for(int i = 0; i < 5; i++) {
A[i] = i + 2;
System.out.println("A[" + i + "] = " + A[i]);
}
}
}

OUTPUT:-

©Topperworld

You might also like