Array
Array
Array is a data structure used in almost all of the languages from second
generation onward. This data structure is used to store related data in
contiguous memory locations. The greatest advantage of array is it's simplicity.
Data is stored in contiguous locations and thus can be easily accessed by simple
mathematical calculation.
Array Operations
1) Initialize: In this operation, first element is stored in the
array. Here is a C function for initializing the array:
/********Initialize function*********/
void initialize(int a[])
{
int i,valid;
clrscr();
printf("Enter limit : ");
fflush(stdin);
if(!scanf("%d",&n))
{
err1();
n=0;
return;
}
if(n>LIMIT)
{
printf("Maximum limit of this array can be %d",LIMIT);
printf("\nPress any key to continue ");
getch();
n=0;
return;
}
else
if(n<=0)
{
printf("Limit cannot be less than or equal to zero .");
printf("\nPress any key to continue ");
getch();
n=0;
return;
}
for(i=0;i<n;i++)
{
do
{
printf("Enter the number : ");
fflush(stdin);
valid=scanf("%d",&a[i]);
if(valid==0)
{
err1();
}
}while(valid==0);
}
}
2) Update: This operation changes one of the values in the array.
/**********update function*****************/
void update(int a[],int num1, int num2)
{
int i,flag=0;
for(i=0;i<n;i++)
if(a[i]==num2)
{
flag=1;
a[i]=num1;
break;
}
if(flag!=1)
{
printf("Number not found in array . Press any key to continue . ");
getch();
}
}
3) Insert: This operation inserts a new value in the array.
/**********insert function***************/
void insert(int a[],int num1,int num2)
{
int i=num2-1,j;
if(num2<=n+1 && num2>0)
{
for(j=n-1;j>=i;j--)
a[j+1]=a[j];
a[i]=num1;
n++;
}
else
{
printf("Wrong location . press any key to continue . ");
getch();
}
}
4) Deletion: This operation deletes a value from the array.
/*************Del function********************/
void del(int a[],int num1)
{
int i,flag=0;
for(i=0;i<n;i++)
if(a[i]==num1)
{
flag=1;
break;
}
if(flag==1)
{
for(;i<n;i++)
a[i]=a[i+1];
n--;
}
else
{
printf("Number was not found in the array . Press any key to continue ");
getch();
}
}
You can download the code here. array.cpp (5.28 KB)

![Validate my RSS feed [Valid RSS]](http://www.vshiksha.com/images/valid-rss.png)