You are on page 1of 9

Name: Mithilesh Singh

Reg No: 221070068

EXPERIMENT No: 1
Operations on arrays

AIM:

The primary objective of this experiment is to develop and implement


a modular C++ program for array manipulation. The program allows
users to dynamically input, display, search for elements, sort, delete,
and insert elements in an integer array.

THEORY:

The input function prompts the user to enter elements into an array of
a specified size, while the output function displays the array
elements.

The search function enables users to input an element and checks if


it exists in the array, providing the index if found.

The sort function utilizes the bubble sort algorithm to arrange the
array elements in ascending order.

The del function deletes an element from the array based on the user-
specified index, shifting subsequent elements to fill the gap.
Conversely, the insert function inserts a user-provided element at a
specified index, shifting existing elements to accommodate the new
entry.

ALGORITHM:
1. Initialization: Initialize an array arr of size MAX and variables n, size, t, a, b, c, d, e.
2. Input Initial Size and Elements: Ask the user for the initial size of the array and its
elements. Store the size in n and the elements in the first n positions of arr.
3. Menu Display and Operation Selection: Display a menu with options for insertion,
deletion, sorting, searching, and exit. Ask the user to select an operation.
4. Perform Selected Operation: Based on the user’s selection, perform the corresponding
operation:
o Insertion: Ask the user for the number of elements to be inserted and their
values. For each value, call the insert function to insert it at the end of the array
and increment size.
o Deletion: Ask the user for the number to be deleted. Call the del function to
delete the number from the array. If the number is found and deleted,
decrement size.
o Sorting: Call the bubbleSort function to sort the array in ascending order.
o Searching: Ask the user for the number to be searched. Call the search function
to find the index of the number in the array.
5. Display Array: After each operation (except exit), display the current elements of the
array using the display function.
6. Repeat or Exit: If the user selects exit, end the program. Otherwise, go back to step 3.
The functions used in the program are:
• bubbleSort: This function sorts the array in ascending order using the Bubble Sort
algorithm. It has a time complexity of O(n^2).
• insert: This function inserts a given number at the end of the array. It has a time
complexity of O(1).
• del: This function deletes a given number from the array by shifting all elements after it
one position to the left. It has a time complexity of O(n).
• search: This function finds a given number in the array and returns its index. It has a
time complexity of O(n).
• display: This function prints all elements of the array. It has a time complexity of O(n).

CODE:
Output:
EXAMPLE:

Suppose you want to manipulate an array of integers. Here's an example


interaction with the program:

1. Enter the size of the array: 5

2. Enter the elements of the array


13254

3. Displaying elements
13254

4. Enter the element you want to search: 2


Element 2 is at index 2 of the array

5. The array sorted in ascending order is


12345

6. Enter the index no. of the array you want to delete: 3


Displaying elements after deletion
1235

7. Enter the no. you want to insert: 6


Displaying elements after insertion
12356

In this example, the user first specifies the size of the array (step 1), then
inputs the elements of the array (step 2). The program displays the entered
elements (step 3) and allows the user to search for a specific element (step
4).

After that, the program sorts the array in ascending order (step 5), deletes
an element at a specified index (step 6), and finally inserts a new element
at a specified index (step 7).

CONCLUSION:

Thus we have successfully implemented simple array operations in this


experiment. The overall time complexity of the program is determined by the operation that
the user chooses to perform the most, as all operations are performed in constant time except
for sorting, which takes O(n^2) time. The space complexity of the program is O(n), where n is
the size of the array.

You might also like