Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Experiment No. - 2A: Theory

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Experiment No.

– 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

To declare a one-dimensional array in C, you use the following syntax:

data_type array_name[array_size];

 data_type: The type of data to store (e.g., int, float, char).


 array_name: The name of the array.
 array_size: The number of elements the array can hold.

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

// Step 2: Display the original elements of the array


printf("Original array:\n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");

// Step 3: Add an element to the array


array[size] = new_element; // Add the new element at the end
size++; // Increment the size of the array

// Step 4: Display the updated elements of the array


printf("Updated array after adding %d:\n", new_element);
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");

return 0;
}

Output :

Original array: 10 20 30 40 50

Updated array after adding 60:

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.

Declaration of two dimensional Array in C


The syntax to declare the 2D array is given below.

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;

// Step 1: Get the size of the multiplication table


printf("Enter the number of rows for the multiplication table: ");
scanf("%d", &rows);
printf("Enter the number of columns for the multiplication table: ");
scanf("%d", &cols);

// Step 2: Declare a 2D array


int table[rows][cols];

// Step 3: Fill the array with multiplication values


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
table[i][j] = (i + 1) * (j + 1); // Fill with multiplication values
}
}

// Step 4: Display the multiplication table


printf("\nMultiplication Table:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%4d ", table[i][j]); // Print each element with spacing
}
printf("\n"); // New line for each row
}

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

AIM : To demonstrate the implementation of linear search

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.

Linear Search Algorithm in C

1. Start
2. linear_search ( array, value)

3. For each element in the array // loop the element till the size

4. If (searched element == value)

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;

// Input the size of the array


printf("Enter the number of elements in the array: ");
scanf("%d", &size);

// Input array elements


printf("Enter %d elements:\n", size);
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

// Input the target element to search


printf("Enter the element to search: ");
scanf("%d", &target);

// Linear search implementation


for (i = 0; i < size; i++) {
if (arr[i] == target) {
printf("Element %d found at index %d.\n", target, i);
found = 1; // Set found flag
break; // Exit the loop if found
}
}

if (!found) {
printf("Element %d not found in the array.\n", target);
}

return 0;
}

Output :

Enter the number of elements in the array: 5


Enter 5 elements: 10 20 30 40 50
Enter the element to search: 30
Element 30 found at index 2.

Experiment No. – 5B

AIM : To demonstrate the implementation of Binary search.

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.

Algorithm for Binary Search in C

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>

// Function to perform binary search


int binarySearch(int arr[], int size, int target) {
int left = 0, right = size - 1;

while (left <= right) {


int mid = left + (right - left) / 2;

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);

// Perform binary search


int result = binarySearch(arr, size, target);

// Output the result


if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}

return 0;
}

Output :

Enter the element to search: 30

Element 30 found at index 2.

You might also like