0% found this document useful (0 votes)
8 views9 pages

WORD FILE 71218

C code
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)
8 views9 pages

WORD FILE 71218

C code
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/ 9

NAME: TALIB HUSSAIN

71218

PF ASSINGMENT 02

#include <stdio.h>

// Function to search for an element in the array

int searchArray(int arr[100], int size, int target) {

for (int i = 0; i < size; i++) {

if (arr[i] == target) {

return i;

// Return the index of the target element

return -1;

// Return -1 if the target element is not found

// Function to delete an element from the array by value

void deleteByValue(int arr[100], int* size, int target) {

int index = searchArray(arr, *size, target);

if (index != -1) {

for (int i = index; i < *size - 1; i++) {

arr[i] = arr[i + 1];

(*size)--;

printf("Element %d deleted successfully.\n", target);

} else {

printf("Element %d not found in the array.\n", target);


}

// Function to delete an element from the array by index

void deleteByIndex(int arr[100], int* size, int index) {

if (index >= 0 && index < *size) {

for (int i = index; i < *size - 1; i++) {

arr[i] = arr[i + 1];

(*size)--;

printf("Element at index %d deleted successfully.\n", index);

} else {

printf("Invalid index.\n");

// Function to insert an element at the end of the array

void insertAtEnd(int arr[100], int* size, int capacity, int element) {

if (*size < capacity) {

arr[*size] = element;

(*size)++;

printf("Element %d inserted successfully at the end.\n", element);

} else {

printf("Array is full. Cannot insert more elements.\n");

// Function to insert an element at the beginning of the array

void insertAtBeginning(int arr[100], int* size, int capacity, int element) {


if (*size < capacity) {

for (int i = *size; i > 0; i--) {

arr[i] = arr[i - 1];

arr[0] = element;

(*size)++;

printf("Element %d inserted successfully at the beginning.\n",


element);

} else {

printf("Array is full. Cannot insert more elements.\n");

// Function to insert an element at a specific index in the array

void insertAtIndex(int arr[100], int* size, int capacity, int index, int
element) {

if (*size < capacity && index >= 0 && index <= *size) {

for (int i = *size; i > index; i--) {

arr[i] = arr[i - 1];

arr[index] = element;

(*size)++;

printf("Element %d inserted successfully at index %d.\n", element,


index);

} else {

printf("Invalid index or array is full.\n");

int main() {
int arr[100];

int size = 0;

int capacity = 100;

printf("**********************************************\n");

printf("WELCOME TO PROGRAM OF ARRAY MANIPULATION\n");

printf("**********************************************\n");

while (1) {

printf("Choose the operation you wish to perform:\n");

printf("Press 1 for Searching in Array.\n");

printf("Press 2 for Deletion in Array.\n");

printf("Press 3 for Insertion in Array.\n");

printf("Press 4 for Exit.\n");

printf("Enter your choice: ");

int choice;

scanf("%d", &choice);

switch (choice) {

case 1: {

printf("Enter the element to search: ");

int target;

scanf("%d", &target);

int result = searchArray(arr, size, target);

if (result != -1) {

printf("Element %d found at index %d.\n", target, result);

} else {

printf("Element %d not found in the array.\n", target);

break;
}

case 2: {

printf("Press 1 for Deletion by value.\n");

printf("Press 2 for Deletion by index.\n");

printf("Press 3 for return to main menu.\n");

printf("Enter your choice: ");

int deleteChoice;

scanf("%d", &deleteChoice);

switch (deleteChoice) {

case 1: {

printf("Enter the value to delete: ");

int value;

scanf("%d", &value);

deleteByValue(arr, &size, value);

break;

case 2: {

printf("Enter the index to delete: ");

int index;

scanf("%d", &index);

deleteByIndex(arr, &size, index);

break;

case 3: {

break;

default:

printf("Invalid choice. Returning to main menu.\n");


break;

break;

case 3: {

printf("Press 1 for Insertion at the end.\n");

printf("Press 2 for Insertion at the beginning.\n");

printf("Press 3 for Insertion at a specific index.\n");

printf("Press 4 for return to main menu.\n");

printf("Enter your choice: ");

int insertChoice;

scanf("%d", &insertChoice);

switch (insertChoice) {

case 1: {

printf("Enter the element to insert at the end: ");

int element;

scanf("%d", &element);

insertAtEnd(arr, &size, capacity, element);

break;

case 2: {

printf("Enter the element to insert at the beginning: ");

int element;

scanf("%d", &element);

insertAtBeginning(arr, &size, capacity, element);

break;

case 3: {
printf("Enter the index to insert the element: ");

int index;

scanf("%d", &index);

printf("Enter the element to insert: ");

int element;

scanf("%d", &element);

insertAtIndex(arr, &size, capacity, index, element);

break;

case 4: {

break;

default:

printf("Invalid choice. Returning to main menu.\n");

break;

break;

case 4:

printf("Exiting program. Goodbye!\n");

return 0;

default:

printf("Invalid choice. Please try again.\n");

break;

return 0;

}
OUTPUT SCREENSHOTS
THIS CODE EXECUTE FROM ONLINE
COMPILER
TALIB HUSSAIN 71218

You might also like