[go: up one dir, main page]

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

C

The document contains a C program that provides a menu for various array operations including traversing, inserting, deleting, bubble sorting, selection sorting, linear searching, and binary searching. Each operation is implemented in a switch-case structure, allowing the user to choose an action and execute it on an array of integers. The program prompts the user for input and displays the results of each operation accordingly.
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 views8 pages

C

The document contains a C program that provides a menu for various array operations including traversing, inserting, deleting, bubble sorting, selection sorting, linear searching, and binary searching. Each operation is implemented in a switch-case structure, allowing the user to choose an action and execute it on an array of integers. The program prompts the user for input and displays the results of each operation accordingly.
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/ 8

#include<stdio.

h>

#include<conio.h>

int main()

int ashish;

printf("Hey user, enter a number.\n"

"1 = Traversing\n"

"2 = Inserting\n"

"3 = Deleting\n"

"4 = Bubble Sorting\n"

"5 = Selection Sorting\n"

"6 = Linear Searching\n"

"7 = Binary Searching\n");

scanf("%d", &ashish);

switch (ashish) { case 1:

{ // Traversing int data[5]

= {3, 5, 7, 9, 11};

int i;

printf("\nElements of data are:");

for (i = 0; i < 5; i++) { printf("\n

%d", data[i]);

break;

}
case 2: { // Inserting int

data[10] = {3, 5, 6, 8, 10};

int i, k, j, n, t;

printf("\nInitially array elements are:");

for (i = 0; i < 5; i++) { printf("\n\t

data[%d] is %d", i, data[i]);

printf("\nTotal number of elements in array data is %d", i); printf("\nEnter the

position in array where you want to insert a new element (0 to %d): ", i);

scanf("%d", &k);

for (j = i - 1; j >= k; j--)

{ data[j + 1] =

data[j];

printf("\nEnter the new element to be inserted: ");

scanf("%d", &n);

data[k] = n;

t = i + 1;

printf("\nNew array after insertion of number '%d' at position '%d' is: ", n, k);

for (int x = 0; x < t; x++) { printf("\n\tdata[%d] = %d", x, data[x]);

break;

case 3: { // Deleting
int data[10] = {3, 5, 6, 8, 10};

int i = 5, k, j, item;

printf("\nInitially array elements are:");

for (i = 0; i < 5; i++) { printf("\n\

tdata[%d] = %d", i, data[i]);

printf("\nEnter the position of element you want to delete (0 to %d): ", i - 1);

scanf("%d", &k);

item = data[k];

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

data[j] = data[j + 1];

printf("\nArray after deletion of number '%d' at position '%d' is: ", item,

k); for (int x = 0; x < i - 1; x++) { printf("\n\tdata[%d] = %d", x,

data[x]);

printf("\nNow, total number of elements in the array is %d", i - 1);

break;

case 4: { // Bubble Sorting

int a[50], i, k, j, n, temp;

printf("Enter the size of array: ");

scanf("%d", &n);
printf("\nEnter %d unsorted numbers:\n", n);

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

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

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

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

if (a[k] > a[k + 1])

{ temp = a[k];

a[k] = a[k + 1]; a[k + 1]

= temp;

printf("\nSorted numbers are:\n");

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

{ printf("\n\t%d", a[i]);

break;

case 5: { // Selection Sorting

int a[50], i, j, k, n, temp;

printf("Enter the size of array: ");

scanf("%d", &n);
printf("\nEnter %d unsorted numbers:\n", n); for (i = 0; i < n; i++)

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

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

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

if (a[i] > a[j])

{ temp = a[i];

a[i] = a[j]; a[j] =

temp;

printf("\nSorted numbers are:\n");

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

{ printf("\n\t%d", a[i]);

break;

case 6: { // Linear Searching

int a[50], i, n, item, found = 0;

printf("Enter the size of array: ");

scanf("%d", &n);

printf("\nEnter %d numbers:\n", n);

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

{ scanf("%d", &a[i]); }
printf("\nEnter item to be searched: ");

scanf("%d", &item);

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

if (a[i] == item)

{ found = 1;

break;

if (found) {

printf("Search is successful. Item found at index %d", i);

} else { printf("Search is

unsuccessful.");

break;

case 7: { // Binary Searching int

a[50], i, n, item, lb, ub, mid, flag = 0;

printf("Enter the size of array: ");

scanf("%d", &n);

printf("\nEnter %d sorted numbers:\n", n);

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

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

}
printf("\nEnter item to be searched: ");

scanf("%d", &item);

lb = 0;

ub = n - 1;

while (lb <= ub) {

mid = (lb + ub) / 2;

if (a[mid] == item) {

flag = 1; break;

} else if (a[mid] < item) {

lb = mid + 1; } else {

ub = mid - 1;

if (flag == 1)

printf("\nItem %d found at location %d", item, mid + 1);

} else { printf("\

nItem not found.");

break;

default:

printf("Invalid choice.");

break;

return 0;
}

You might also like