Barkha Ds File-5
Barkha Ds File-5
8 WAP to search in an
array.
11 WAP to implement
binary search.
13 WAP to insert and
delete in a queue.
Page | 2
14 WAP of lower
triangular matrices.
15 WAP of upper
triangular matrices.
16 WAP of bubble sort
using c++.
17 WAP of selection sort
using C++.
Page | 2
Q.1 WAP to read and write array.
Ans #include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size <= 0) {
printf("Invalid size! The size of the array must be greater than 0.\n");
return 1
Page | 2
}
int arr[size];
readArray(arr, size);
writeArray(arr, size);
return 0;
}
Output:
Enter the size of the array: 5
Enter 5 elements:
10
20
30
40
50
The elements of the array are:
10 20 30 40 50
Page | 2
Q.2 WAP to find the largest and smallest element in array.
Page | 2
*smallest = arr[0];
int main() {
int size;
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
scanf("%d", &arr[i]);
}
Page | 2
int largest, smallest;
return 0;
Output:
Enter the size of the array: 5
Enter 5 elements:
10
20
5
40
15
The largest element is: 40
The smallest element is: 5
Page | 2
Q.3 WAP to insert and delete in an array.
Ans #include <stdio.h>
arr[position] = element;
(*size)++;
printf("Element inserted successfully!\n");
}
(*size)--;
printf("Element deleted successfully!\n");
}
int main() {
int arr[100], size, choice, element, position;
Page | 2
scanf("%d", &arr[i]);
}
while (1)
Page | 2
printf("\nChoose an operation:\n");
printf("1. Insert an element\n");
printf("2. Delete an element\n");
printf("3. Display the array\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the position to insert (0-based index): ");
scanf("%d", &position);
printf("Enter the element to insert: ");
scanf("%d", &element);
insertElement(arr, &size, position, element);
break;
case 2:
printf("Enter the position to delete (0-based index): ");
scanf("%d", &position);
deleteElement(arr, &size
break;
case 3:
displayArray(arr, size);
break;
case 4:
printf("Exiting program. Goodbye!\n");
return 0;
default:
Page | 2
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Output:
Menu:
1. Insert an element
2. Delete an element
3. Display array
4. Exit
Enter your choice: 1
Enter the element to insert: 10
Enter the position to insert: 1
Enter your choice: 3
Array elements: 10
Enter your choice: 1
Enter the element to insert: 20
Enter the position to insert: 2
Enter your choice: 3
Array elements: 10 20
Enter your choice: 2
Enter the position to delete: 1
Enter your choice: 4
Page | 2
Q.4 WAP of mini calculator(i.e. addition, subtraction, multiplication, division) of
two numbers.
Ans #include <stdio.h>
int main() {
int num1, num2, choice;
float result;
do {
printf("\nMini Calculator Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
Page | 2
printf("Number 1: ");
scanf("%d", &num1);
printf("Number 2: ");
scanf("%d", &num2);
}
switch (choice)
case 1:
result = num1 + num2;
printf("Result: %d + %d = %.2f\n", num1, num2, result);
break;
case 2:
result = num1 - num2;
printf("Result: %d - %d = %.2f\n", num1, num2, result);
break;
case 3:
result = num1 * num2;
printf("Result: %d * %d = %.2f\n", num1, num2, result);
break;
case 4:
if (num2 == 0) {
printf("Error! Division by zero is not allowed.\n");
} else {
result = (float)num1 / num2;
printf("Result: %d / %d = %.2f\n", num1, num2, result);
}
break;
case 5:
printf("Exiting the program. Goodbye!\n");
break;
default:
Page | 2
printf("Invalid choice! Please choose again.\n");
}
} while (choice != 5);
return 0;
}
Output:
Mini Calculator Menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 1
Enter two numbers:
Number 1: 5
Number 2: 3
Result: 5 + 3 = 8.00
Enter your choice: 2
Enter two numbers:
Number 1: 7
Number 2: 2
Result: 7 - 2 = 5.00
Enter your choice: 3
Enter two numbers:
Number 1: 4
Number 2: 5
Result: 4 * 5 = 20.00
Enter your choice: 4
Enter two numbers:
Number 1: 10
Page | 2
Number 2: 2
Result: 10 / 2 = 5.00
Enter your choice: 5
Exiting the program. Goodbye!
void subtractMatrices(int rows, int cols, int mat1[10][10], int mat2[10][10], int
result[10][10]) {
for (int i = 0; i < rows; i++) {
Page | 2
for (int j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
int main() {
int rows, cols;
int mat1[10][10], mat2[10][10], result[10][10];
Page | 2
scanf("%d", &mat1[i][j]);
}
}
return 0;
}
Output:
The user enters a 2x2 matrix.
mat1:
1 2
3 4
mat2:
5 6
7 8
Page | 2
After performing the subtraction mat1 - mat2, the resultant matrix will be:
(1-5) (2-6)
(3-7) (4-8)
Resultant Matrix (Subtraction): -4 -4 -4 -4
Page | 2
Q.6 WAP to calculate and display display simple interest.
Ans #include <stdio.h>
int main() {
float principal, rate, time, simpleInterest;
return 0
Page | 2
}
Output:
Enter the principal amount: 1000
Enter the rate of interest (in percentage): 5
Enter the time (in years): 2
Simple Interest: 100.00
Page | 2
Q.7 WAP of single linked list.
Ans #include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
if (*head == NULL) {
*head = newNode;
return;
}
Page | 2
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
if (temp == NULL) {
Page | 2
printf("Node with value %d not found!\n", value);
return;
}
prev->next = temp->next;
free(temp);
}
int main() {
struct Node *head = NULL;
int choice, value;
do {
printf("\nMenu:\n");
Page | 2
printf("1. Insert at the beginning\n");
printf("2. Insert at the end\n");
switch (choice) {
case
:
printf("Enter the value to insert at the beginning: ");
scanf("%d", &value);
insertAtBeginning(&head, value);
break;
case 2:
printf("Enter the value to insert at the end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
printf("Enter the value to delete: ");
scanf("%d", &value);
deleteNode(&head, value);
break;
case 4:
Page | 2
displayList(head);
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
Output:
Menu:
1. Insert at the beginning
2. Insert at the end
3. Delete a node
4. Display the list
5. Exit
Enter your choice: 1
Enter the value to insert at the beginning: 10
Enter your choice: 1
Enter the value to insert at the beginning: 20
Enter your choice: 2
Enter the value to insert at the end: 30
Enter your choice: 2
Enter the value to insert at the end: 40
Enter your choice: 3
Enter the value to delete: 20Menu:
Page | 2
Enter your choice: 4
Linked List: 10 -> 30 -> 40 -> NULL
Enter your choice: 5
Exiting program.
int main() {
int arr[100], size, key, result;
if (result == -1)
Page | 2
{
printf("Element %d not found in the array.\n", key);
} else {
printf("Element %d found at index %d.\n", key, result);
}
return 0;
}
Output:
Enter the size of the array: 4
Enter 4 elements of the array:
10 20 30 40
Enter the element to search: 25
Element 25 not found in the array.
Page | 2
Q.9 WAP to push and pop in a stack.
Ans #include <stdio.h>
#include <stdlib.h>
#define MAX 5
struct Stack {
int arr[MAX];
int top;
};
Page | 2
int main() {
struct Stack stack;
int choice, value;
initStack(&stack);
do {
printf("\nMenu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display Stack\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&stack, value);
break;
case 2:
pop(&stack);
break;
case 3:
display(&stack);
bre
k;
Page | 2
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
Output:
Menu:
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice: 1
Enter value to push: 10
Enter your choice: 1
Enter value to push: 20
Enter your choice: 1
Enter value to push: 30
Enter your choice: 3
Stack elements: 10 20 30
Enter your choice: 2
Page | 2
Popped 30 from the stack.
Enter your choice: 3
Stack elements: 10 20
Pushed 10 to the stack.
Pushed 20 to the stack.
Pushed 30 to the stack.
Stack elements: 10 20 30
Popped 30 from the stack.
Stack elements: 10 20
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
Page | 2
void displayArray(int arr[], int size) {
printf("Sorted Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[100], size;
scanf("%d", &arr[i]);
}
bubbleSort(arr, size);
displayArray(arr, size);
Page | 2
return 0;
}
Output:
Enter the size of the array: 5
Enter 5 elements of the array:
34 12 9 56 22
Sorted Array: 9 12 22 34 56
Page | 2
Q.11 WAP to implement binary search.
Ans #include <stdio.h>
if (arr[mid] == key) {
return mid;
}
else {
high = mid - 1;
}
}
Page | 2
return -1;
}
int main() {
int arr[100], size, key, result;
if (result == -1) {
printf("Element %d not found in the array.\n", key);
} else {
printf("Element %d found at index %d.\n", key, result);
}
return 0;
Page | 2
}
Output:
Enter the size of the array: 6
Enter 6 elements of the array in sorted order:
5 10 15 20 25 30
Enter the element to search: 20
Element 20 found at index 3.
struct Node {
int data;
struct Node* left;
Page | 2
struct Node* right;
};
int main() {
Page | 2
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
return 0
}
Output:
In-order traversal: 4 2 5 1 6 3 7
Pre-order traversal: 1 2 4 5 3 6 7
Post-order traversal: 4 5 2 6 7 3 1
Page | 2
Page | 2
Q.13 WAP to insert and delete in a queue.
Ans #include <stdio.h>
#include <stdlib.h>
#define MAX 5
struct Queue {
int arr[MAX];
int front;
int rear;
} else {
if (q->front == -1) {
q->front = 0;
}
q->rear++;
q->arr[q->rear] = value;
printf("%d inserted into the queue.\n", value);
}
}
Page | 2
}
int main() {
struct Queue q;
initializeQueue(&
Page | 2
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
enqueue(&q, 40);
enqueue(&q, 50);
display(&q);
enqueue(&q, 60);
display(&q);
return 0;
}
Output:
10 inserted into the queue.
20 inserted into the queue.
30 inserted into the queue.
40 inserted into the queue.
50 inserted into the queue.
Queue elements: 10 20 30 40 50
Queue is full. Cannot insert 60.
Page | 2
10 dequeued from the queue.
20 dequeued from the queue.
Queue elements: 30 40 50
#define MAX 10
Page | 2
matrix[i][j] = 0;
}
}
}
int main() {
int n;
int matrix[MAX][MAX];
Page | 2
lowerTriangular(matrix, n);
return 0;
}
Output:
Enter the size of the matrix (n x n): 3
Enter the elements of the matrix:
123
456
789
Lower Triangular Matrix:
100
450
789
Page | 2
Q.15 WAP of upper triangular matrices.
Ans #include <stdio.h>
matrix[i][j] = 0;
}
}
}
int main() {
int n;
int matrix[MAX][MAX];
upperTriangular(matrix, n);
return 0;
}
Output:
Enter the size of the matrix (n x n): 3
Enter the elements of the matrix:
Page | 2
123
456
789
Upper Triangular Matrix:
123
056
009
Page | 2
Q.16 WAP of bubble sort using c++.
Ans #include <iostream>
using namespace std
Page | 2
void bubbleSort(int arr[], int n) {
int main() {
int n;
Page | 2
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
bubbleSort(arr, n);
return 0;
}
Output:
Enter the number of elements: 5
Enter the elements of the array: 64 34 25 12 22
Sorted array: 12 22 25 34 64
Page | 2
Q.17 WAP of selection sort using C++.
Ans #include <iostream>
using namespace std;
if (minIndex != i) {
int temp = arr[i];
Page | 2
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
int main() {
int n;
int arr[n];
selectionSort(arr, n);
Page | 2
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
Page | 2
Output:
Enter the number of elements: 5
Enter the elements of the array: 64 34 25 12 22
Sorted array: 12 22 25 34 64
Page | 2