Intro to Arrays ADT
-
Abstract Data Type : representation of data, and the set of operations on the data.
-
Representation of data is defined by the compiler itself.
-
Operations on the data is not given by the compiler, we are supposed to implement / provide them.
-
We will learn how to perform different operations on an array.
-
Some operations we can define:
-
Display( )
-
Add( x ) / Append( x )
-
Insert( index , x )
-
Delete( index )
-
Search( x )
-
Get( index )
-
Set( index , x )
-
Max( ) / Min( )
-
Reverse( )
-
Shift( ) / Rotate( )
-
The representation of an array requires 3 things:
- Array size
- Size
- length (number of elements)
#include <stdio.h>
#include <stdlib.h>
struct Array
{
int *A;
int size;
int length;
};
void Display(struct Array arr)
{
int i;
printf("\nElements are\n");
for(i = 0; i < arr.length; i++)
printf("%d", arr.A[i]);
}
int main()
{
struct Array arr;
int n, i;
printf("Enter size of an array\n");
scanf("%d", &arr.size);
arr.A = (int *)malloc(arr.size * sizeof(int));
arr.length = 0;
printf("Enter number of numbers\n");
scanf("%d", &n);
printf("Enter all elements\n");
for (i = 0; i < n; i++)
scanf("%d", &arr.A[i]);
arr.length = n;
Display(arr);
return 0;
}