0% found this document useful (0 votes)
16 views62 pages

Barkha Ds File-5

The document contains a collection of programming assignments (WAPs) in C, covering various topics such as array manipulation, matrix operations, a mini calculator, simple interest calculation, and linked lists. Each section includes code examples and expected outputs for tasks like reading/writing arrays, finding largest/smallest elements, inserting/deleting in arrays, and more. The assignments are structured to help users practice fundamental programming concepts.

Uploaded by

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

Barkha Ds File-5

The document contains a collection of programming assignments (WAPs) in C, covering various topics such as array manipulation, matrix operations, a mini calculator, simple interest calculation, and linked lists. Each section includes code examples and expected outputs for tasks like reading/writing arrays, finding largest/smallest elements, inserting/deleting in arrays, and more. The assignments are structured to help users practice fundamental programming concepts.

Uploaded by

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

INDEX

NO TITLE PAGE NO SIGN


1 WAP to read and write
array.
2 WAP to find the largest
and smallest element
in an array.
3 WAP to insert and
delete in an array.
4 WAP of mini
calculator(i.e. addition,
subtraction,
multiplication,
division) of t8wo
numbers.
5 WAP to subtraction of
two matrices.
6 WAP to calculate and
display display simple
interest.
7 WAP of single linked
list.

8 WAP to search in an
array.

9 WAP to push and pop


in a stack.

10 WAP to sort array


element.

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>

void readArray(int arr[], int size) {


printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
}

void writeArray(int arr[], int size) {


printf("The elements of the array are:\n");
for (int i = 0; i < size; i++) {
printf("Element %d: %d\n", i + 1, arr[i]);
}
}

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.

Ans #include <stdio.h>

voidfindLargestAndSmallest(int arr[], int size, int *largest, int *smallest) {


*largest = arr[0];

Page | 2
*smallest = arr[0];

for (int i = 1; i < size; i++) {


if (arr[i] > *largest) {
*largest = arr[i];
}
if (arr[i] < *smallest) {
*smallest = arr[i];
}
}
}

int main() {
int size;

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

scanf("%d", &size);

int arr[size];
printf("Enter %d elements of the array:\n", size);

for (int i = 0; i < size; i++) {

printf("Element %d: ", i + 1);

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

Page | 2
int largest, smallest;

findLargestAndSmallest(arr, size, &largest, &smallest);

printf("The largest element is: %d\n", largest);


printf("The smallest element is: %d\n", 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>

void insertElement(int arr[], int *size, int position, int element) {


if (position < 0 || position > *size) {
printf("Invalid position! Please enter a valid position.\n");
return;
}

for (int i = *size; i > position; i--) {


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

arr[position] = element;
(*size)++;
printf("Element inserted successfully!\n");
}

void deleteElement(int arr[], int *size, int position) {


if (position < 0 || position >= *size) {
printf("Invalid position! Please enter a valid position.\n");
return;
}
Page | 2
for (int i = position; i < *size - 1; i++) {

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


}

(*size)--;
printf("Element deleted successfully!\n");

}

void displayArray(int arr[], int size) {


printf("Current Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[100], size, choice, element, position;

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


scanf("%d", &size);

printf("Enter %d elements of the array:\n", size);

for (int i = 0; i < size; i++) {


printf("Element %d: ", i + 1);

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);

if (choice >= 1 && choice <= 4) {


printf("Enter two numbers:\n");

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!

Q.5 WAP to subtraction of two matrices.


Ans #include <stdio.h>

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];
}
}
}

void displayMatrix(int rows, int cols, int matrix[10][10]) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int rows, cols;
int mat1[10][10], mat2[10][10], result[10][10];

printf("Enter the number of rows and columns of the matrices: ");


scanf("%d %d", &rows, &cols);

printf("Enter elements of the first matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)

printf("Element [%d][%d]: ", i + 1, j + 1);

Page | 2
scanf("%d", &mat1[i][j]);
}
}

printf("Enter elements of the second matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &mat2[i][j]);
}
}

subtractMatrices(rows, cols, mat1, mat2, result);

printf("\nResultant Matrix (Subtraction):\n");


displayMatrix(rows, cols, result);

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;

printf("Enter the principal amount: ");


scanf("%f", &principal);

printf("Enter the rate of interest (in percentage): ");


scanf("%f", &rate);

printf("Enter the time (in years): ");


scanf("%f", &time);

simpleInterest = (principal * rate * time) / 100;

printf("\nSimple Interest: %.2f\n", 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;
};

void insertAtBeginning(struct Node **head, int data) {


struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}

void insertAtEnd(struct Node **head, int data) {


struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node *temp = *head;
newNode->data = data;
newNode->next = NULL;

if (*head == NULL) {

*head = newNode;
return;
}
Page | 2
while (temp->next != NULL) {
temp = temp->next;

}

temp->next = newNode;
}

void deleteNode(struct Node **head, int value) {


struct Node *temp = *head;
struct Node *prev = NULL;

if (temp != NULL && temp->data == value) {


*head = temp->next;
free(temp);
return;
}

while (temp != NULL && temp->data != value) {


prev = temp;
temp = temp->next;
}

if (temp == NULL) {
Page | 2
printf("Node with value %d not found!\n", value);
return;
}

prev->next = temp->next;
free(temp);
}

void displayList(struct Node *head) {


struct Node *temp = head;
if (temp == NULL) {
printf("List is empty.\n");
return;
}
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

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");

printf("3. Delete a node\n");

printf("4. Display the list\n");


printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

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.

Q.8 WAP to search in an array.


Ans #include <stdio.h>

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


for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i;
Page | 2
}
}
return -1;
}

int main() {
int arr[100], size, key, result;

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


scanf("%d", &size);

printf("Enter %d elements of the array:\n", size);


for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

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


scanf("%d", &key);

result = searchElement(arr, size, key);

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;
};

void initStack(struct Stack *stack) {


stack->top = -1;
}

int isFull(struct Stack *stack) {


return stack->top == MAX - 1;
}

int isEmpty(struct Stack *stack) {


return stack->top == -1;
}

void push(struct Stack *stack, int value) {


if (isFull(stack)) {
Page | 2
printf("Stack overflow! Cannot push %d.\n", value);
} else {
stack->arr[++(stack->top)] = value;
printf("Pushed %d to the stack.\n", value);
}
}

int pop(struct Stack *stack) {


if (isEmpty(stack)) {
printf("Stack underflow! The stack is empty.\n");
return -1;
} else {
int poppedValue = stack->arr[(stack->top)--];
printf("Popped %d from the stack.\n", poppedValue);
return poppedValue;
}

void display(struct Stack *stack) {


if (isEmpty(stack)) {
printf("Stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}
}

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

Q.10 WAP to sort array element.


Ans #include <stdio.h>

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


int temp;
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {

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

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;

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


scanf("%d", &size);

printf("Enter %d elements of the array:\n", size);

for (int i = 0; i < size; i++) {

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>

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


int low = 0, high = size - 1, mid;

while (low <= high) {


mid = (low + high) / 2;

if (arr[mid] == key) {
return mid;
}

if (arr[mid] < key)

{low = mid + 1;


}

else {
high = mid - 1;
}
}

Page | 2
return -1;
}

int main() {
int arr[100], size, key, result;

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


scanf("%d", &size);

printf("Enter %d elements of the array in sorted order:\n", size);


for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

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


scanf("%d", &key);

result = binarySearch(arr, size, key);

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.

Q.12 WAP to traverse tree elements.


Ans #include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* left;
Page | 2
struct Node* right;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

void inorderTraversal(struct Node* root) {


if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}

void preorderTraversal(struct Node* root) {


if (root != NULL) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}

void postorderTraversal(struct Node* root) {


if (root != NULL) {
Page | 2
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
}

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);

printf("In-order traversal: ");


inorderTraversal(root);
printf("\n");

printf("Pre-order traversal: ");


preorderTraversal(root);
printf("\n");

printf("Post-order traversal: ");


postorderTraversal(root);
printf("\n");

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;

void initializeQueue(struct Queue* q) {


q->front = -1;
q->rear = -1;
}

int isFull(struct Queue* q) {


if (q->rear == MAX - 1) {
return 1;
}
return 0;
}

int isEmpty(struct Queue* q) {


if (q->front == -1) {
return 1;
Page | 2
}
return 0;
}

void enqueue(struct Queue* q, int value) {


if (isFull(q)) {
printf("Queue is full. Cannot insert %d.\n", value);

} else {
if (q->front == -1) {
q->front = 0;
}
q->rear++;
q->arr[q->rear] = value;
printf("%d inserted into the queue.\n", value);
}
}

int dequeue(struct Queue* q) {


int value;
if (isEmpty(q)) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
} else {
value = q->arr[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else
q->front++;
}
Page | 2
Return

Page | 2
}

void display(struct Queue* q) {


if (isEmpty(q)) {
printf("Queue is empty.\n");
} else {
printf("Queue elements: ");
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->arr[i]);
}
printf("\n");
}
}

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);

printf("%d dequeued from the queue.\n", dequeue(&q));


printf("%d dequeued from the queue.\n", dequeue(&q));

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

Q.14 WAP of lower triangular matrices.


Ans #include <stdio.h>

#define MAX 10

void lowerTriangular(int matrix[MAX][MAX], int n) {


printf("Lower Triangular Matrix:\n");
Page | 2
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i < j)

Page | 2
matrix[i][j] = 0;
}
}
}

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


for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int n;
int matrix[MAX][MAX];

printf("Enter the size of the matrix (n x n): ");


scanf("%d", &n);

printf("Enter the elements of the matrix:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}

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>

#define MAX 10

void upperTriangular(int matrix[MAX][MAX], int n) {


printf("Upper Triangular Matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i > j) {

matrix[i][j] = 0;
}
}
}

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


Page | 2
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int n;
int matrix[MAX][MAX];

printf("Enter the size of the matrix (n x n): ");


scanf("%d", &n);

printf("Enter the elements of the matrix:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}

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) {

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

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

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

int temp = arr[j];


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

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


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

int main() {
int n;

Page | 2
cout << "Enter the number of elements: ";
cin >> n;

int arr[n];

cout << "Enter the elements of the array: ";


for (int i = 0; i < n; i++) {
cin >> arr[i];
}

bubbleSort(arr, n);

cout << "Sorted array: ";


printArray(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;

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

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

int minIndex = i;


for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

if (minIndex != i) {
int temp = arr[i];
Page | 2
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}

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


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

int main() {
int n;

cout << "Enter the number of elements: ";


cin >> n;

int arr[n];

cout << "Enter the elements of the array: ";


for (int i = 0; i < n; i++) {
cin >> arr[i];
}

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

You might also like