Professional Documents
Culture Documents
Definition of 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.
• The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
• It also has the capability to store the collection of derived data types, such
as pointers, structure, etc.
Representation of array
Why do we need arrays?
• Consider a scenario where you need to find out the average of 100 integer
numbers entered by user.
• In C, you have two ways to do this:
1) Define 100 variables with int data type and then perform 100 scanf()
operations to store the entered values in the variables and then at last calculate
the average of them.
2) Have a single integer array to store all the values, loop the array to store all
the entered values in array and later calculate the average.
Advantage 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
• We must know in advance that how many elements are to be stored in array.
• Only elements of same data types can be stored in an array. We cannot store
elements of multiple data types in a single array.
• As Array elements are stored in consecutive memory locations. So, insertions
and deletions of an element is time consuming as we have to shift other
elements one position ahead or back respectively.
Types of Array
One Dimensional Array (1-D)
Multidimensional Array
One Dimensional Array (1-D)
Syntax: datatype array_name[size];
datatype: It denotes the type of the elements in the array.
array_name: Name of the array. It must be a valid identifier.
size: Number of elements an array can hold.
Here are some example of array declarations:
1
int num[100];
float temp[20];
2
char ch[50];
3
Initialization of Array
• The different types of initializing arrays:
1. At Compile time
(i) Initializing all specified memory locations.
(ii) Partial array initialization
(iii)Initialization without size
(iv)String initialization
2. At Run time
Initializing all specified memory
locations
• Arrays can be initialized at the time of declaration when their initial
values are known in advance.
• Array elements can be initialized with data items of type int, char etc.
• Ex:- int a[5]={10,15,1,3,20};
• During compilation, 5 contiguous memory locations are reserved by the
compiler for the variable a and all these locations are initialized
• Ex:- int a[3]={9,2,4,5,6}; //error: no. of initial vales are more than the size
of array.
Partial array initialization
• Partial array initialization is possible in c language.
• If the number of values to be initialized is less than the size of the array,
then the elements will be initialized to zero automatically.
• Ex:- int a[5]={10,15};
• Even though compiler allocates 5 memory locations, using this declaration
statement; the compiler initializes first two locations with 10 and 15, the
next set of memory locations are automatically initialized to 0's by
compiler
• Initialization with all zeros:-
Ex:- int a[5]={0};
Example
• In this type of search, a sequential search is made over all items one by
one.
• Every item is checked and if a match is found then that particular item is
returned, otherwise the search continues till the end of the data collection.
Program
for (i = 0; i < num ; i++)
{ if (keynum == array[i] )
int num,i, keynum, found = 0; {
printf("Enter the number of elements ");
found = 1;
scanf("%d", &num); break;
int array[num];
printf("Enter the elements one by one \n"); }
for (i = 0; i < num; i++) }
{
scanf("%d", &array[i]);
if (found == 1)
} printf("Element is present in the array at position
printf("Enter the element to be searched "); %d",i+1);
scanf("%d", &keynum); else
printf("Element is not present in the array\n");
}
return 0;
Binary Search Method
• Binary Search is a search algorithm that is used to find the position of an element in a sorted
array. The array should be sorted prior to applying a binary search.
• Working
The binary search algorithm works by comparing the element to be searched by the middle
element of the array and based on this comparison follows the required procedure.
Case 1 − element = middle, the element is found return the index.
Case 2 − element > middle, search for the element in the sub-array starting from middle+1 index
to n.
Case 3 − element < middle, search for element in the sub-array starting from 0 index to middle -1.
1. Let x = 4 be the element to be
searched.
8. x = 4 is found.
7. Repeat steps 3 to 6 until low meets high
Sorting Methods: Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements
if they are in wrong order.
Two Dimensional Array
• An array of arrays is known as 2D array.
• The 2D array is organized as matrices which can be represented as the
collection of rows and columns
• In 2D arrays ,we have to define at least the second dimension of the array.
Declaration of two dimensional Array in C
data_type array_name[rows][columns];
int abc[5][4];
Initialization of 2D Arrays
int disp[ 2 ][4] = { {10, 11, 12, 13}, {14, 15, 16, 17}
};
OR
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
Representation of array in memory
• Let a be a two dimensional m x n array. Though a is pictured as a
rectangular pattern with m rows and n columns, it is represented in
memory by a block of m*n sequential memory locations.
• However the sequence can be stored in two different ways:
• Column Major Order
• Row Major Order
Column Major and Row Major Order
e.g.[3][3][3];