Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Data structure

Unit-2
Arrays
Concept of Arrays:-

An array is a data structure for storing more than one data item that has a similar data type. The
items of an array are allocated at adjacent memory locations. These memory locations are
called elements of that array.

The total number of elements in an array is called length. The details of an array are accessed
about its position. This reference is called index or subscript. accessed about its position. This
reference is called index or subscript.

Array (अरे ) एक ह कार के Data items के एक समू ह होते ह। ये Data items एक-दूसरे से स बि धत होते ह। इन समू ह को
एक ह नाम से पहचाने जाते ह। Array के सभी Data items memory म एक ह थान पर Store कये जाते ह िज ह
Contiguous Memory Allocation कहते ह।

कसी Array को Declare करने के लये नाम के बाद बड़ा को ठक (Square Bracket ) म एक Value लखते ह िजसे Array क
आकार (size) कहते ह।

int n [10];

"n" एक Array Variable है िजसम व भ न 10 न बर को Strore कया जा सकता है। इन Numbers को Access (उपयोग)
करने के लये एक Value क आव यकता होती है िजसे Index Value कहते ह जो उस Data item क इस Array म ि थ त
(Position) को बताता है। Aray म Index Value 0(zero) से ार भ होती है।
Array Declaration क कुंछ मह वपू ण बाते िज हं आगे Programming म
उपयोग कया जायेगा :-

(1) Array एक ह जैसे (Same Type) Data Items का समूह होता है।

(2) Array के पहले Data Item क ि थ त (Position) 0 (Zero) से ार भ होती है ।

(3) Array को योग करने के पहले इसका कार (Type) एवं आकार (Size) तय करना आव यक होता है।

(4): Array के सभी Data Items मेमोर म एक ह थान पर Store कये जाते ह िज ह एक Index Variable क सहायता से Use
कये जाते ह।

(5) array को sub-script variable भी कहते है।

Array के थान पर Pointer को use करने के बहु त से फायदे :-

(1) Array के वारा Memory को Access करने क तुलना म Memory को Pointer से Access करना अ धक उपयु त तथा
आसान होता है।

(2) कसी Loop Operation को Array Index क अपे ा Pointer से करना अ धक उपयुि त होता है तथा ती ता के साथ होती
है।

(3) Loop म कसी Array Element को Access करने के लये एक Counter क आव यकता होती है जब क Pointer का उपयोग
करने पर कसी भी Counter क आव यकता नह ं होती तथा काय ती ता के साथ होता है।

(4) कसी Array तथा String को Function म सीधे Pass नह ं कया जा सकता। उसे Pass करने के लये Pointer का उपयोग
कया जाता है।

(5) Pointer का उपयोग Dynamic Array बनाने के लये भी करते ह तथा इसक size program के Run-time म नि चत कया
जा सकता है।

(6) Pointer का उपयोग करके Array क तरह अ य Data Structure बनाया जा सकता है।
Array and Pointer म अंतर :-

Array Pointer

(1) Array एक ह कार के Data Elements के समू ह Pointer का उपयोग कसी अ य Variable के Memory
होता है िज ह Contiguous Memory Allocation म Address को Store करने के लये कया जाता है।
Store कये जाते ह।

(2) Array Elements को उपयोग करने के लये एक इसे सीधे उपयोग कये जाते ह।
Index Variable क आव यकता होती है।

(3) Array को Define करते समय Initialize कया जा इसे Define करते समय Initialize नह ं कया जा सकता।
सकता है। int n [ ] = (2, 3, 4};
(4) Array 'Static' होते ह अथात ् Array क Pointer 'Dynamic' होते ह अथात ् Program
Size Fixed होती है िजसे बाद म बदला के Run time म इसक Size को बढ़ाया या
नह ं जा सकता। हटाया जा सकता है।

(5) कसी Loop म Array के Element को Access जब कसी Loop म Array Elements को
करना एक धीमा process होता है। य क इसम Index Pointer के वारा Access कया जाता है तब
Variable क आव यकता होती है। यह अ धक ती ता से होती है य क इसम
Index Variable क आव यकता नह ं होती।

Memory Allocation of the Array


Array के सभी डेटा elements मु य मेमोर म contiguous थान पर सं ह त कए जाते ह। array का नाम मु य
मेमोर म base address या पहले element के address का त न ध व करता है। array के येक element को
उ चत indexing वारा दशाया गया है।
Array क indexing को तीन तर क से प रभा षत कया जा सकता है।
1. 0 (शू य-आधा रत अनु मण): Array का पहला त व arr[0] होगा।
2. 1 (एक – आधा रत अनु मण): Array का पहला त व arr[1] होगा।
3. n (n- आधा रत अनु म णका): Array का पहला त व कसी भी random index number पर म हो सकता
है।
न न ल खत image म, हमने size 5 क एक array arr क Memory allocation को दखाया है। यह Array 0-
based indexing approach का अनुसरण करती है। array का base address 100th byte है। यह arr[0] का पता
होगा। यहाँ, int का आकार 4 bytes है इस लए येक त व मेमोर म 4 bytes लेगा।

Properties of the Array


1. येक element समान data type का होता है और आकार म भी एकसमान अथात int = 4bytes
2. Array के त व को contiguous memory locations पर सं ह त कया जाता है जहां पहला त व सबसे
छोट मेमोर थान पर सं ह त कया जाता है।
3. Array के त व को randomly ए सेस कया जा सकता है य क हम दए गए base address और डेटा
त व के आकार के साथ array के येक त व के address क गणना कर सकते ह।
उदाहरण के लए, C भाषा म, एक array घो षत करने का syntax न न ल खत है:
int arr[10]; char arr[10]; float arr[5]
Accessing Elements of an array
कसी array के कसी भी random element तक पहु ंचने के लए हम न न ल खत जानकार क आव यकता है:
1. Array का base Address.
2. Bytes म एक element का आकार।
3. Array कस कार क indexing का अनुसरण करता है।
1D array के कसी भी element के address क गणना न न formula का उपयोग करके क जा सकती है:
1. Byte address of element A[i] = base address + size * ( i – first index)

Example

In an array, A[-10 ….. +2 ], Base address (BA) = 999, size of an element = 2 bytes,
find the location of A[-1].
L(A[-1]) = 999 + [(-1) – (-10)] x 2
= 999 + 18
= 1017
Storage representation of multi-dimensional arrays:-

Types of Array
Array मु यतः तीन कार के होते है -:
 One – dimensional arrays (1D Array)
 Two – dimensional arrays (2D Array)
 Multi – dimensional arrays

1. One Dimensional Array (1D Array)


C language म हम जो नामल Array बनाते है वो एक one dimensional array होते है | इसका
Syntax कुछ ऐसा होता है -:
Syntax -:

data_type array_name[array_size];

datatype – यह दशाता है क ऐरे म कस तरह का डाटा टोर होने वाला है |


array_name – यहाँ पर हम ऐरे का नाम लखते है ता क हम ऐरे को इस नाम से अपने ो ाम म
कह भी ए सेस करके उपयोग कर सके |
array_size – हमे िजतने साइज का array चा हए होता है उसे हम यहाँ लखते है जैसे मान
ल िजये य द मुझे 20 वे रएबल साइज का array बनाना है तो म यहाँ पर 20 लखू ंगा और य द
मु झे 200 वे रएबल साइज का array बनाना है तो यहाँ पर म 200 लखू ंगा
Example -:
int number[100];
int डेटा टाइप है
number, array का नाम है
और 100, उस array का साइज है |
Note -:

 जब ऐरे को ड लेअर कया जाता है तब उसमे by default गाबज वै यू टोर होता है |


 ऐरे के इंडे स नंबर by default जीरो से शु होता है जैसे य द ऐरे का साइज 5 है तो इसका इंडे स
नंबर कुछ ऐसा होगा arr[0], arr[1], arr[2], arr[3], arr[4] |
Initialization of one Dimensional Array

सी ल वेज म हम Array को अलग अलग तर के से initialize कर सकते है -:

 Compile time initialization


 Runtime initialization
1. Compile-time initialization of a One Dimensional Array (1D Array)

िजस तरह सी ल वेज म हम एक नामल वे रएबल को initialize करते है ठ क उसी तरह 1D Array को भी
हम initialize कर सकते है और जब हम ऐसा करते है तो इसे Compile-time Initialization कहते है |

जब इस ो ाम को क पाइल कया जायेगा तब उसी समय इस Array क वै यू इ न शयलाइज़ हो


जाएगी |

Examples -:

int var[10] = {10,20,30,40,50,60,70,80,90,100};


int var2[] = {101,201,301,401,501,601,701,801,901,};
int var3[10] = {11,22,33,44,55};

2. Runtime initialization of a One Dimensional Array

जब Array म वै यू को ो ाम रन टाइम म initialize कया जाता है तब इसे run time initialization


कहते है |

Example -:

#include<stdio.h>
void main()
{
int arr[4];
int i, j;
printf("Enter array element : ");
for(i = 0; i < 4; i++)
{
scanf("%d", &arr[i]); //Run time array initialization
}
printf("Printing array element : ");
for(j = 0; j < 4; j++)
{
printf("%d\n", arr[j]);
}
}
इस example म हमने यू जर से वै यू लया और Array ए लमट म टोर कया और बाद म उन वै यू को
printf() फं शन क मदद से नम टं करा दया |

Output -:

Enter array element : 21


23
32
43
Printing array element :
21
23
32
43
Accessing Elements of One Dimensional Array

Array ए लमट को हम Array Name और इंडे स नंबर वारा ए सेस कर सकते है और उनमे अपने
हसाब से बदलाव कर सकते है |

Example -:

#include<stdio.h>
void main()
{ int i;
int arr[5] = {10,20,30,40,50};
printf("Before any changes result :");
for(i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}

arr[3] = 45;
printf("\nAfter changes result : ");
for(i = 0; i < 5; i++)
{
printf("%d ",arr[i]);
}
}
Output

Before any changes result : 10 20 30 40 50


After changes result : 10 20 30 45 50
इस उदाहरण म ऐरे के arr[3] इंडे स वाले ए लमट क वै यू को हमने arr[3] = 45; करके चज कया है |

2. Two Dimensional Array in C (2D Array)


Two Dimensional Array मै स क तरह होता है | िजसे row और columns प म र ेजट
कया जाता है | इसे आसान श द म Array of Array भी कहा जाता है | इसका ड लेरेशन कुछ
ऐसा होता है -:

Syntax -:

data_type array_name[rows][columns];

Example -:

int arr[21][30];

Initialization of Two Dimensional Array (2D Array)

सी ल वेज म हम two Dimensional Array को इन 2 तर के से initialize कर सकते है -:

1. Compile time initialization


2. Runtime initialization
1. Compile-time Initialization of a Two Dimensional Array

Compile-time initialization म हम two dimensional Array को कुछ इस तरह initialize करते है -:

int matrix_A [2][3] = {


{1, 2, 3},
{4, 5, 6}
};
2. Runtime Initialization of a Two Dimensional Array

Two Dimensional Array को जब रन टाइम म वै यू initialize क जाती है तब इसे Runtime


initialization कहते है

Example -:

#include<stdio.h>

void main()
{
int arr[4][4];
int i, j, k;
printf("Enter array element");
for(i = 0; i <= 3;i++)
{
for(j = 0; j < 4; j++)
{
scanf("%d", &arr[i][j]);
}
}
}

3. Multidimensional Array in C

सी ल वेज म Multidimensional array को हम आसान श द म array of arrays


कहते है इसम डेटा टे बु लर फॉम (row और columns के प म ) टोर होता है |

Syntax -:

data_type array_name[size1][size2]....[sizeN];
यहाँ पर डेटा टाइप बताता है क ऐरे ए लमट म कस टाइप का डेटा हो ड होगा |

Example -:

int three_dArray[2][3][4];

Initializing Three Dimensional Arrays


int three_dArray[2][3][4] = {
{{3, 4, 5, 7}, {1, -3, 10, 11}, {3, 12, 53, 20}},
{{17, 4, 16, 3}, {56, 9, 31, 5}, {30, 1, 42, 9}}};
Example of Three Dimensional Arrays
#include <stdio.h>
int main()
{ int x,y,z;
int arr[2][3][2] = {
{ {1,-5},{2,-6}, {3,-7} },
{ {4,-8},{5,-8}, {6,-10} }
};
for ( x=0;x<2;x++)
{
for ( y=0;y<3;y++)
{
for ( z=0;z<2;z++)
{
printf("%d ", arr[x][y][z]);
}
printf("\n");
}
printf("\n");
}
return 0;
}

Output -:
1 -5

2 -6

3 -7

4 -8

5 -9

6 -10

Difference Between One Dimensional Array and Two Dimensional Array in C

One Dimensional Array Two Dimensional Array

One Dimensional array वे रएबल का एक ु प है Two -dimensional array म हर ए लमट खुद म एक 1D


िजसमे हर ए लमट/वे रएबल का डेटा टाइप Array होता है इस लए ऐसे array of array भी कहा
सामान होता है | जाता है |

One Dimensional Array म rows और columns Two Dimensional Array म rows और columns का
का कोई कांसे ट नह ं होता | कॉ से ट होता है |

Syntax – : Syntax -:
data_type array_name[array_size]; data_type array_name[rows][columns];

Example: int a[15]; Example: int a[5][3];

Operations on arrays with Algorithms (searching, traversing,


inserting, Deleting):-

Basic Operations
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.
In C, when an array is initialized with size, then it assigns defaults values to its elements in
following order.
Data Type Default Value

bool false

char 0

int 0

float 0.0

double 0.0f

void

wchar_t 0

Traverse Operation

This operation is to traverse through the elements of an array.


Example
Following program traverses and prints the elements of an array:
#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8

Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a
new element can be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of
the array −
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>

main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

n = n + 1;

while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}

LA[k] = item;

printf("The array elements after insertion :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after insertion :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8
For other variations of array insertion operation click here

Deletion Operation

Deletion refers to removing an existing element from the array and re-organizing all elements of
an array.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to delete an element available at the Kth position of LA.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>

void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

j = k;

while( j < n) {
LA[j-1] = LA[j];
j = j + 1;
}

n = n -1;

printf("The array elements after deletion :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after deletion :
LA[0] = 1
LA[1] = 3
LA[2] = 7
LA[3] = 8
Search Operation
You can perform a search for an array element based on its value or its index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to find an element with a value of ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>

void main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

while( j < n){


if( LA[j] == item ) {
break;
}

j = j + 1;
}

printf("Found element %d at position %d\n", item, j+1);


}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Found element 5 at position 3

Update Operation
Update operation refers to updating an existing element from the array at a given index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to update an element available at the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop
Example
Following is the implementation of the above algorithm −
Live Demo
#include <stdio.h>

void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

LA[k-1] = item;

printf("The array elements after updation :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}
}
When we compile and execute the above program, it produces the following result −
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after updation :
LA[0] = 1
LA[1] = 3
LA[2] = 10
LA[3] = 7
LA[4] = 8

You might also like