2. Definition
• Defn: An array in C is a collection of items
stored at contiguous memory locations and
elements can be accessed randomly using
indices of an array.
• An array is a linear data structure which
stores homogeneous(similar)data items.
• The array is the simplest data structure where
each data element can be randomly accessed
by using its index number.
3. Example:
• C array is beneficial if you have to store similar elements.
• Suppose you have to store marks of 50 students, one way to do this is
allotting 50 variables.
• So it will be typical and hard to manage. For example we can not access
the value of these variables with only 1 or 2 lines of code.
• Another way to do this is array. By using array, we can access the elements
easily. Only few lines of code is required to access the elements of
array.
4. Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Easy to traverse data: By using the for loop, we can retrieve the
elements of an array easily.
3) Easy to sort data: To sort the elements of array, we need a few
lines of code only.
4) Random Access: We can access any element randomly using the
array.
5. Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration
of array, we can't exceed the limit. So, it doesn't grow the size
dynamically like LinkedList.
6. Array Terminology
Array variable is composed with the following terms:
• Size:total number of elements in an array
• Type: data type of declared array.
• Base:address of the first element of the array.
• Index:Location or index value of array element.
• Range:Value from lower bound to upper bound.
• Word:indicates the space required for an element.
7. Declaration of C Array
when array size is given:
• We can declare an array in the c language in the following way:
• the example to declare array:
• Here, int is the data_type, marks is the array_name and 5 is
the array_size.
8. Declaration of C Array
when array size is not known before compilation:
data type array_name [];
//size will depend upon the elements entered by
user at run time.
Example:
int a[];
9. Initialization of C Array
At the time of declaration:
• A simple way to initialize array is by index.
• array index starts from 0 and ends with [SIZE - 1].
int a[5]={10,20,30,40,50}
10. Taking input from the user:
void main()
{
int a[5];
for(int i=0;i<5;i++)
{ to take input
printf(“enter the array element”); from the user
scanf(“%d”,&a[i]);
}
for(int i=0;i<5;i++)
{ to display output
printf(“the array is: %d”,a[i]); to the user
}
}
A
i=0<5 true
a[0]
=5
i=1<5 true
A[1]= 7
i=2<5 true
A[2]=9
i=3<5 true
A[3]=4
i=4<5 true
A[4]=3
A[0] A[1] A[2] A[3] A[4]
5 7 9 4 3
11. C array example
0 1 2 3 4
marks[0] marks[1] marks[2] marks[3] marks[4]
80 60 70 85 75
12. C Array: Declaration with
Initialization
• We can initialize the c array at the time of declaration.
• In such case, there is no requirement to define size. So it can
also be written as the following code.
14. Memory Representation of elements of 1D- array
• Loc(a[k])=base(a)+w(k-lb)
• Size of an array =ub-lb+1
Where
a[k]= element at kth location of an array
base(a)= base address of an array.
w= number of bytes per storage location
k= position of element
lb= lower bound of an array
ub= upper bound of array
1. Consider an integer array a of 10 elements[0,1,…,9] such that first element is stored at
1197. Also if each integer stores 2 bytes of memory. Find the address of 4th element . (K=3)
1197-1198 1199-1200 1201-1202
1203-1204
Giveb:
W,width=2 bytes
K=4
Lower bound,lb=1
Upper bound,ub=10
Base address,base(a)= 1197
To find: loc(a[4])?
Loc(a[k])=base(a)+w(k-lb)
loc(a[4])= 1197 + 2(4- 1)
= 1197 +6
= 1203
21. Time Complexity of Linear Search
• The best-case complexity is O(1) if the element is found in the first iteration
of the loop.
• The worst-case time complexity is O(n), if the search element is found at
the end of the array, provided the size of the array is n.
22. Binary Search
• Binary search can be implemented only
on a sorted list of items. If the elements
are not sorted already, we need to sort
them first.
• Procedure: Search a sorted array by
repeatedly dividing the search interval in
half. Begin with an interval covering the
whole array. If the value of the search
key is less than the item in the middle of
the interval, narrow the interval to the
lower half. Otherwise, narrow it to the
upper half. Repeatedly check until the
value is found or the interval is empty.
1. Input size of array, array, element to be searched.
2. Set Low=0, high=size-1
3. Mid=(low+high)/2
4. While((low<=high) && (a[mid]!= element))
5. {
1. If(element<a[mid])
2. High=mid-1
3. else
4. low=mid+1
5. Mid=(low+high)/2
}
6. If(low>end)
7. Display not found
8. Else
9. Return found at location
Pre-requisite: elements in the array
should be sorted in ascending order.
23. • i is used for iteration through the array.
• first is used to hold the first array index.
• last is used to hold the last array index.
• middle holds the middle value calculated by
adding high and low and dividing it by 2.
• n is the number of elements in the array.
• search is the element to search.
• array is the array element of size 100.
26. Selection sort
The selection sort algorithm sorts an array
by repeatedly finding the minimum
element (considering ascending order) from
unsorted part and putting it at the
beginning.
The algorithm maintains two subarrays in a
given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the
minimum element (considering ascending
order) from the unsorted subarray is picked
and moved to the sorted subarray.
27. Working of Selection Sort
1.Set the first element as minimum
2.Compare minimum with the second element. If the second
element is smaller than minimum, assign the second element
as minimum.
Compare minimum with the third element. Again, if the third
element is smaller, then assign minimum to the third element
otherwise do nothing. The process goes on until the last element.
3.After each iteration, minimum is placed in the front of the
unsorted list.
4.For each iteration, indexing starts from the first unsorted
element. Step 1 to 3 are repeated until all the elements are
placed at their correct positions.
28.
29.
30. Insertion sort
• Insertion sort is a sorting algorithm that places an unsorted
element at its suitable place in each iteration.
31. Working of Insertion Sort
Suppose we need to sort the following array.
1. The first element in the array is assumed to be sorted. Take the second element and store it
separately in key.
Compare key with the first element. If the first element is greater than key, then key is placed in front of the
first element.
32. 2. Now, the first two elements are sorted.
Take the third element and compare it with the elements on the left of it. Placed it just behind
the element smaller than it. If there is no element smaller than it, then place it at the
beginning of the array.
35. Time complexities
Algorithm Best case Average case Worst case
Linear search O(1) O(n) O(n)
Binary search O(1) O(logn) O(logn)
Bubble sort O(n*n) O(n*n) O(n*n)
Selection sort O(n*n) O(n*n) O(n*n)
Insertion sort O(n) O(n*n) O(n*n)
36. Lab Exercises on 1D array
1. WAP to input an array and traverse each element of the array.
2. WAP to find sum of all elements in an array.
3. Create a Menu driven program to insert and delete any element in an array at specific position.
4. WAP to replace an element at given position.
5. WAP to print the smallest element of the array
6. WAP to search a particular element in an array.(Linear search)
7. WAP to search a particular element in an array.(Binary Search)
8. WAP to implement Bubble Sort.
9. WAP to implement Selection Sort.
10. WAP to implement Insertion Sort.
37. Two dimensional Array
• The two dimensional array in C language
is represented in the form of rows and
columns, also known as matrix.
• It is also known as array of arrays or list
of arrays.
• The two dimensional, three dimensional
or other dimensional arrays are also
known as multidimensional arrays.
38. Declaration of two dimensional Array in
C
• We can declare an array in the C language in the following way:
• A simple example to declare two dimensional array:
• Here, 4 is the row number and 3 is the column number.
39. Initialization of 2D Array in C
While declaring it:
Taking input from the user:
Col0 Col1 Col2
Row0 a[0][0] a[0][1] a[0][2]
Row1 a[1][0] a[1][1] a[1][2]
Row2 A[2][0] a[2][1] a[2][2]
43. Row Major Order
Loc(a[i][j])=base(a)+w[n(i-lbr)+(j-lbc)]
Where,
Base(a)= base address of array
W= bytes per word
N= total number of col.
I= index of element
Lbr= lower bound of row
J= index of element
Lbc= lower bound of col.
Column Major Order
Loc(a[i][j])=base(a)+w[(i-lbr)+m(j-lbc)]
Where,
Base(a)= base address of array
W= bytes per word
m= total number of rows.
I= index of element
Lbr= lower bound of row
J= index of element
Lbc= lower bound of col.
44. Numerical
Question:
Consider a 2D array [3,…...,10:2,…..7]. Suppose the base address of
the array is 1197. Find the location of a[5][4] if each element
requires 4 bytes for its storage. Find the location if the elements
are arranged in
1) row major order
2.) col major order
45. Addition & Subtraction of Two Matrices
Point to Remember:
Two matrices can be added or subtracted if they have same dimensions I.e; rows and columns of first
matrix should be equal to rows and columns of second matrix.
46. Program to perform addition of two matrices
#include<stdio.h>
#include<conio.h>
Void main()
{
int a[2][2],b[2][2],c[2][2];
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
printf(“enter a[][]”);
scanf(“%d”,&a[i][j]);
}
}
A[2][2]
Col0Col1
Row0 1 2
Row1 3 4
B[2][2]
Col0Col1
Row0 5 6
Row1 7 8
C[2][2]
Col0Col1
Row0 6 8
Row1 10 12
C[0][0]=a[0][0]+b[0][0]
C[0][1]=a[0][1]+b[0][1]
47. for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
printf(“enter b[][]”);
scanf(“%d”,&b[i][j]);
}
}
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
c[i][j]=a[i][j]+ b[i][j];
}
}
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
printf(“c[i][j] : %d”,c[i][j]);
}
}
48. TRANSPOSE OF A MATRIX
Row becomes column and
column becomes row.
52. Lab exercise for 2-D Array
1. Write a program to enter elements in a matrix and display them
where rows and columns are entered by the user.
2. WAP to add two matrices.
3. WAP to find the transpose of a matrix.
4. WAP to multiply two matrices.