KEMBAR78
Ds Using C Lab Programs 2025 | PDF | Integer (Computer Science) | Computer Programming
0% found this document useful (0 votes)
10 views11 pages

Ds Using C Lab Programs 2025

The document contains C programs for various array operations including insertion, deletion, linear search, binary search, and bubble sort. Each section provides user prompts for inputting array elements and performing the respective operations, along with outputting the results. The programs utilize standard input/output functions and include error handling for invalid positions during deletion.

Uploaded by

ganjayipujitha
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)
10 views11 pages

Ds Using C Lab Programs 2025

The document contains C programs for various array operations including insertion, deletion, linear search, binary search, and bubble sort. Each section provides user prompts for inputting array elements and performing the respective operations, along with outputting the results. The programs utilize standard input/output functions and include error handling for invalid positions during deletion.

Uploaded by

ganjayipujitha
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/ 11

//Array Insert

#include <stdio.h>

#include <conio.h>

int main() {

int arr[100], n, i, pos, value;

clrscr();

// Input number of elements

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

scanf("%d", &n);

// Input elements

printf("Enter %d elements:\n", n);

for(i = 0; i < n; i++) {

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

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

// Input position and value to insert

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

scanf("%d", &pos);
printf("Enter value to insert: ");

scanf("%d", &value);

// Shift elements to the right

for(i = n; i > pos; i--) {

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

// Insert value

arr[pos] = value;

n++; // Increase size

// Print updated array

printf("Array after insertion:\n");

for(i = 0; i < n; i++) {

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

getch();

return 0;

}
//Array Deletion

#include <stdio.h>

#include<conio.h>

int main() {

int arr[100], n, i, pos;

clrscr();

// Input number of elements

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

scanf("%d", &n);

// Input elements

printf("Enter %d elements:\n", n);

for(i = 0; i < n; i++) {

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

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

// Input position to delete

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


scanf("%d", &pos);

// Check valid position

if(pos < 0 || pos >= n) {

printf("Invalid position!\n");

} else {

// Shift elements to the left

for(i = pos; i < n - 1; i++) {

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

n--; // Decrease size

// Print updated array

printf("Array after deletion:\n");

for(i = 0; i < n; i++) {

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

getch();

return 0;

}
Linear Search:

#include <stdio.h>

#include <conio.h>

int main() {

int arr[100], n, i, key, found = 0;

clrscr();

// Input array size

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

scanf("%d", &n);

// Input array elements

printf("Enter %d elements:\n", n);

for(i = 0; i < n; i++) {

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

}
// Input element to search

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

scanf("%d", &key);

// Linear search logic

for(i = 0; i < n; i++) {

if(arr[i] == key) {

found = 1;

break;

// Output result

if(found) {

printf("Element %d found at position %d (index %d).\n", key, i +


1, i);

} else {

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

getch();

return 0;

}
//Binary Search

#include <stdio.h>

#include <conio.h>

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

int low = 0, high = size - 1;

while (low <= high)

int mid = low + (high - low) / 2;

if (arr[mid] == key)

return mid; // Key found

} else if (arr[mid] < key) {

low = mid + 1; // Search in the right half

} else {

high = mid - 1; // Search in the left half

return -1; // Key not found


}

int main() {

int n, key, result,i,arr[100];

clrscr();

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

scanf("%d", &n);

printf("Enter %d sorted elements:\n", n);

for (i = 0; i < n; i++) {

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

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

scanf("%d", &key);

result = binarySearch(arr, n, key);

if (result != -1) {

printf("Element found at index %d (0-based indexing).\n",


result);
} else {

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

getch();

return 0;

Bubble Sort:

#include <stdio.h>

#include <conio.h>

void bubbleSort(int arr[], int n);

int main() {

int arr[100], n, i;

clrscr();

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

scanf("%d", &n);

printf("Enter %d integers:\n", n);

for(i = 0; i < n; i++) {

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

bubbleSort(arr, n);

printf("Sorted array in ascending order:\n");

for(i = 0; i < n; i++) {

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

return 0;

// Function to perform bubble sort

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

int i, j, temp;

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

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

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

// Swap elements

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

arr[j+1] = temp;

getch();

You might also like