Chapter-2
Array
Definition of an Array :-
"An array is a collection of variables of the same data type that are referenced by a common
name."
Types of an array - 2 type
1. Single dimensional array :- The simplest form of an array is the single dimensional array. The
array is given a name and its elements are referred to by their subscripts or indices. In C
language, the array's index numbering starts with zero. The index of the first element is known as
the lower bound and the index number of the last element is known as the upper bound.
Declaration of single dimensional array -
data_type array_name [size];
Where the data type declares the base type of array which is the type of each element in the
array, the array name is specified the name with which the array will be referenced and size
defines how many elements the array will hold. The size must be an integer value or integer
constant without any sign.
For eg,
int marks[10];
Initialisation of a single dimensional array :-
data_type array_name []={element -1, element -2, .... element -n};
For eg,
int marks[5]={15,25,89,65,55};
OR
float price[3]={25.50,15.25,35.55};
OR
char grade[5]={'A','B',',C','D','E'};
Accessing an element at a particular index for one dimensional arrays :-
Individual elements of an array can be accessed using the following syntax-
array_name [index or subscript];
For eg,
To assign a value to the second location of the array, we give the following statement :
marks[1]=90;
Similarly for reading the value of the 4th element in array_name marks, we give the following
statement :
scanf("%d", & marks[3]);
For writing (printing) the value of the second element in array_name marks, we give the
following statement :
printf("%d",marks[1]);
Note:-
Arrays can always be read or written through a loop. If we read one dimensional array,
requires one loop for reading and another for writing (printing) the array.
For example,
a) for reading the array :
For reading the marks of 10 students,
for{I=0;I<10;I++)
{
scanf("%d",&marks[I]);
}
b) for writing(print) the array :
for(I=0;I<10;I++)
{
printf("%d",marks[I]);
}
2. Two dimensional array :- Like single or one dimensional array has only one subscript or index
number, in the same way- two dimensional arrays have two subscript or index numbers. This is
also called a matrix. As in a matrix, every element is pointed by its row as well as column, in the
same way 4 pointing the element to subscript are used in which the first subscript is called row
while the second sab script is called column.
Declaration of two dimensional array -
data_type array_name[row_number][column_number];
For eg,
int sum[2][3];
Initialisation of a two dimensional array :-
data_type array_name[row][column]={{values of row1}{values of row2}{values of row
n}};
OR
data_type array_name[row][column]={{values of row,}values of row2,...,values of row
n}};
For eg,
int sum[2][3]={5,6,7,8,9,10,11,12,13};
OR
int sum[2][3]={{5,6,7}{8,9,10}{11,12,13}};
Linear Search (Linear / Sequential Search)
Linear search is the simplest method where you check every element of the array one by
one from the starting index to the end until the element is found.
Condition: The array elements can be in any order (sorted or unsorted).
Logic: A loop runs from index 0 to n-1 to check if Array[index] == target.
Efficiency: Slow for large datasets.
Example of Linear Search
Let's search for the target value 55 in an unsorted array:
Index 0 1 2 3 4
2
Value 10 55 8 90
3
Step 1: Compare 55 with index 0 (10). Not a match.
Step 2: Compare 55 with index 1 (23). Not a match.
Step 3: Compare 55 with index 2 (55). Match found! Stop searching and return index 2.
Binary Search (Binary / Half-Interval Search)
Binary search is an efficient algorithm that works on the principle of Divide and Conquer by
splitting the array into halves.
Strict Condition: The array must be sorted either in ascending or descending order.
Logic: Find the middle element. If the target is smaller than the middle, discard the right half. If
greater, discard the left half. Repeat.
Variables Used: low (start index), high (end index), and mid (middle index calculated as (low +
high) / 2). Example of Binary Search
Let's search for the target value 70 in a sorted array:
Index 0 1 2 3 4 5 6
Valu 2 6
10 30 40 50 70
e 0 0
Step 1: Set low = 0, high = 6.
o Calculate mid = (0 + 6) / 2 = 3.
o Value at index 3 is 40.
o Since our target 70 > 40, discard the left half. Set low = mid + 1 = 4.
Step 2: Now low = 4, high = 6.
o Calculate mid = (4 + 6) / 2 = 5.
o Value at index 5 is 60.
Since our target 70 > 60, discard the left half again. Set low = mid + 1 = 6.
Step 3: Now low = 6, high = 6.
o Calculate mid = (6 + 6) / 2 = 6.
o Value at index 6 is 70. Match found! Return index 6.
Key Differences
Feature Linear Search Binary Search
Array Requirement Sorted or Unsorted Must be Sorted
Working Principle Sequential checks Divide and Conquer)
Speed / Process Slow process Very fast process
Complexity Simple implementation Complex implementation
Sorting-
Sorting is the process of arranging data or elements in a specific logical order, which can
be either ascending order (increasing order, e.g., 1 to 10) or descending order (decreasing order,
e.g., 10 to 1)
Bubble sort, selection sort, and insertion sort are fundamental sorting algorithms. They
rearrange an array or list of elements in a specific order (like ascending or descending).
1. Bubble Sort
In bubble sort, adjacent elements (elements next to each other) are compared, and they are
swapped if they are in the wrong order. With each pass, the largest unsorted element "bubbles"
to its correct position at the end of the list.
2. Selection Sort
Selection sort repeatedly finds the minimum (smallest) element in the unsorted part of the list
and swaps it with the first element of that unsorted part. This places the smallest elements at the
beginning, one by one.
3. Insertion Sort
Insertion sort is similar to how you organize a hand of playing cards. It divides the list into a
sorted part and an unsorted part. It takes the first element from the unsorted part and inserts it
into its correct position in the sorted part.
String Array
In a C program, the group string is called character array. We can store character element in a
same way as we Store element in int or float. Only difference is that the last element is always’\0’
that is called null character. So when we declare or initialise a string array then the last element
of string will be the null character which represent the end of string.
For example,
S A T L U J \0
String Declaration -
String declaration will be done in a same way as we learn to declare elements of arra y
before in this chapter. Syntax will be same but only the difference is char will be used for data
type
String Initialisation -
Declaration of the character array is done in the same way as done in normal array that is, it
is initialised in the same way as done in normal array. Initialization of character array can be done
in two different way-
Char name[8]= “RUKMANI”;
OR
Char name[8]={’R’,’U’,’K’,’M’,’A’,’N’,’I’,’\0’};
String Manipulation -
The various functions on strings like to join the strings, to copy strings, to compare the
string,etc in C programming is called string manipulation. The functions that we will use for the
above operations, all that functions need a header file is string.h that we must use in our
program when we write because these functions are pre-defined in this header file string.h.
Following are few function used on string-
[Link]()- By using this function to different string can be connected with each other which mean
this function take first string and connect second string at the end of first string.
Syntax,
Strcat(str1,str2);
OR
Strcat(to, from);
[Link]()- With the help of this function we can copy on a string into the another string or we can
say that this can copy first string in place of second string.
Syntax,
Strcpy(str1,str2);
OR
Strcpy(to, from);
[Link]()- With the help of this string function we can compared to strings. Ek bahut string is
same then it provide’0’ else if both are different than both string are compared not on the basis
of length but on the basis of element is stored.
Syntax,
Strcmp(str1,str2);
[Link]()- This is string function is used to determine the length of string or number of elements
present in the string.
Syntax,
Strlen(string);
Other String manipulation functions -
[Link]() function:- this function is used to convert the uppercase character to lowercase
character.
[Link]() function:- this function is used to convert lowercase characters into uppercase
character.
[Link]() function:- this function is used to print the encode string in the reverse order