0% found this document useful (0 votes)
21 views

Experiment 1

The document describes an experiment to implement list operations using an array. It includes: 1. Defining functions to create an array, display the array, search for an element, delete an element, and insert an element. 2. A main function that populates an array, displays a menu to call the above functions, and loops until the user exits. 3. Explanations of how each function works, such as searching by iterating the array, deleting by shifting elements, and inserting by making space.

Uploaded by

abeetpaul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Experiment 1

The document describes an experiment to implement list operations using an array. It includes: 1. Defining functions to create an array, display the array, search for an element, delete an element, and insert an element. 2. A main function that populates an array, displays a menu to call the above functions, and loops until the user exits. 3. Explanations of how each function works, such as searching by iterating the array, deleting by shifting elements, and inserting by making space.

Uploaded by

abeetpaul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment-1 (EC-102)

Dated: January 10 ,2024


Course Instructor: Dr. Chhavi Dhiman

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:

1. Personal Computer (PC) with a C language compiler installed


2. Text editor or Integrated Development Environment (IDE) for wri1ng and edi1ng C
code (e.g., Visual Studio Code).
3. Basic understanding of C programming concepts.

Code:
#include <stdio.h>

// Maximum size of the array


#define MAX_SIZE 100

// Func1on to create an array


int createArray(int arr[], int size) {
prin]("Enter the number of elements (up to %d): ", MAX_SIZE);
scanf("%d", &size);

prin]("Enter the elements:\n");


for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

return size;
}

// Func1on to display the array


void displayArray(int arr[], int size) {
prin]("Array elements: ");
for (int i = 0; i < size; i++) {
prin]("%d ", arr[i]);
}
prin]("\n");
}

// Func1on to search for an element in the array


int searchElement(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Element found, return its index
}
}
return -1; // Element not found
}

// Func1on to delete an element from the array


int deleteElement(int arr[], int *size, int key) {
int index = searchElement(arr, *size, key);

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
}
}

// Func1on to insert an element into the array


int insertElement(int arr[], int *size, int posi1on, int element) {
if (*size == MAX_SIZE) {
prin]("Array is full. Cannot insert element.\n");
return 0; // Inser1on unsuccessful
}
if (posi1on < 0 || posi1on > *size) {
prin]("Invalid posi1on for inser1on.\n");
return 0; // Inser1on 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];
}

// Insert the new element at the specified posi1on


arr[posi1on] = element;
(*size)++;
return 1; // Inser1on successful
}

int main() {
int arr[MAX_SIZE];
int size = 0;

size = createArray(arr, size);

int choice, key, posi1on, element;

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

} while (choice != 5);

return 0;
}

Theory(Code Explana+on):

1. Macro Defini4on:

• Defines a macro MAX_SIZE to represent the maximum size of the array.

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:

You might also like