Professional Documents
Culture Documents
Experiment 1
Experiment 1
Experiment 1
Aim:
Write a program to perform the following operations: Create an array of elements, delete
any element, search any element, Insertion of any element and display the results to
implement list using an array.
Objectives:
1. Create an array of elements.
2. Implement a function to delete a specified element from the array.
3. Implement a function to search for a given element in the array.
4. Implement a function to insert a new element at a specified position in the array.
5. Display the array after each operation to demonstrate the results.
Materials Required:
Code:
#include <stdio.h>
return size;
}
if (index != -1) {
// Shift elements to the left 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, deletion unsuccessful
}
}
int main() {
int arr[MAX_SIZE];
int size = 0;
do {
printf("\nMenu:\n");
printf("1. Display Array\n");
printf("2. Search Element\n");
printf("3. Delete Element\n");
printf("4. Insert Element\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
displayArray(arr, size);
break;
case 2:
printf("Enter the element to search: ");
scanf("%d", &key);
int index = searchElement(arr, size, key);
if (index != -1) {
printf("Element found at index %d.\n", index);
} else {
printf("Element not found.\n");
}
break;
case 3:
printf("Enter the element to delete: ");
scanf("%d", &key);
if (deleteElement(arr, &size, key)) {
printf("Element deleted successfully.\n");
} else {
printf("Element not found. Deletion unsuccessful.\n");
}
break;
case 4:
printf("Enter the position for insertion: ");
scanf("%d", &position);
printf("Enter the element to insert: ");
scanf("%d", &element);
if (insertElement(arr, &size, position, element)) {
printf("Element inserted successfully.\n");
}
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
return 0;
}
Theory(Code Explanation):
1. Macro Definition:
2. Function Definitions:
createArray Function:
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 Function:
Takes an array and its size as parameters.
Prints the elements of the array using a loop.
searchElement Function:
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 Function:
Takes an array, a pointer to its size, and an element to delete as parameters.
Calls the searchElement function to find the index of the element to be deleted.
If the element is found, shifts the elements to the left 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 Function:
Takes an array, a pointer to its size, the position for insertion, and the element
to insert as parameters.
Checks if the array is full and if the insertion position is valid.
Shifts elements to the right to make space for the new element and inserts
the element at the specified position.
Increments the size.
Returns 1 if the insertion is successful, 0 otherwise.
3. main Function:
Declares an array and initializes the variable representing the array size.
Calls the createArray function to populate the array with user input.
Uses a do-while loop to display a menu and execute user-selected operations
until the user chooses to exit.
The menu includes options to display the array, search for an element, delete
an element, insert an element, and exit.
Calls corresponding functions based on the user's choice.
Continues to loop until the user selects the exit option.
Output: