0% found this document useful (0 votes)
2 views9 pages

Unit 4- Threads_Program Solution

The document contains multiple C++ code examples demonstrating multithreading techniques. It includes printing even and odd numbers, counting up and down, printing squares, and performing matrix multiplication, merge sort, and quick sort using threads. Each example illustrates the use of threads to execute tasks concurrently, enhancing performance and efficiency.

Uploaded by

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

Unit 4- Threads_Program Solution

The document contains multiple C++ code examples demonstrating multithreading techniques. It includes printing even and odd numbers, counting up and down, printing squares, and performing matrix multiplication, merge sort, and quick sort using threads. Each example illustrates the use of threads to execute tasks concurrently, enhancing performance and efficiency.

Uploaded by

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

1.

Print even and odd numbers from 1 to 20 in a separate thread – use


functions
#include <iostream>
#include <thread>

void printEven() {
for (int i = 1; i <= 20; ++i) {
if (i % 2 == 0)
std::cout << "Even: " << i << std::endl;
}
}

void printOdd() {
for (int i = 1; i <= 20; ++i) {
if (i % 2 != 0)
std::cout << "Odd: " << i << std::endl;
}
}
int main() {
// Create two threads
std::thread t1(printEven);
std::thread t2(printOdd);

// Wait for both to finish


t1.join();
t2.join();

std::cout << "Both even and odd numbers printed.\n";


return 0;
}

2. Count down from 10 to 1 and count up from 1 to 10 in a thread using a


class

#include <iostream>
#include <thread>

class Countdown {
public:
void operator()() {
for (int i = 10; i >= 1; --i) {
std::cout << "Countdown: " << i << std::endl;
}
}
};

int main() {
Countdown count;
std::thread t(count);
t.join();
std::cout
#include <iostream>
#include <thread>
#include <chrono>

class Countdown {
public:
void operator()() {
for (int i = 10; i >= 1; --i) {
std::cout << "Countdown: " << i << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
};

class Countup {
public:
void operator()() {
for (int i = 1; i <= 10; ++i) {
std::cout << "Countup: " << i << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
};

int main() {
Countdown down;
Countup up;
std::thread t1(down); // Thread for countdown
std::thread t2(up); // Thread for countup

t1.join();
t2.join();

std::cout << "Both countdown and countup complete.\n";


return 0;
}
3. Print squares of numbers from 1 to 10

#include <iostream>
#include <thread>

int main() {
auto printSquares = []() {
for (int i = 1; i <= 10; ++i) {
std::cout << "Square of " << i << " is " << (i * i) << std::endl;
}
};

std::thread t(printSquares);
t.join();
std::cout << "All squares printed.\n";
return 0;
}

4. Multithreaded Matrix Multiplication


#include <iostream>
#include <thread>

const int ROWS1 = 3;


const int COLS1 = 3;
const int ROWS2 = 3;
const int COLS2 = 3;

int A[ROWS1][COLS1] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int B[ROWS2][COLS2] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};

int result[ROWS1][COLS2] = {0};

// Function to multiply one row


void multiplyRow(int row) {
for (int col = 0; col < COLS2; ++col) {
result[row][col] = 0;
for (int k = 0; k < COLS1; ++k) {
result[row][col] += A[row][k] * B[k][col];
}
}
}

int main() {
if (COLS1 != ROWS2) {
std::cout << "Matrix multiplication not possible: incompatible
dimensions." << std::endl;
return 1;
}

std::thread threads[ROWS1];

// Create a thread for each row


for (int i = 0; i < ROWS1; ++i) {
threads[i] = std::thread(multiplyRow, i);
}

// Wait for all threads to finish


for (int i = 0; i < ROWS1; ++i) {
threads[i].join();
}

// Print the result


std::cout << "Resultant Matrix:" << std::endl;
for (int i = 0; i < ROWS1; ++i) {
for (int j = 0; j < COLS2; ++j) {
std::cout << result[i][j] << " ";
}
std::cout << std::endl;
}

return 0;
}

5. Multithreaded Merge Sort


#include <iostream>
#include <thread>

const int SIZE = 7;


int arr[SIZE] = {13, 9, 12, 8, 7, 6, 1};

// Merge function to merge two sorted parts of array


void merge(int low, int mid, int high) {
int n1 = mid - low + 1;
int n2 = high - mid;

int* left = new int[n1];


int* right = new int[n2];

for (int i = 0; i < n1; ++i)


left[i] = arr[low + i];
for (int j = 0; j < n2; ++j)
right[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = low;

while (i < n1 && j < n2)


arr[k++] = (left[i] <= right[j]) ? left[i++] : right[j++];

while (i < n1)


arr[k++] = left[i++];
while (j < n2)
arr[k++] = right[j++];
delete[] left;
delete[] right;
}

// Regular merge sort for a portion of the array


void mergeSort(int low, int high) {
if (low >= high) return;

int mid = (low + high) / 2;


mergeSort(low, mid);
mergeSort(mid + 1, high);
merge(low, mid, high);
}

int main() {
std::cout << "Original array: ";
for (int i = 0; i < SIZE; ++i)
std::cout << arr[i] << " ";
std::cout << std::endl;

// Thread 1 will sort indices 0 to 2 (13, 9, 12)


std::thread t1(mergeSort, 0, 2);

// Thread 2 will sort indices 3 to 6 (8, 7, 6, 1)


std::thread t2(mergeSort, 3, 6);

// Wait for both threads to complete


t1.join();
t2.join();

// Merge the two sorted parts


merge(0, 2, 6);

std::cout << "Sorted array: ";


for (int i = 0; i < SIZE; ++i)
std::cout << arr[i] << " ";
std::cout << std::endl;

return 0;
}

6. Multithreaded Quick Sort


#include <iostream>
#include <thread>

const int SIZE = 7;


int arr[SIZE] = {13, 9, 12, 8, 7, 6, 1};

// Swap function
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}

// Partition function for Quick Sort


int partition(int low, int high) {
int pivot = arr[high];
int 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;
}

// Quick Sort
void quickSort(int low, int high) {
if (low < high) {
int pivotIndex = partition(low, high);
quickSort(low, pivotIndex - 1);
quickSort(pivotIndex + 1, high);
}
}
// Manual merge of two sorted parts
void mergeTwoSortedParts(int start1, int end1, int start2, int end2) {
int n1 = end1 - start1 + 1;
int n2 = end2 - start2 + 1;
int* temp = new int[n1 + n2];

int i = start1, j = start2, k = 0;

while (i <= end1 && j <= end2) {


if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}

while (i <= end1) temp[k++] = arr[i++];


while (j <= end2) temp[k++] = arr[j++];

// Copy back to original array


for (int l = 0; l < n1 + n2; ++l) {
arr[start1 + l] = temp[l];
}

delete[] temp;
}

int main() {
std::cout << "Original array: ";
for (int i = 0; i < SIZE; ++i)
std::cout << arr[i] << " ";
std::cout << std::endl;

// Thread 1 sorts elements at indices 0–2


std::thread t1(quickSort, 0, 2);

// Thread 2 sorts elements at indices 3–6


std::thread t2(quickSort, 3, 6);
t1.join();
t2.join();

// Merge the two sorted parts


mergeTwoSortedParts(0, 2, 3, 6);

std::cout << "Sorted array: ";


for (int i = 0; i < SIZE; ++i)
std::cout << arr[i] << " ";
std::cout << std::endl;

return 0;
}

You might also like