PPS Unit 2-Notes
PPS Unit 2-Notes
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.
C array is beneficial when you have to store similar elements. For example, if we want
to store the marks of a student in 6 subjects, then we don't need to define different
variables for the marks in the different subject. Instead of that, we can define an array
which can store the marks in each subject at the contiguous memory locations.
By using the array, we can access the elements easily. Only a few lines of code are
required to access the elements of the array.
Properties of Array
The array contains the following properties.
o Each element of an array is of same data type and carries the same size, i.e., int = 4
bytes.
o Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
o 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.
Advantage of an 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.
Sub : PPS Unit 2 Notes
Disadvantage of Array
1) 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.
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
for( int i=0; i<5; i++)
{
printf("numbers[%d] = %d\n", i, numbers[i]);
}
return 0;
}
Output:
Memory mapping in computer
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30 Number
numbers[3] = 40
numbers[4] = 50 0 1 2 3 4
10 20 30 40 50
We have used a for loop to iterate over the elements of the array and print their values
using the printf function.
arrayName[index]
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
printf("The first element of the array is: %d\n", numbers[0]);
printf("The third element of the array is: %d\n", numbers[2]);
Output:
Explanation:
arrayName[index]
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
printf("The third element of the array is %d\n", numbers[2]);
return 0;
}
Output:
Explanation:
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
Sub : PPS Unit 2 Notes
return 0;
Output:
Explanation:
After that, we modified the value of the third element by assigning a new value
of 35 to it. Finally, we have used the printf function again to print the new value of the
third element.
Declaration of 1D Array consists of any data type and it can be declared by any
random name or any random variable. Syntax:
int arr[10];
Suppose we want to store the age of 10 students. In that case, we have to declare
10 variables in our C program to store the age of 10 students.
Now here comes the use of a one-dimensional array. With the help of 1D Array,
we will declare a single variable in our C program that can store the age of 10
students at a time.
(OR)
Once we declare the 1D Array, it will look like as shown in the picture
below:
In above image we can see that the name of the one dimensional array
is a and it can store 5 integer numbers. Size of the array is 5. Index of the
array is 0, 1, 2, 3 and 4.
The first index is called Lower Bound, and the last index is called an Upper
Bound. Upper Bound of a one dimensional is always Size – 1.
- "{element1, element2, ..., elementN}' specifies the values of the elements in the
array. The number of elements must be equal to "arraySize'.
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.
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
for(int i=0; i<5; i++)
{
printf("numbers[%d] = %d\n", i, numbers[i]);
}
return 0;
}
Sub : PPS Unit 2 Notes
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
Explanation:
arrayName[index] = value;
#include <stdio.h>
int main() {
int numbers[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
for(int i=0; i<5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}
Sub : PPS Unit 2 Notes
return 0;
}
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
Explanation:
Linear Search
A Linear Search, which is also popularly known as a sequential search, is a
process for finding an item in a list of items. This sort of searching algorithm
checks each element of the list one by one until a match is found or the entire
list is searched.
In worst-case scenarios, the Linear Search takes linear time and performs at
most n comparisons, where n is the list's length.
3. If the element we're looking for doesn't match, move on to the next one.
Return -1, if there is no match or the search element is not found in the given array.
These data structures could be array, string, Linked-List, Stack, Queue, or any other
data structure.
Array:
24 56 -1 90 4 12 34 89 37
0 1 2 3 4 5 6 7 8
Solution:
Index Number Working
7 89
8 37
Because 6 is the index of our element (34), our answer is 6 for 0-based indexing arrays
and 7 for 1-based indexing arrays. This is how linear search works in an array.
Searching is the method of finding a certain item in a list of items. If the element is
found in the list, the process is considered successful, and the location of that
element is returned; otherwise, the search is considered unsuccessful.
1. Set j to 1
2. If j > n, jump to step 7
Sub : PPS Unit 2 Notes
#include <stdio.h>
int main()
scanf("%d",&number); // 10
scanf("%d",&array[c]); array[10]
scanf("%d",&search);
break;
}
Sub : PPS Unit 2 Notes
if ( c == number )
return 0;
OUTPUT:
A 2D array is like a matrix and has a row and a column of elements ( Although in
memory these are stored in contiguous memory locations).
A 1-D array, is a linear list of data and needed only one index to access the element
like a[2]. To access a two-dimensional array we need a pair of indices one for the
row and another for the column like a[1][2].
Similar to a one-dimensional array, we have the same name for all the elements
present in the matrix.
The difference that we have here is that a two-dimensional array is not linear in
nature.
The following figure illustrates the difference between a one-dimensional array and a two-
dimensional array:
Sub : PPS Unit 2 Notes
In the above figure, we can clearly see that the 2D array has two dimensions just
like any two-dimensional figure such as a square or a rectangle.
Also, the number of rows and the number of columns in the 2D array are
represented by this format:
2D array format
A matrix can also have specifications like 3×4, 2×1, etc. Let us learn more about
the 2D arrays.
A 2D array needs to be declared so that the compiler gets to know what type of data
is being stored in the array.
Similar to 1D array, a 2D array can also be declared as an int, char, float, double,
etc. Here is how we declare a 2D array(here integer array):
2D array declaration
int num[10][5];
The ‘int’ specifies that the data stored in the array will be of integer type.
‘num’ is the variable name under which all the data is stored.
[10] refers to the number of rows of the array and
[5] refers to the number of columns of the array.
This is also a static memory allocation, that is, we are allocating the array a size
equal to 10 x 5, that is, in this array, we can store 10 x 5 = 50 number of elements.
The actual size it will occupy in memory will depend on the data type of the
array i.e. (size of array = number of elements it can hold x Size of datatype).
The two brackets i.e. [][] specify that the array is two-dimensional.
int num[3][3]={{25,10,5},{4,6,13},{45,90,78}};
int num[3][3]={{25,10,5}, //row0
{4,6,13}, //row1
{45,90,78} //row2
};
//this format also stores data in the array but results in a loss of readability
int num[2][4]={4,6,8,10,3,5,7,9};
Another important fact is that when initializing a 2D array, specifying the number
of rows of the array is optional but specifying the number of columns of the
array is important and mandatory.
Example :
#include <stdio.h>
int main(void) {
Sub : PPS Unit 2 Notes
int *p[2];
p[0] = arr[0];
p[1] = arr[1];
printf("\n");
return 0;
Output
31 12 11
81 91 110
Binary Search
A binary search program in C is a commonly used search algorithm in computer
programming. It is an efficient algorithm that allows finding a target value within
a sorted array or list by repeatedly dividing the search interval in half. In a binary
search program in C, the algorithm starts by comparing the target value with the
middle element of the array. If the middle element is equal to the target value,
the search is complete. If not, the search interval is divided in half again, and
the search continues in the appropriate subarray until the target value is found
Sub : PPS Unit 2 Notes
or the subarray is empty. The binary search algorithm has a time complexity of
O(log n), making it much faster than linear search for large arrays or lists.
To understand the working of the Binary search algorithm, let's take a sorted array. It
will be easy to understand the working of Binary search with an example.
o Iterative method
o Recursive method
The recursive method of binary search follows the divide and conquer approach.
Sub : PPS Unit 2 Notes
We have to use the below formula to calculate the mid of the array -
beg = 0
end = 8
Now, the element to search is found. So algorithm will return the index of the element
matched.
In linear search, input data Whereas, in binary search, input data has to be
doesn’t need to be sorted . sorted according to the order.
It is also referred as
It is also referred to as half-interval search.
sequential search.
The time complexity of the The time complexity of the binary search is
linear search is O(n) 0 (logn)
It operates equality
Binary search operates ordering comparisons
comparisons
Linear search is less complex Binary search is more complex and has a fast
and involves a slow process process
#include <stdio.h>
int main() {
int values[5];
printf("%d\n", values[i]);
return 0;
}
Enter 5 integers : 1
-3
34
0
3
Displaying integers : 1
Sub : PPS Unit 2 Notes
-3
34
0
3
array example
1. #include<stdio.h>
2. int main(){
3. int i=0;
4. int marks[5];//declaration of array
5. marks[0]=80;//initialization of array
6. marks[1]=60;
7. marks[2]=70;
8. marks[3]=85;
9. marks[4]=75;
10. //traversal of array
11. for(i=0;i<5;i++){
12. printf("%d \n",marks[i]);
13. }//end of for loop
14. return 0;
15. }
Output
80
60
70
85
75