Experiment No. - 2A: Theory
Experiment No. - 2A: Theory
Experiment No. - 2A: Theory
– 2A
AIM: To demonstrate the creation of array, addition of an element and displaying the elements
from one dimensional array.
THEORY : an array is a collection of elements of the same type stored in contiguous memory
locations. This organization allows efficient access to elements using their index. Arrays can also be
of different types depending upon the direction/dimension they can store the elements.
One-Dimensional Arrays In C
A one-dimensional array can be viewed as a linear sequence of elements. We can only increase or
decrease its size in a single direction.
Syntax
elements_type array_name[array_size];
Declaration
data_type array_name[array_size];
Initialization
int numbers[5] = {10, 20, 30, 40, 50};
Program :
#include <stdio.h>
int main() {
// Step 1: Create a one-dimensional array
int array[6] = {10, 20, 30, 40, 50}; // Initial size is 6 to accommodate one additional element
int new_element = 60; // Element to be added
int size = 5; // Current number of elements in the array
return 0;
}
Output :
Original array: 10 20 30 40 50
10 20 30 40 50 60
Conclusion : Thus, We have successfully performed the creation of array, addition of an element
and displaying the elements from one dimensional array.
Experiment No. – 2B
AIM : To demonstarte 2 D Array declaration, Input and output array elements for displaying
multiplication table.
THEORY : 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.
data_type array_name[rows][columns];
Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the array if the declaration and
initialization are being done simultaneously. However, this will not work with 2D arrays.
We will have to define at least the second dimension of the array. The two-dimensional
array can be declared and defined in the following way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Program :
#include <stdio.h>
int main() {
int rows, cols;
return 0;
}
Output :
Enter the number of rows for the multiplication table: 5
Enter the number of columns for the multiplication table: 5
Multiplication Table:
12 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Experiment No. – 5A
THEORY :
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.
If each element has an equal chance of being searched, Linear Search has an average case of n+1/2
comparisons, but the average case can be influenced if the search probability for each element differs.
Linear Search is rarely feasible as other search algorithms and schemes (such as the binary search
algorithm and hash tables) that provide significantly faster search operation for all lists.
1. Start
2. linear_search ( array, value)
3. For each element in the array // loop the element till the size
5. return key
6. end if
7. end for
8. end
Program :
#include <stdio.h>
int main() {
int arr[100], size, target, i, found = 0;
if (!found) {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
Output :
Experiment No. – 5B
THEORY :
Binary Search is an interval searching algorithm that searches for an item in the sorted list. It works
by repeatedly dividing the list into two equal parts and then searching for the item in the part where it
can possibly exist.
Let key be the element we are searching for, and the array be sorted in the ascending order.
1. Compare key with the middle element of the array.
2. If key matches with the middle element, we return the index of the middle element.
3. Else if key is greater than the middle element, it means that key can only lie in the right half
subarray after the middle element. So we repeat steps 1 to 4 for the right half subarray and leave
the left half subarray.
4. Else if key is smaller than the middle element, it means that target can only lie in the left half
subarray before the middle element. So, we repeat steps 1 to 4 for the left half subarray.
5. We will keep doing that till we find key or there is no element left in the subarray being
considered.
PROGRAM :
#include <stdio.h>
if (arr[mid] == target) {
return mid; // Target found
}
if (arr[mid] < target) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}
return -1; // Target not found
}
int main() {
int arr[] = {10, 20, 30, 40, 50}; // Sorted array
int size = sizeof(arr) / sizeof(arr[0]);
int target;
// Input the target element to search
printf("Enter the element to search: ");
scanf("%d", &target);
return 0;
}
Output :