# APPLICATION OF ARRAYS

 < Day Day Up >

## 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 i th element of the list into the i th entry of the array, as shown in Figure 18.4.

#### Program

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 main() { void read(int *,int); void dis(int *,int); 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 d[],int i) { int j; for(j=0;j<i;j++) printf("%d ",d[j]); printf("\n"); }
```

#### Example

##### Input

Enter the elements of the first array

```15 30 45 60 75
```

##### Output

The elements of the first array are

```15 30 45 60 75
```

 < Day Day Up >
 < Day Day Up >

## MANIPULATIONS ON THE LIST IMPLEMENTED USING AN ARRAY

### Introduction

Shown next are C programs for carrying out manipulations such as finding the sum of elements of an array, adding two arrays, and reversing an array.

#### Program

```ADDITION OF THE ELEMENTS OF THE LIST #include<stdio.h> #include<conio.h> void main() { void read(int *,int); void dis(int *,int); int a[5],i,sum=0; clrscr(); printf("Enter the elements of list \n"); read(a,5); /*read the list*/ printf("The list elements are \n"); dis(a,5); for(i=0;i<5;i++) { sum+=a[i]; } printf("The sum of the elements of the list is %d\n",sum); getch(); } void read(int c[],int i) { int j; for(j=0;j<i;j++) scanf("%d",&c[j]);

fflush

(stdin); } void dis(int d[],int i) { int j; for(j=0;j<i;j++) printf("%d ",d[j]); printf("\n"); }
```

#### Example

##### Input

Enter the elements of the first array

```15 30 45 60 75
```

##### Output

The elements of the first array are

```15 30 45 60 75
```

The sum of the elements of an array is 225.

##### Addition of the two lists

Suppose the first list is

```1 2 3 4 5
```

and the second list is

```5 6 8 9 10
```

The first element of first list is added to the first element of the second list, and the result of the addition is the first element of the third list.

In this example, 5 is added to 1, and the first element of third list is 6.

This step is repeated for all the elements of the lists and the resultant list after the addition is

```6 8 11 13 15
```
```#include<stdio.h> #include<conio.h> void main() { void read(int *,int); void dis(int *,int); void add(int *,int *,int * ,int); int a[5],b[5],c[5],i; clrscr(); printf("Enter the elements of first list \n"); read(a,5); /*read the first list*/ printf("The elements of first list are \n"); dis(a,5); /*Display the first list*/ printf("Enter the elements of second list \n"); read(b,5); /*read the second list*/ printf("The elements of second list are \n"); dis(b,5); /*Display the second list*/ add(a,b,c,i); printf("The resultant list is \n"); dis(c,5); getch(); } void add(int a[],int b[],int c[],int i) { for(i=0;i<5;i++) { c[i]=a[i]+b[i]; } } void read(int c[],int i) { int j; for(j=0;j<i;j++) scanf("%d",&c[j]); fflush(stdin); } void dis(int d[],int i) { int j; for(j=0;j<i;j++) printf("%d ",d[j]); printf("\n"); }
```

#### Explanation

1. Repeat step (2) for i=0,1,2 , (n 1) , where n is the maximum number of elements in a list.

2. c[i] = a[i]+b[i] , where a is the first list, b is the second list, and c is the resultant list; a[i] denotes the i th element of list a .

#### Example

##### Input

Enter the elements of the first list

```1 2 3 4 5
```

##### Output

The elements of the first list are

```2 3 4 5
```

##### Input

Enter the elements of the second list

```6 7 8 9 10
```

##### Output

The elements of the second list are

```6 7 8 9 10
```

The resultant list is

```7 9 11 13 15
```

##### Inverse of the list

The following program makes a reverse version of the list.

```#include<stdio.h> #include<conio.h> void main() { void read(int *,int); void dis(int *,int); void inverse(int *,int); int a[5],i; clrscr(); read(a,5); dis(a,5); inverse(a,5); dis(a,5); getch(); } void read(int c[],int i) { int j; printf("Enter the list \n"); for(j=0;j<i;j++) scanf("%d",&c[j]); fflush(stdin); } void dis(int d[],int i) { int j; printf("The list is \n"); for(j=0;j<i;j++) printf("%d ",d[j]); printf("\n"); } void inverse(int inver_a[],int j) { int i,temp; j-; for(i=0;i<(j/2);i++) { temp=inver_a[i]; inver_a[i]=inver_a[j]; inver_a[j]=temp; j-; } }
```

#### Example

##### Input

Enter the list

```10 20 30 40 50
```

##### Output

The list is

```10 20 30 40 50
```

The inverse of the list is

```50 40 30 20 10
```

This is another version of an inverse program, in which another list is used to hold the reversed list.

```#include<stdio.h> #include<conio.h> void main() { void read(int *,int); void dis(int *,int); void inverse(int *,int *,int); int a[5],b[5]; clrscr(); read(a,5); dis(a,5); inverse(a,b,5); dis(b,5); getch(); } void read(int c[],int i) { int j; printf("Enter the list \n"); for(j=0;j<i;j++) scanf("%d",&c[j]); fflush(stdin); } void dis(int d[],int i) { int j; printf("The list is \n"); for(j=0;j<i;j++) printf("%d ",d[j]); printf("\n"); } void inverse(int a[],int inverse_b[],int j) { int i,k; k=j-1; for(i=0;i<j;i++) { inverse_b[i]=a[k]; k-; } }
```

#### Example

##### Input

Enter the list

```10 20 30 40 50
```

##### Output

The list is

```10 20 30 40 50
```

The inverse of the list is

```50 40 30 20 10
```

 < Day Day Up >