11.
LAB SOLUTIONS
1.Write a program to compute minimum/maximum of a given array.
#include <stdio.h>
// Function to find minimum and maximum in an array
void findMinMax(int arr[], int size, int *min, int *max) {
*min = arr[0];
*max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] < *min) {
*min = arr[i];
}
if (arr[i] > *max) {
*max = arr[i];
}
}
}
// Main function
int main() {
int n, min, max;
// Input array size
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
// Input array elements
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Find minimum and maximum
findMinMax(arr, n, &min, &max);
// Display results
printf("Minimum element: %d\n", min);
printf("Maximum element: %d\n", max);
return 0;
}
Output-
Enter the number of elements: 4
Enter 4 elements: 12
34
5
6
Minimum element: 5
Maximum element: 34
2. Write a menu-based program to perform array operations: deletion of an element from the specified
position, inserting an element at the specified position, printing the array elements.
#include <stdio.h>
// Function to insert an element at a specified position
void insertElement(int arr[], int *size, int pos, int element) {
if (pos < 0 || pos > *size) {
printf("Invalid position!\n");
return;
}
for (int i = *size; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = element;
(*size)++;
}
// Function to delete an element from a specified position
void deleteElement(int arr[], int *size, int pos) {
if (pos < 0 || pos >= *size) {
printf("Invalid position!\n");
return;
}
for (int i = pos; i < *size - 1; i++) {
arr[i] = arr[i + 1];
}
(*size)--;
}
// Function to print the array
void printArray(int arr[], int size) {
printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Main function with menu-based operations
int main() {
int arr[100], size, choice, pos, element;
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
while (1) {
printf("\nMenu:\n");
printf("1. Insert an element\n");
printf("2. Delete an element\n");
printf("3. Print array\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter position to insert (0-based index): ");
scanf("%d", &pos);
printf("Enter element to insert: ");
scanf("%d", &element);
insertElement(arr, &size, pos, element);
break;
case 2:
printf("Enter position to delete (0-based index): ");
scanf("%d", &pos);
deleteElement(arr, &size, pos);
break;
case 3:
printArray(arr, size);
break;
case 4:
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
}
Enter the number of elements: 5
Enter 5 elements: 12
34
56
67
78
Menu:
1. Insert an element
2. Delete an element
3. Print array
4. Exit
Enter your choice: 3
Array elements: 12 34 56 67 78
Menu:
1. Insert an element
2. Delete an element
3. Print array
4. Exit
3.Write a program to search an element in a 2-dimensional array.
#include <stdio.h>
// Function to search an element in a 2D array
void searchElement(int rows, int cols, int arr[rows][cols], int key) {
int found = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] == key) {
printf("Element %d found at position (%d, %d)\n", key, i, j);
found = 1;
}
}
}
if (!found) {
printf("Element %d not found in the array.\n", key);
}
}
// Main function
int main() {
int rows, cols, key;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
int arr[rows][cols];
printf("Enter elements of the 2D array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}
printf("Enter the element to search: ");
scanf("%d", &key);
searchElement(rows, cols, arr, key);
return 0;
}
Output-
Enter number of rows: 2
Enter number of columns: 2
Enter elements of the 2D array:
1
2
5
6
Enter the element to search: 2
Element 2 found at position (0, 1)
4. Write a program to perform following operations in matrix:
a. Addition
b. Subtraction
c. Multiplication
d. Transpose
#include <stdio.h>
// Function to add two matrices
void addMatrices(int r, int c, int A[r][c], int B[r][c], int result[r][c]) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
}
// Function to subtract two matrices
void subtractMatrices(int r, int c, int A[r][c], int B[r][c], int result[r][c]) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
result[i][j] = A[i][j] - B[i][j];
}
}
}
// Function to multiply two matrices
void multiplyMatrices(int r1, int c1, int A[r1][c1], int r2, int c2, int B[r2][c2], int result[r1][c2]) {
if (c1 != r2) {
printf("Matrix multiplication not possible!\n");
return;
}
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
for (int k = 0; k < c1; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
}
// Function to transpose a matrix
void transposeMatrix(int r, int c, int A[r][c], int result[c][r]) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
result[j][i] = A[i][j];
}
}
}
// Function to print a matrix
void printMatrix(int r, int c, int A[r][c]) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d ", A[i][j]);
}
printf("\n");
}
}
int main() {
int r1, c1, r2, c2, choice;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
int A[r1][c1];
printf("Enter elements of first matrix:\n");
for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j++)
scanf("%d", &A[i][j]);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
int B[r2][c2];
printf("Enter elements of second matrix:\n");
for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
scanf("%d", &B[i][j]);
while (1) {
printf("\nMenu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Transpose of First Matrix\n");
printf("5. Transpose of Second Matrix\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
int result[r1][c1];
switch (choice) {
case 1:
if (r1 == r2 && c1 == c2) {
addMatrices(r1, c1, A, B, result);
printf("Resultant matrix after addition:\n");
printMatrix(r1, c1, result);
} else {
printf("Matrix addition not possible!\n");
}
break;
case 2:
if (r1 == r2 && c1 == c2) {
subtractMatrices(r1, c1, A, B, result);
printf("Resultant matrix after subtraction:\n");
printMatrix(r1, c1, result);
} else {
printf("Matrix subtraction not possible!\n");
}
break;
case 3:
if (c1 == r2) {
int res[r1][c2];
multiplyMatrices(r1, c1, A, r2, c2, B, res);
printf("Resultant matrix after multiplication:\n");
printMatrix(r1, c2, res);
} else {
printf("Matrix multiplication not possible!\n");
}
break;
case 4:
{
int transA[c1][r1];
transposeMatrix(r1, c1, A, transA);
printf("Transpose of first matrix:\n");
printMatrix(c1, r1, transA);
}
break;
case 5:
{
int transB[c2][r2];
transposeMatrix(r2, c2, B, transB);
printf("Transpose of second matrix:\n");
printMatrix(c2, r2, transB);
}
break;
case 6:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output-
Enter rows and columns of first matrix: 2 2
Enter elements of first matrix:
12
34
Enter rows and columns of second matrix: 2 2
Enter elements of second matrix:
56
78
Menu:
1. Addition
2. Subtraction
3. Multiplication
4. Transpose of First Matrix
5. Transpose of Second Matrix
6. Exit
Enter your choice: 1
Resultant matrix after addition:
68
10 12
5. Write a menu-based program to perform following operations on single linked list:
a. To insert a node at the beginning of the list.
b. To insert a node at the end of the list.
c. To insert a node after a given node in the list.
d. To delete the first node from the list.
e. To delete the last node from the list.
f. To delete a node after a given node from the list.
g. To delete a node at a given position from the list.
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Function to insert a node at the beginning
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
// Function to insert a node at the end
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
// Function to insert after a given node
void insertAfterNode(struct Node* prevNode, int data) {
if (prevNode == NULL) {
printf("Given previous node cannot be NULL\n");
return;
}
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = prevNode->next;
prevNode->next = newNode;
}
// Function to delete the first node
void deleteFirstNode(struct Node** head) {
if (*head == NULL) return;
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
}
// Function to delete the last node
void deleteLastNode(struct Node** head) {
if (*head == NULL) return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node* temp = *head;
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
// Function to delete a node after a given node
void deleteAfterNode(struct Node* prevNode) {
if (prevNode == NULL || prevNode->next == NULL) return;
struct Node* temp = prevNode->next;
prevNode->next = temp->next;
free(temp);
}
// Function to delete a node at a given position
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL) return;
struct Node* temp = *head;
if (position == 0) {
*head = temp->next;
free(temp);
return;
}
struct Node* prev = NULL;
for (int i = 0; temp != NULL && i < position; i++) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
// Function to display the linked list
void displayList(struct Node* head) {
struct Node* temp = head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main function with menu
int main() {
struct Node* head = NULL;
int choice, data, pos;
while (1) {
printf("\nMenu:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert after a given node\n");
printf("4. Delete First Node\n");
printf("5. Delete Last Node\n");
printf("6. Delete after a given node\n");
printf("7. Delete at a given position\n");
printf("8. Display List\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter data: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter data: ");
scanf("%d", &data);
printf("Enter position (0-based index) after which to insert: ");
scanf("%d", &pos);
struct Node* temp = head;
for (int i = 0; temp != NULL && i < pos; i++)
temp = temp->next;
if (temp)
insertAfterNode(temp, data);
else
printf("Invalid position!\n");
break;
case 4:
deleteFirstNode(&head);
break;
case 5:
deleteLastNode(&head);
break;
case 6:
printf("Enter position (0-based index) after which to delete: ");
scanf("%d", &pos);
temp = head;
for (int i = 0; temp != NULL && i < pos; i++)
temp = temp->next;
if (temp)
deleteAfterNode(temp);
else
printf("Invalid position!\n");
break;
case 7:
printf("Enter position to delete (0-based index): ");
scanf("%d", &pos);
deleteAtPosition(&head, pos);
break;
case 8:
displayList(head);
break;
case 9:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output-
Menu:
1. Insert at Beginning
2. Insert at End
3. Insert after a given node
4. Delete First Node
5. Delete Last Node
6. Delete after a given node
7. Delete at a given position
8. Display List
9. Exit
Enter your choice:
6. Write a menu-based program to implement stack operations: PUSH, POP using array implementation
of stack.
#include <stdio.h>
#define MAX 100
// Stack structure
struct Stack {
int arr[MAX];
int top;
};
// Function to initialize stack
void initialize(struct Stack *s) {
s->top = -1;
}
// Function to check if stack is full
int isFull(struct Stack *s) {
return s->top == MAX - 1;
}
// Function to check if stack is empty
int isEmpty(struct Stack *s) {
return s->top == -1;
}
// Function to push an element onto the stack
void push(struct Stack *s, int data) {
if (isFull(s)) {
printf("Stack Overflow!\n");
return;
}
s->arr[++(s->top)] = data;
printf("%d pushed onto the stack.\n", data);
}
// Function to pop an element from the stack
void pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack Underflow!\n");
return;
}
printf("%d popped from the stack.\n", s->arr[s->top--]);
}
// Function to display the stack
void display(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
for (int i = s->top; i >= 0; i--) {
printf("%d ", s->arr[i]);
}
printf("\n");
}
int main() {
struct Stack s;
initialize(&s);
int choice, data;
while (1) {
printf("\nMenu:\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. DISPLAY\n");
printf("4. EXIT\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to push: ");
scanf("%d", &data);
push(&s, data);
break;
case 2:
pop(&s);
break;
case 3:
display(&s);
break;
case 4:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output-
Menu:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice:
7. Write a menu-based program using functions to implement stack operations: PUSH, POP using linked
implementation of stack.
#include <stdio.h>
#include <stdlib.h>
// Structure for a stack node
struct Node {
int data;
struct Node* next;
};
// Function to check if stack is empty
int isEmpty(struct Node* top) {
return top == NULL;
}
// Function to push an element onto the stack
void push(struct Node** top, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Heap Overflow!\n");
return;
}
newNode->data = data;
newNode->next = *top;
*top = newNode;
printf("%d pushed onto the stack.\n", data);
}
// Function to pop an element from the stack
void pop(struct Node** top) {
if (isEmpty(*top)) {
printf("Stack Underflow!\n");
return;
}
struct Node* temp = *top;
printf("%d popped from the stack.\n", temp->data);
*top = (*top)->next;
free(temp);
}
// Function to display the stack
void display(struct Node* top) {
if (isEmpty(top)) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
struct Node* temp = top;
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* top = NULL;
int choice, data;
while (1) {
printf("\nMenu:\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. DISPLAY\n");
printf("4. EXIT\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to push: ");
scanf("%d", &data);
push(&top, data);
break;
case 2:
pop(&top);
break;
case 3:
display(top);
break;
case 4:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output-
Menu:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice:
8. Write a program to solve Towers of Hanoi Problem.
#include <stdio.h>
// Function to solve Tower of Hanoi
void towersOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
towersOfHanoi(n - 1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towersOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
printf("The sequence of moves are:\n");
towersOfHanoi(n, 'A', 'C', 'B');
return 0;
}
Output-
Enter the number of disks: 3
The sequence of moves are:
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
9. Write a menu-based program to implement linear queue operations: INSERTION, DELETION using
array implementation of queue.
#include <stdio.h>
#define MAX 100
// Queue structure
typedef struct {
int arr[MAX];
int front, rear;
} Queue;
// Function to initialize queue
void initialize(Queue *q) {
q->front = q->rear = -1;
}
// Function to check if queue is empty
int isEmpty(Queue *q) {
return q->front == -1;
}
// Function to check if queue is full
int isFull(Queue *q) {
return q->rear == MAX - 1;
}
// Function to insert an element into the queue
void enqueue(Queue *q, int data) {
if (isFull(q)) {
printf("Queue Overflow!\n");
return;
}
if (isEmpty(q)) {
q->front = q->rear = 0;
} else {
q->rear++;
}
q->arr[q->rear] = data;
printf("%d inserted into the queue.\n", data);
}
// Function to delete an element from the queue
void dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue Underflow!\n");
return;
}
printf("%d deleted from the queue.\n", q->arr[q->front]);
if (q->front == q->rear) {
initialize(q);
} else {
q->front++;
}
}
// Function to display the queue
void display(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->arr[i]);
}
printf("\n");
}
int main() {
Queue q;
initialize(&q);
int choice, data;
while (1) {
printf("\nMenu:\n");
printf("1. INSERT\n");
printf("2. DELETE\n");
printf("3. DISPLAY\n");
printf("4. EXIT\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
enqueue(&q, data);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output-
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 1
Enter data to insert: 10
10 inserted into the queue.
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 1
Enter data to insert: 20
20 inserted into the queue.
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 1
Enter data to insert: 30
30 inserted into the queue.
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 3
Queue elements: 10 20 30
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 2
10 deleted from the queue.
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 3
Queue elements: 20 30
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 2
20 deleted from the queue.
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 2
30 deleted from the queue.
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 3
Queue is empty.
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 4
10. Write a menu-based program to implement linear queue operations: INSERTION, DELETION using
linked list implementation of queue.
#include <stdio.h>
#include <stdlib.h>
// Structure for a queue node
struct Node {
int data;
struct Node* next;
};
// Queue structure
struct Queue {
struct Node *front, *rear;
};
// Function to initialize the queue
void initialize(struct Queue* q) {
q->front = q->rear = NULL;
}
// Function to check if queue is empty
int isEmpty(struct Queue* q) {
return q->front == NULL;
}
// Function to insert an element into the queue
void enqueue(struct Queue* q, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed!\n");
return;
}
newNode->data = data;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
printf("%d inserted into the queue.\n", data);
}
// Function to delete an element from the queue
void dequeue(struct Queue* q) {
if (isEmpty(q)) {
printf("Queue Underflow!\n");
return;
}
struct Node* temp = q->front;
printf("%d deleted from the queue.\n", temp->data);
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
}
// Function to display the queue
void display(struct Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
}
struct Node* temp = q->front;
printf("Queue elements: ");
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Queue q;
initialize(&q);
int choice, data;
while (1) {
printf("\nMenu:\n");
printf("1. INSERT\n");
printf("2. DELETE\n");
printf("3. DISPLAY\n");
printf("4. EXIT\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
enqueue(&q, data);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output-
Menu:
1. INSERT
2. DELETE
3. DISPLAY
4. EXIT
Enter your choice: 1
Enter data to insert: 10
10 inserted into the queue.
Menu:
1. INSERT
2. DELETE