Given a 2-D matrix, find the element having maximum value using multi-threading.

Prerequisite : Multithreading

Examples :

Input : {{1, 5, 3, 6}, {22, 10, 4, 34}, {4, 45, 67, 3}, {69, 3, 23, 3}} Output :69 Input :{{1, 2, 3} {2, 4, 5}} Output :5

A matrix can be of very large size so when it comes to traversing it, it will take lot of time. When finding the max element in a matrix, each element of the matrix is to be traversed which will take more time. So, to minimize the time taken in traversing the matrix can be avoided using multi-threading.

`// CPP code to find max in 2d ` `// array using multi-threading ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// declaring two thread_id variable ` `pthread_t ` `thread` `[2]; ` ` ` `// structure for passing arguments ` `typedef` `struct` `dim ` `{ ` ` ` `int` `s, e; ` `}dim; ` ` ` `// matrix of 4X4 ` `int` `mat1[][4] = {{1, 5, 3, 6}, {22, 80, 4, 34}, ` ` ` `{4, 45, 67, 3}, {99, 3, 23, 3}}; ` `int` `maxf[2]; ` ` ` `// function that find max from a given array ` `void` `*max(` `void` `*size) ` `{ ` ` ` `int` `i, j, max; ` ` ` `dim *b = (dim *)size; ` ` ` `max = mat1[b -> s][0]; ` ` ` ` ` `// finding max ` ` ` `for` `(i = b -> s; i < b -> e; i++) ` ` ` `{ ` ` ` `for` `(j = 0; j < 4; j++) ` ` ` `{ ` ` ` `if` `(max < mat1[i][j]) ` ` ` `max = mat1[i][j]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// storing max from first half of ` ` ` `// 2-d array into 0th index ` ` ` `if` `(b -> s == 0) ` ` ` `maxf[0] = max; ` ` ` ` ` `// storing max from second half of ` ` ` `// 2-d array into 1st index ` ` ` `else` ` ` `maxf[1] = max; ` `} ` ` ` `// driver function ` `int` `main() ` `{ ` ` ` `int` `i, j; ` ` ` `dim *a, *b; ` ` ` `a = (dim *)` `malloc` `(` `sizeof` `(dim)); ` ` ` `b = (dim *)` `malloc` `(` `sizeof` `(dim)); ` ` ` ` ` `// creating thread1 ` ` ` `a -> s = 0; a -> e = 4/2; ` ` ` `pthread_create(&` `thread` `[0], NULL, ` ` ` `&max, (` `void` `*)a); ` ` ` ` ` `b -> s = 4/2; b -> e = 4; ` ` ` ` ` `// creating thr ead 2 ` ` ` `pthread_create(&` `thread` `[1], NULL, ` ` ` `&max, (` `void` `*)b); ` ` ` ` ` `// waiting until the complition of threads ` ` ` `pthread_join(` `thread` `[0], NULL); ` ` ` `pthread_join(` `thread` `[1], NULL); ` ` ` ` ` `// printing max ` ` ` `if` `(maxf[0] > maxf[1]) ` ` ` `cout << maxf[0] << endl; ` ` ` `else` ` ` `cout<< maxf[1] << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Multithreading in C
- Multithreading in C++
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- C++ program to Convert a Matrix to Sparse Matrix
- Determinant of N x N matrix using multi-threading
- Maximum element in a very large array using pthreads
- How to find the minimum and maximum element of a Vector using STL in C++?
- How to find the minimum and maximum element of an Array using STL in C++?
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- C++ Program to Print Matrix in Z form
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Program to print elements of a Matrix row-wise skipping alternate elements
- Program to convert the diagonal elements of the matrix to 0
- Replace the middle element of matrix with sum of surrounding elements
- Find safe cells in a matrix
- Check if all enemies are killed with bombs placed in a matrix
- Remove first X rows and columns from a matrix
- Remove any corner X rows and columns from a matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.