Sum of array is a small problem where we have to add each element in the array by traversing through the entire array. But when the number of elements are too large, it could take a lot of time. But this could solved by dividing the array into parts and finding sum of each part simultaneously i.e. by finding sum of each portion in parallel.
This could be done by using multi-threading where each core of the processor is used. In our case, each core will evaluate sum of one portion and finally we will add the sum of all the portion to get the final sum. In this way we could improve the performance of a program as well as utilize the cores of processor.
It is better to use one thread for each core. Although you can create as many thread as you want for better understanding of multi-threading.
Input : 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 Output : sum is 600 Input : 10, 50, 70, 100, 120, 140, 150, 180, 200, 220, 250, 270, 300, 640, 110, 220 Output : sum is 3030
Note – It is advised to execute the program in Linux based system.
Compile in linux using following code:
g++ -pthread program_name.cpp
sum is 600
- Maximum element in a very large array using pthreads
- Addition and Subtraction of Matrix using pthreads
- Odd Even Transposition Sort / Brick Sort using pthreads
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Find minimum value to assign all array elements so that array product becomes greater
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Generate original array from an array that store the counts of greater elements on right
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Print modified array after multiple array range increment operations
- Maximum subarray sum in array formed by repeating the given array k times
- Kth smallest element in the array using constant space when array can't be modified
- Check if it is possible to make array increasing or decreasing by rotating the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.