Array in Data Structure
Array in Data Structure
Arrays are defined as the collection of similar types of data items stored at
contiguous memory locations. It is one of the simplest data structures
where each data element can be randomly accessed by using its index
number.
In C programming, they are the derived data types that can store the
primitive type of data such as int, char, double, float, etc. For example, if
we want to store the marks of a student in 6 subjects, then we don't need
to define a different variable for the marks in different subjects. Instead,
we can define an array that can store the marks in each subject at the
contiguous memory locations.
Properties of array
There are some of the properties of an array that are listed as follows -
o Each element in an array is of the same data type and carries the
same size that is 4 bytes.
Representation of an array
As per the above illustration, there are some of the following important
points -
In the above image, we have shown the memory allocation of an array arr
of size 5. The array follows a 0-based indexing approach. The base
address of the array is 100 bytes. It is the address of arr[0]. Here, the size
of the data type used is 4 bytes; therefore, each element will take 4 bytes
in the memory.
Here, size represents the memory taken by the primitive data types. As an
instance, int takes 2 bytes, float takes 4 bytes of memory space in C
programming.
Suppose an array, A[-10 ..... +2 ] having Base address (BA) = 999 and size
of an element = 2 bytes, find the location of A[-1].
= 999 + 18
= 1017
Advertisement
Basic operations
Traversal operation
This operation is performed to traverse through the array elements. It
prints all array elements one after another. We can understand it with the
below program -
1. #include <stdio.h>
2. void main() {
4. int i;
8. }
9. }
Output
Insertion operation
This operation is performed to insert one or more elements into the array.
As per the requirements, an element can be added at the beginning, end,
or at any index of the array. Now, let's see the implementation of inserting
an element into the array.
1. #include <stdio.h>
2. int main()
3. {
5. int i, x, pos, n = 5;
9. printf("\n");
10.
13. n++;
14.
17. arr[pos - 1] = x;
21. printf("\n");
22. return 0;
23. }
Output
Deletion operation
As the name implies, this operation removes an element from the array
and then reorganizes all of the array elements.
1. #include <stdio.h>
2.
3. void main() {
5. int k = 30, n = 5;
6. int i, j;
7.
9.
12. }
13.
14. j = k;
15.
18. j = j + 1;
19. }
20.
21. n = n -1;
22.
24.
27. }
28. }
Output
Search operation
1. #include <stdio.h>
2.
3. void main() {
6.
8.
11. }
15. break;
16. }
17.
18. j = j + 1;
19. }
20.
22. }
Output
Update operation
1. #include <stdio.h>
2.
3. void main() {
6.
8.
11. }
12.
15.
18. }
19. }
Output
Time Complexity
Space Complexity
Advantages of Array
o Array provides the single name for the group of variables of the
same type. Therefore, it is easy to remember the name of all the
elements of an array.
Disadvantages of Array