2. 2
Array
An array is defined as the collection of similar type of
data items stored at contiguous memory locations.
Arrays are the derived data type in C programming
language which can store the primitive type of data
such as int, char, double, float, etc. It also has the
capability to store the collection of derived data
types, such as pointers, structure, etc. The array is
the simplest data structure where each data element
can be randomly accessed by using its index number.
3. 3
Properties of Array
The array contains the following properties.
● Each element of an array is of same data type and carries
the same size, i.e., int = 4 bytes.
●
Elements of the array are stored at contiguous memory
locations where the first element is stored at the smallest
memory location.
●
Elements of the array can be randomly accessed since we
can calculate the address of each element of the array
with the given base address and the size of the data
element.
4. 4
Advantage of C Array
1) Code Optimization: Less code to the access the
data.
2) Ease of traversing: By using the for loop, we can
retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array,
we need a few lines of code only.
4) Random Access: We can access any element
randomly using the array.
5. 5
Disadvantage of C Array
Fixed Size: Whatever size, we define at the time of
declaration of the array, we can't exceed the limit.
So, it doesn't grow the size dynamically like
LinkedList which we will learn later
7. 7
One Dimensional Array
A One-Dimensional Array is the simplest form of an
Array in which the elements are stored linearly and
can be accessed individually by specifying the index
value of each element stored in the array.
8. 8
Declaration of C Array
data_type array_name[array_size];
For example: int num[5];
●
Memory representation:
●
9. 9
Initialization of C Array
The simplest way to initialize an array is by using the
index of each element. We can initialize each element of
the array by using the index. Consider the following
example.
●
marks[0]=80;//initialization of array
●
marks[1]=60;
●
marks[2]=70;
●
marks[3]=85;
●
marks[4]=75;
10. 10
Printing array elements
C array example
●
#include<stdio.h>
●
int main(){
●
int i=0;
●
int marks[5];//declaration of array
●
marks[0]=80;//initialization of array
●
marks[1]=60;
●
marks[2]=70;
●
marks[3]=85;
●
marks[4]=75;
●
//traversal of array
●
for(i=0;i<5;i++){
●
printf("%d n",marks[i]);
●
}//end of for loop
●
return 0;
●
}
11. 11
C Array: Declaration with Initialization
We can initialize the c array at the time of
declaration. Let's see the code.
●
int marks[5]={20,30,40,50,60};
In such case, there is no requirement to define the
size. So it may also be written as the following code.
●
int marks[]={20,30,40,50,60};
12. 12
Accessing the Elements of an Array
#include<stdio.h>
●
int main(){
●
int i=0;
●
int marks[5]={20,30,40,50,60};//declaration and initialization of array
●
//traversal of array
●
for(i=0;i<5;i++){
●
printf("%d n",marks[i]);
●
}
●
return 0;
●
}
16. 16
Operations on Arrays
●
Traverse − print all the array elements one by one.
●
Insertion − Adds an element at the given index.
●
Deletion − Deletes an element at the given index.
●
Search − Searches an element using the given index
or by the value.
17. 17
Traversal
●
Visiting every element
of an array once is
known as traversing the
array.
●
If we create an array of length 100
using a[100] in C language, we need
not use all the elements. It is
possible for a program to use just 60
elements out of these 100. (But we
cannot go beyond 100 elements).
18. 18
Insertion
●
An element can be inserted in an array at a specific position. For this
operation to succeed, the array must have enough capacity. Suppose we
want to add an element 10 at index 2 in the below-illustrated array, then the
elements after index 1 must get shifted to their adjacent right to make way
for a new element.
●
●
●
●
●
●
●
When no position is specified, it’s best to insert the element at the end to
avoid shifting, and this is when we achieve the best runtime O(1)
19. 19
Deletion
An element at a specified position can be deleted,
creating a void that needs to be fixed by shifting all
the elements to their adjacent left, as illustrated in
the figure .
We can also bring the last element of the array to fill
the void if the relative ordering is not important. :)
21. 21
Searching
●
Searching can be done by traversing the array until the
element to be searched is found O(n) There is still a better
method. As you may remember, we talked about binary search
in some previous tutorials. Don't forget to look it up if you
missed it. We had analyzed both linear and binary search. This
search method is only applicable for sorted arrays. Therefore,
for sorted arrays, the time taken to search is much less than an
unsorted array. O(logn)
22. 22
Sorting
Sorting means arranging an array in an orderly
fashion (ascending or descending). We have different
algorithms to sort arrays. We’ll see various sorting
techniques later in the course.
26. 26
Two Dimensional Array in C
The two-dimensional array can be defined as an
array of arrays. The 2D array is organized as matrices
which can be represented as the collection of rows
and columns. However, 2D arrays are created to
implement a relational database lookalike data
structure. It provides ease of holding the bulk of data
at once which can be passed to any number of
functions wherever required.
27. 27
Declaration of two dimensional Array in C
The syntax to declare the 2D array is given below.
●
data_type array_name[rows][columns];
Consider the following example.
●
int twodimen[4][3];
28. 28
Initialization of 2D Array in C
●
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
●
int arr[4][3]={
{1,2,3},
{2,3,4},
{3,4,5},
{4,5,6}
};
●
int a[4][3] = {1,2,3,2,3,4,3,4,5,4,5,6};
30. 30
C 2D array example: Storing elements in a matrix and printing it.
31. 31
Adding and Subtracting Matrices
A matrix can only be added to (or
subtracted from) another matrix if the
two matrices have the same
dimensions .