Professional Documents
Culture Documents
Data Structure Array
Data Structure Array
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 का समूह होता है।
(3) Array को योग करने के पहले इसका कार (Type) एवं आकार (Size) तय करना आव यक होता है।
(4): Array के सभी Data Items मेमोर म एक ह थान पर Store कये जाते ह िज ह एक Index Variable क सहायता से 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 क आव यकता नह ं होती।
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
data_type array_name[array_size];
िजस तरह सी ल वेज म हम एक नामल वे रएबल को initialize करते है ठ क उसी तरह 1D Array को भी
हम initialize कर सकते है और जब हम ऐसा करते है तो इसे Compile-time Initialization कहते है |
Examples -:
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 -:
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
Syntax -:
data_type array_name[rows][columns];
Example -:
int arr[21][30];
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
Syntax -:
data_type array_name[size1][size2]....[sizeN];
यहाँ पर डेटा टाइप बताता है क ऐरे ए लमट म कस टाइप का डेटा हो ड होगा |
Example -:
int three_dArray[2][3][4];
Output -:
1 -5
2 -6
3 -7
4 -8
5 -9
6 -10
One Dimensional Array म rows और columns Two Dimensional Array म rows और columns का
का कोई कांसे ट नह ं होता | कॉ से ट होता है |
Syntax – : Syntax -:
data_type array_name[array_size]; data_type array_name[rows][columns];
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
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;
n = n + 1;
while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}
LA[k] = item;
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;
j = k;
while( j < n) {
LA[j-1] = LA[j];
j = j + 1;
}
n = n -1;
void main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
j = j + 1;
}
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;
LA[k-1] = item;