Application of Arrays
Whenever we require a collection of data objects of the same type and want to process them as a single unit, an array can be used, provided the number of data items is constant or fixed. Arrays have a wide range of applications ranging from business data processing to scientific calculations to industrial projects.
Implementation of a Static Contiguous List
A list is a structure in which insertions, deletions, and retrieval may occur at any position in the list. Therefore, when the list is static, it can be implemented by using an array. When a list is implemented or realized by using an array, it is a contiguous list. By contiguous, we mean that the elements are placed consecutively one after another starting from some address, called the base address. The advantage of a list implemented using an array is that it is randomly accessible. The disadvantage of such a list is that insertions and deletions require moving of the entries, and so it is costlier. A static list can be implemented using an array by mapping the ith element of the list into the ith entry of the array, as shown below:
A complete C program for implementing a list with operations for reading values of the elements of the list and displaying them is given here:
#include<stdio.h>
#include<conio.h>
void read(int *,int);
void dis(int *,int);
void main()
{
int a[5],i,sum=0;
clrscr();
printf(“Enter the elements of array \n”);
read(a,5); /*read the array*/
printf(“The array elements are \n”);
dis(a,5);
}
void read(int c[],int i)
{
int j;
for(j=0;j<i;j++)
scanf(“%d”,&c[j]);
fflush(stdin);
}
void dis(int [],int i)
{
int j;
for(j=0;j<i;j++)
printf(“%d “,d[j]);
printf(“\n”);
}