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

C

Uploaded by

imamoddin15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

C

Uploaded by

imamoddin15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

q1: A menu driven program to do following operation

1)insertion
2)deletion
3)travertion

### Algorithm

1. **Initialize** an array with a fixed size (30) and a variable to track the
number of elements.
2. **Display the menu** options for user interaction.
3. **Input Array**:
- Prompt the user for the number of elements and store them.
4. **Insert Element**:
- Request the position and value to insert.
- Shift elements and insert the new item.
5. **Delete Element**:
- Ask for the position to delete.
- Shift elements to fill the gap.
6. **Traverse**:
- Request lower and upper bounds.
- Print elements in the specified range.
7. **Exit** the program when the user selects the exit option.

### C++ Code for Turbo C++

```cpp
#include <iostream.h>
#include <conio.h>

class ArrayOperations {
private:
int arr[30];
int n;

public:
ArrayOperations() : n(0) {}

void inputArray() {
cout << "Enter total number of elements: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Enter element for arr[" << i << "]: ";
cin >> arr[i];
}
}

void printArray() {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}

void insertion(int k, int item) {


if (k < 0 || k > n) {
cout << "Invalid position!" << endl;
return;
}
for (int j = n; j > k; j--) {
arr[j] = arr[j - 1];
}
arr[k] = item;
n++;
cout << "Array after insertion: ";
printArray();
}

void deletion(int k) {
if (k < 0 || k >= n) {
cout << "Invalid position!" << endl;
return;
}
for (int j = k; j < n - 1; j++)
arr[j] = arr[j + 1];
n--;
cout << "Array after deletion: ";
printArray();
}

void traversal(int lb, int ub) {


if (lb < 0 || ub >= n || lb > ub) {
cout << "Invalid bounds!" << endl;
return;
}
cout << "Array elements from " << lb << " to " << ub << ": ";
for (int i = lb; i <= ub; i++)
cout << arr[i] << " ";
cout << endl;
}
};

void main() {
clrscr();
ArrayOperations ao;
int choice, item, k, lb, ub;

do {
cout << "\nMenu:\n";
cout << "1. Input Array\n";
cout << "2. Insert Element\n";
cout << "3. Delete Element\n";
cout << "4. Traverse\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
ao.inputArray();
break;
case 2:
cout << "Enter position for insertion: ";
cin >> k;
cout << "Enter element for insertion: ";
cin >> item;
ao.insertion(k, item);
break;
case 3:
cout << "Enter position for deletion: ";
cin >> k;
ao.deletion(k);
break;
case 4:
cout << "Enter lower bound: ";
cin >> lb;
cout << "Enter upper bound: ";
cin >> ub;
ao.traversal(lb, ub);
break;
}
} while (choice != 5);

getch();
}

Q2 :/* A menu driven program of linear and binary search. */

### Algorithm

1. **Class Initialization**: Create a class to handle array operations.


2. **Input Array**: Prompt for the number of elements and input them.
3. **Print Array**: Display the contents of the array.
4. **Sort Array**: Sort the array in ascending order.
5. **Linear Search**: Find an element by checking each array element sequentially.
6. **Binary Search**: Find an element using a sorted array and divide-and-conquer
strategy.
7. **Menu Loop**: Continuously prompt the user for actions until they choose to
exit.

### C++ Code with Class Mechanism for Turbo C++

```cpp
#include <iostream.h>
#include <conio.h>

class SearchOperations {
private:
int arr[30];
int n;

public:
SearchOperations() : n(0) {}

void inputArray() {
cout << "Enter total number of elements in the array: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Enter element for arr[" << i << "]: ";
cin >> arr[i];
}
}

void printArray() {
cout << "\nGIVEN ARRAY IS AS BELOW:";
for (int i = 0; i < n; i++)
cout << "\n arr[" << i << "]: " << arr[i];
}

void sortArray() {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

void linearSearch() {
int item, pos = -1;
cout << "\nEnter item for search: ";
cin >> item;
for (int i = 0; i < n; i++) {
if (arr[i] == item) {
pos = i;
break; // Stop once the item is found
}
}
if (pos == -1)
cout << "\nGiven item is not found";
else
cout << "\nGiven item found at position " << pos;
}

void binarySearch() {
int item, beg, mid, end, i = 0;
cout << "\nEnter item for search: ";
cin >> item;
beg = 0;
end = n - 1;

while (beg <= end) {


mid = (beg + end) / 2;
i++;
cout << "\nSTEP " << i << ":";
cout << " beg=" << beg << " value=" << arr[beg];
cout << " end=" << end << " value=" << arr[end];
cout << " mid=" << mid << " value=" << arr[mid];

if (arr[mid] == item) {
cout << "\n\nGiven item found at position " << mid;
return;
} else if (item > arr[mid]) {
beg = mid + 1;
} else {
end = mid - 1;
}
}
cout << "\n\nGiven item is not found";
}
};

void main() {
clrscr();
SearchOperations so;
int choice;

do {
clrscr();
cout << "\nA menu driven program of linear and binary search";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout << "\nSEARCH MENU";
cout << "\n~~~~~~~~~~~";
cout << "\n1. INPUT ARRAY";
cout << "\n2. LINEAR SEARCH";
cout << "\n3. BINARY SEARCH";
cout << "\n4. EXIT";
cout << "\nEnter your choice: ";
cin >> choice;

switch (choice) {
case 1:
so.inputArray();
break;
case 2:
so.printArray();
so.linearSearch();
break;
case 3:
so.sortArray();
so.printArray();
so.binarySearch();
break;
}
getch();
} while (choice != 4);
getch();
}
```

Q3 /* A menu driven program of bubble,selection and insertion sorting method */

### Algorithm

1. **Class Initialization**: Create a class to manage sorting operations.


2. **Input Array**: Prompt for the number of elements and input them into the
array.
3. **Print Array**: Display the contents of the array.
4. **Bubble Sort**: Implement bubble sort with steps shown.
5. **Selection Sort**: Implement selection sort with steps shown.
6. **Insertion Sort**: Implement insertion sort with steps shown.
7. **Menu Loop**: Allow user to choose operations until they decide to exit.
### C++ Code with Class Mechanism for Turbo C++

```cpp
#include <iostream.h>
#include <conio.h>

class Sorting {
private:
int arr[30];
int n;

public:
Sorting() : n(0) {}

void inputArray() {
cout << "\nEnter total number of elements in the array: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Enter element for arr[" << i << "]: ";
cin >> arr[i];
}
}

void printArray() {
cout << "\nGIVEN ARRAY IS AS BELOW:";
for (int i = 0; i < n; i++)
cout << " " << arr[i];
cout << endl;
}

void bubbleSort() {
for (int k = 0; k < n - 1; k++) {
int ptr = 0;
while (ptr < n - k - 1) {
if (arr[ptr] > arr[ptr + 1]) {
int temp = arr[ptr];
arr[ptr] = arr[ptr + 1];
arr[ptr + 1] = temp;
}
ptr++;
}
cout << "\nStep " << k + 1 << ":";
printArray();
}
}

int min(int k) {
int loc = k;
int minimum = arr[k];
for (int i = k; i < n; i++) {
if (arr[i] < minimum) {
loc = i;
minimum = arr[i];
}
}
return loc;
}
void selectionSort() {
for (int k = 0; k <= n - 2; k++) {
int loc = min(k);
int temp = arr[k];
arr[k] = arr[loc];
arr[loc] = temp;
cout << "\nStep " << k + 1 << ":";
printArray();
}
}

void insertionSort() {
for (int k = 1; k < n; k++) {
int temp = arr[k];
int ptr = k - 1;
while (ptr >= 0 && temp < arr[ptr]) {
arr[ptr + 1] = arr[ptr];
ptr--;
}
arr[ptr + 1] = temp;
cout << "\nStep " << k << ":";
printArray();
}
}
};

void main() {
clrscr();
Sorting sorting;
int choice;

do {
clrscr();
cout << "\nA menu driven program of sorting methods";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout << "\nSORT MENU";
cout << "\n~~~~~~~~~";
cout << "\n1. INPUT ARRAY";
cout << "\n2. BUBBLE SORT";
cout << "\n3. INSERTION SORT";
cout << "\n4. SELECTION SORT";
cout << "\n5. EXIT";
cout << "\nEnter your choice: ";
cin >> choice;

switch (choice) {
case 1:
sorting.inputArray();
break;
case 2:
sorting.printArray();
sorting.bubbleSort();
break;
case 3:
sorting.printArray();
sorting.insertionSort();
break;
case 4:
sorting.printArray();
sorting.selectionSort();
break;
}
getch();
} while (choice != 5);
getch();
}
```

Q5 /* A program to convert infix expression into postfix expression */

PENDING

Q6

You might also like