Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

PRACTICAL 1

AIM- Write C Program to perform following operations on Array: Create,Insert,Delete,Display

#include <stdio.h>

#include <stdlib.h>

void display(int *arr, int size) {

if (size == 0) {

printf("Array is empty.\n");

return;

printf("Array elements: ");

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

printf("%d ", arr[i]);

printf("\n");

int insert(int **arr, int *size, int element, int position) {

if (position < 0 || position > *size) {

printf("Invalid position.\n");

return -1;

*arr = realloc(*arr, (*size + 1) * sizeof(int));

if (*arr == NULL) {

printf("Memory allocation failed.\n");

return -1;

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

(*arr)[i] = (*arr)[i - 1];

}
(*arr)[position] = element;

(*size)++;

return 0;

int delete(int **arr, int *size, int position) {

if (position < 0 || position >= *size) {

printf("Invalid position.\n");

return -1;

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

(*arr)[i] = (*arr)[i + 1];

*arr = realloc(*arr, (*size - 1) * sizeof(int));

if (*arr == NULL && *size > 1) {

printf("Memory allocation failed.\n");

return -1;

(*size)--;

return 0;

int main() {

int *arr = NULL;

int size = 0;

int choice, element, position;

while (1) {

printf("\nMenu:\n");

printf("1. Create\n");
printf("2. Insert\n");

printf("3. Delete\n");

printf("4. Display\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Creating array. Enter the number of elements: ");

scanf("%d", &size);

arr = (int *)malloc(size * sizeof(int));

if (arr == NULL) {

printf("Memory allocation failed.\n");

return -1;

printf("Enter the elements:\n");

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

scanf("%d", &arr[i]);

break;

case 2:

printf("Enter element to insert: ");

scanf("%d", &element);

printf("Enter position to insert (0 to %d): ", size);

scanf("%d", &position);

insert(&arr, &size, element, position);

break;
case 3:

printf("Enter position to delete (0 to %d): ", size - 1);

scanf("%d", &position);

delete(&arr, &size, position);

break;

case 4:

display(arr, size);

break;

case 5:

free(arr);

return 0;

default:

printf("Invalid choice.\n");

return 0;

PRACTICAL NO.2

AIM- Write a ‘C’ Program to search a particular data from the given array of numbers using :
Linear Search Method

#include <stdio.h>

int linearSearch(int arr[], int size, int target) {

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

if (arr[i] == target) {

return i;

return -1;

}
int main() {

int size, target;

printf("Enter the number of elements in the array: ");

scanf("%d", &size);

int arr[size];

printf("Enter the elements of the array:\n");

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

scanf("%d", &arr[i]);

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

scanf("%d", &target);

int result = linearSearch(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);

return 0;

PRACTICAL 3

AIM- Write C Program to search a particular data from the given array of strings using Linear
Search Method

#include <stdio.h>

#include <string.h>

#define MAX_STRINGS 100

#define MAX_LENGTH 100

int linearSearch(char arr[][MAX_LENGTH], int size, const char *target) {

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

if (strcmp(arr[i], target) == 0) {
return i;

return -1;

int main() {

int size;

char arr[MAX_STRINGS][MAX_LENGTH]; // Array of strings

char target[MAX_LENGTH]; // String to search for

printf("Enter the number of strings (up to %d): ", MAX_STRINGS);

scanf("%d", &size);

getchar();

printf("Enter the strings:\n");

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

fgets(arr[i], MAX_LENGTH, stdin);

arr[i][strcspn(arr[i], "\n")] = '\0';

printf("Enter the string to search for: ");

fgets(target, MAX_LENGTH, stdin);

target[strcspn(target, "\n")] = '\0';

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

if (result != -1) {

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

} else {

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

}
return 0;

PRACTICAL NO.4

AIM- Write a ‘C” program to search a particular data from the given array of numbers using
Binary Search method

#include <stdio.h>

int binarySearch(int arr[], int size, int target) {

int left = 0;

int right = size - 1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == target) {

return mid;

if (arr[mid] < target) {

left = mid + 1;

else {

right = mid - 1;

return -1;

int main() {

int arr[] = {2, 3, 4, 10, 40, 50, 60};

int size = sizeof(arr) / sizeof(arr[0]);

int target;

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

scanf("%d", &target);
int result = binarySearch(arr, size, target);

if (result != -1) {

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

} else {

printf("Element not found in the array.\n");

return 0;

PRACTICAL NUMBER 5

AIM- Write a ‘C” program to search a particular data from the given array of strings using
Binary Search Method

#include <stdio.h>

#include <string.h>

int binarySearch(char *arr[], int size, char *target) {

int left = 0;

int right = size - 1;

while (left <= right) {

int mid = left + (right - left) / 2;

int comparison = strcmp(arr[mid], target);

if (comparison == 0) {

return mid;

} else if (comparison < 0) {

left = mid + 1;

} else {

right = mid - 1;

return -1;

}
int main() {

char *arr[] = {"apple", "banana", "cherry", "date", "fig", "grape", "kiwi"};

int size = sizeof(arr) / sizeof(arr[0]);

char target[20];

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

scanf("%s", target);

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

if (result != -1) {

printf("String found at index: %d\n", result);

} else {

printf("String not found in the array.\n");

return 0;

PRACTICAL NO 6

AIM- Write a ‘C” Program to sort an array of numbers using Bubble Sort Method

#include <stdio.h>

void bubbleSort(int arr[], int size) {

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

int swapped = 0;

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

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

swapped = 1;

}
}

if (swapped == 0) {

break;

void printArray(int arr[], int size) {

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int size = sizeof(arr) / sizeof(arr[0]);

printf("Original array: \n");

printArray(arr, size);

bubbleSort(arr, size);

printf("Sorted array: \n");

printArray(arr, size);

return 0;

PRACTICAL NO 7

AIM- Write a ‘C’ Program to Sort an Array of Strings using Bubble Sort Method

#include <stdio.h>

#include <string.h>

void bubbleSort(char *arr[], int size) {

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


int swapped = 0;

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

if (strcmp(arr[j], arr[j + 1]) > 0) {

char *temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

swapped = 1;

if (swapped == 0) {

break;

void printArray(char *arr[], int size) {

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

printf("%s ", arr[i]);

printf("\n");

int main() {

char *arr[] = {"banana", "apple", "cherry", "date", "fig", "grape", "kiwi"};

int size = sizeof(arr) / sizeof(arr[0]);

printf("Original array: \n");

printArray(arr, size);

bubbleSort(arr, size);

printf("Sorted array: \n");


printArray(arr, size);

return 0;

PRACTICAL 8

AIM- Write a ‘C’Program to sort an Array of numbers using Selection Sort Method

#include <stdio.h>

void selectionSort(int arr[], int size) {

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

int minIndex = i;

for (int j = i + 1; j < size; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j; // Update minIndex if a smaller element is found

if (minIndex != i) {

int temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

void printArray(int arr[], int size) {

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

printf("%d ", arr[i]);

printf("\n");

}
int main() {

int arr[] = {64, 25, 12, 22, 11, 90};

int size = sizeof(arr) / sizeof(arr[0]);

printf("Original array: \n");

printArray(arr, size);

selectionSort(arr, size);

printf("Sorted array: \n");

printArray(arr, size);

return 0;

PRACTICAL9

AIM- Write a ‘C’ Program to sort an Array of Strings using Selection Sort Method

#include <stdio.h>

#include <string.h>

void selectionSort(char *arr[], int size) {

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

int minIndex = i;

for (int j = i + 1; j < size; j++) {

if (strcmp(arr[j], arr[minIndex]) < 0) {

minIndex = j; // Update minIndex if a smaller string is found

if (minIndex != i) {

char *temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

}
}

void printArray(char *arr[], int size) {

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

printf("%s ", arr[i]);

printf("\n");

int main() {

char *arr[] = {"banana", "apple", "cherry", "date", "fig", "grape", "kiwi"};

int size = sizeof(arr) / sizeof(arr[0]);

printf("Original array: \n");

printArray(arr, size);

selectionSort(arr, size);

printf("Sorted array: \n");

printArray(arr, size);

return 0;

PRACTICAL NO 10

AIM- Write a ‘C’Program to sort an Array of numbers using Insertion Sort Method

#include <stdio.h>

void insertionSort(int arr[], int size) {

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

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

}
arr[j + 1] = key;

void printArray(int arr[], int size) {

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {64, 25, 12, 22, 11, 90};

int size = sizeof(arr) / sizeof(arr[0]);

printf("Original array: \n");

printArray(arr, size);

insertionSort(arr, size);

printf("Sorted array: \n");

printArray(arr, size);

return 0;

PRACTICAL 11

AIM- Write a ‘C’ Program to Sort an Array of Strings using Insertion Sort Method

#include <stdio.h>

#include <string.h>

void insertionSort(char *arr[], int size) {

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

char *key = arr[i];

int j = i - 1;

while (j >= 0 && strcmp(arr[j], key) > 0) {


arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

void printArray(char *arr[], int size) {

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

printf("%s ", arr[i]);

printf("\n");

int main() {

char *arr[] = {"banana", "apple", "cherry", "date", "fig", "grape", "kiwi"};

int size = sizeof(arr) / sizeof(arr[0]);

printf("Original array: \n");

printArray(arr, size);

insertionSort(arr, size);

printf("Sorted array: \n");

printArray(arr, size);

return 0;

You might also like