Experiment 1
Experiment 1
Experiment 1
Aim:
Write a program to perform the following opera1ons: Create an array of elements, delete
any element, search any element, Inser1on of any element and display the results to
implement list using an array.
Objec+ves:
1. Create an array of elements.
2. Implement a func1on to delete a specified element from the array.
3. Implement a func1on to search for a given element in the array.
4. Implement a func1on to insert a new element at a specified posi1on in the array.
5. Display the array aDer each opera1on to demonstrate the results.
Materials Required:
Code:
#include <stdio.h>
return size;
}
if (index != -1) {
// ShiD elements to the leD to overwrite the deleted element
for (int i = index; i < *size - 1; i++) {
arr[i] = arr[i + 1];
}
(*size)--;
return 1; // Element deleted successfully
} else {
return 0; // Element not found, dele1on unsuccessful
}
}
// ShiD elements to the right to make space for the new element
for (int i = *size; i > posi1on; i--) {
arr[i] = arr[i - 1];
}
int main() {
int arr[MAX_SIZE];
int size = 0;
do {
prin]("\nMenu:\n");
prin]("1. Display Array\n");
prin]("2. Search Element\n");
prin]("3. Delete Element\n");
prin]("4. Insert Element\n");
prin]("5. Exit\n");
prin]("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
displayArray(arr, size);
break;
case 2:
prin]("Enter the element to search: ");
scanf("%d", &key);
int index = searchElement(arr, size, key);
if (index != -1) {
prin]("Element found at index %d.\n", index);
} else {
prin]("Element not found.\n");
}
break;
case 3:
prin]("Enter the element to delete: ");
scanf("%d", &key);
if (deleteElement(arr, &size, key)) {
prin]("Element deleted successfully.\n");
} else {
prin]("Element not found. Dele1on unsuccessful.\n");
}
break;
case 4:
prin]("Enter the posi1on for inser1on: ");
scanf("%d", &posi1on);
prin]("Enter the element to insert: ");
scanf("%d", &element);
if (insertElement(arr, &size, posi1on, element)) {
prin]("Element inserted successfully.\n");
}
break;
case 5:
prin]("Exi1ng program.\n");
break;
default:
prin]("Invalid choice. Please enter a valid op1on.\n");
}
return 0;
}
Theory(Code Explana+on):
1. Macro Defini4on:
2. Func4on Defini4ons:
createArray Func@on:
• Takes an array and its current size as parameters.
• Prompts the user to enter the number of elements and their values.
• Returns the updated size of the array.
displayArray Func@on:
• Takes an array and its size as parameters.
• Prints the elements of the array using a loop.
searchElement Func@on:
• Takes an array, its size, and an element to search as parameters.
• Uses a loop to iterate through the array, checking if the current element matches the
key.
• Returns the index of the element if found, otherwise returns -1.
deleteElement Func@on:
• Takes an array, a pointer to its size, and an element to delete as parameters.
• Calls the searchElement func1on to find the index of the element to be deleted.
• If the element is found, shiDs the elements to the leD to overwrite the deleted
element and decrements the size.
• Returns 1 if the element is deleted successfully, 0 if the element is not found.
insertElement Func@on:
• Takes an array, a pointer to its size, the posi1on for inser1on, and the element to
insert as parameters.
• Checks if the array is full and if the inser1on posi1on is valid.
• ShiDs elements to the right to make space for the new element and inserts the
element at the specified posi1on.
• Increments the size.
• Returns 1 if the inser1on is successful, 0 otherwise.
3. main Func4on:
• Declares an array and ini1alizes the variable represen1ng the array size.
• Calls the createArray func1on to populate the array with user input.
• Uses a do-while loop to display a menu and execute user-selected opera1ons un1l
the user chooses to exit.
• The menu includes op1ons to display the array, search for an element, delete an
element, insert an element, and exit.
• Calls corresponding func1ons based on the user's choice.
• Con1nues to loop un1l the user selects the exit op1on.
Output: