0% found this document useful (0 votes)
24 views11 pages

Nikhilesh DAA Practical

The document is a practical file for a B.Tech course on Design & Analysis of Algorithms, containing multiple programs in C++. It includes implementations for matrix addition, subtraction, multiplication, linear search, binary search, and quick sort. Each program is presented with code and prompts for user input.

Uploaded by

nikhileshadd2
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)
24 views11 pages

Nikhilesh DAA Practical

The document is a practical file for a B.Tech course on Design & Analysis of Algorithms, containing multiple programs in C++. It includes implementations for matrix addition, subtraction, multiplication, linear search, binary search, and quick sort. Each program is presented with code and prompts for user input.

Uploaded by

nikhileshadd2
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/ 11

PCTE Institute of Engineering &

Technology, Ludhiana

B.Tech 4th Semester


BTCS 403-18

DESIGN & ANALYSIS OF ALGORITHMS


PRESENTATION SYNOPSIS

PRACTICAL FILE

NAME: NIKHILESH SHARMA


ROLL.NO: 2329391
CONTACT NUMBER: 9417319477
E-MAIL ADDRESS: [email protected]
Program 1: Program for addition and
subtraction of two matrices.

#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows and columns: "<<endl;
cin >> rows >> cols;

int A[rows][cols], B[rows][cols], sum[rows][cols], diff[rows][cols];

cout << "Enter elements of first matrix:" << endl;


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> A[i][j];
}
}

cout << "Enter elements of second matrix:" << endl;


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> B[i][j];
}
}

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


for (int j = 0; j < cols; j++) {
sum[i][j] = A[i][j] + B[i][j];
diff[i][j] = A[i][j] - B[i][j];
}
}

cout << "Sum of matrices:" << endl;


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << sum[i][j] << " ";
}
cout << endl;
}

cout << "Difference of matrices:" << endl;


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << diff[i][j] << " ";
}
cout << endl;
}

return 0;
}
Program 2: Program for implementing
multiplication of two matrices.

#include <iostream>
using namespace std;

int main() {
int r1, c1, r2, c2;
cout << "Enter rows and columns of first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns of second matrix: ";
cin >> r2 >> c2;

if (c1 != r2) {
cout << "Matrix multiplication not possible.";
return 0;
}

int A[r1][c1], B[r2][c2], C[r1][c2] = {0};

cout << "Enter elements of first matrix:";


for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j++)
cin >> A[i][j];

cout << "Enter elements of second matrix:";


for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
cin >> B[i][j];
for (int i = 0; i < r1; i++)
for (int j = 0; j < c2; j++)
for (int k = 0; k < c1; k++)
C[i][j] += A[i][k] * B[k][j];

cout << "Product of matrices:" << endl;


for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++)
cout << C[i][j] << " ";
cout << endl;
}

return 0;
}
Program 3: Implement linear search using
one dimensional arrays.

#include <iostream>
using namespace std;

int main() {
int n, key, found = 0;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];

cout << "Enter elements:";


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

cout << "Enter element to search: ";


cin >> key;

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


if (arr[i] == key) {
cout << "Element found at index " << i;
found = 1;
break;
}
}

if (!found) cout << "Element not found.";


return 0;
}
Program 4: Program for implementing Binary
Search also using recursion.

#include <iostream>
using namespace std;

int binarySearch(int arr[], int low, int high, int key) {


if (low > high) return -1;
int mid = (low + high) / 2;
if (arr[mid] == key) return mid;
if (arr[mid] > key) return binarySearch(arr, low, mid - 1, key);
return binarySearch(arr, mid + 1, high, key);
}

int main() {
int n, key;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];

cout << "Enter sorted elements: ";


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

cout << "Enter element to search: ";


cin >> key;

int result = binarySearch(arr, 0, n - 1, key);


if (result != -1)
cout << "Element found at index " << result;
else
cout << "Element not found.";

return 0;
}
Program 5: Program for implementing quick sort.

#include <iostream>
using namespace std;

int partition(int arr[], int low, int high) {


int pivot = arr[high], i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];

cout << "Enter elements: ";


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

quickSort(arr, 0, n - 1);

cout << "Sorted array: ";


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

return 0;
}

You might also like