0% found this document useful (0 votes)
33 views6 pages

Experiment 1

The document describes an experiment to implement a list using an array in C. 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 from user input, displays a menu to call the functions, and loops until the user exits. 3. An explanation of how each function works, such as searching by iterating the array, deleting by shifting elements, and inserting by making space. The program allows the user to perform list operations on an array to demonstrate its functionality as a list.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

Experiment 1

The document describes an experiment to implement a list using an array in C. 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 from user input, displays a menu to call the functions, and loops until the user exits. 3. An explanation of how each function works, such as searching by iterating the array, deleting by shifting elements, and inserting by making space. The program allows the user to perform list operations on an array to demonstrate its functionality as a list.

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 DOCX, 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 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:

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


2. Text editor or Integrated Development Environment (IDE) for writing and editing
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

// Function to create an array


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

printf("Enter the elements:\


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

return size;
}

// Function to display the array


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

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

// Function to delete an element from the array


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

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

// Function to insert an element into the array


int insertElement(int arr[], int *size, int position, int element)
{ if (*size == MAX_SIZE) {
printf("Array is full. Cannot insert element.\n");
return 0; // Insertion unsuccessful
}
if (position < 0 || position > *size) {
printf("Invalid position for insertion.\
n"); return 0; // Insertion unsuccessful
}

// Shift elements to the right to make space for the new


element for (int i = *size; i > position; i--) {
arr[i] = arr[i - 1];
}

// Insert the new element at the specified position


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

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

size = createArray(arr, size);

int choice, key, position, element;

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

} while (choice != 5);

return 0;
}

Theory(Code Explanation):

1. Macro Definition:

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

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:

You might also like