From the course: Parallel and Concurrent Programming with C++ Part 2

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Divide and conquer: C++ demo

Divide and conquer: C++ demo - C++ Tutorial

From the course: Parallel and Concurrent Programming with C++ Part 2

Start my 1-month free trial

Divide and conquer: C++ demo

- [Instructor] To demonstrate a parallel divide and conquer algorithm in C++ we'll implement a function that sums together all of the integer values over a range between two input values low and high. We'll start with this single threaded implementation of a recursive sum algorithm on line six which takes two input parameters low and high values representing the range of numbers to sum over. The if statement on line seven looks at the difference between the low and high value to determine if the problem has been sufficiently subdivided. If so we've reached the base case and it returns the sum of numbers in that range. Otherwise the else statement beginning on line 13 determines the middle index between low and high then recursively calls the recursive sum function on the low to middle index which is referred to as the left half and from the middle to high index, the right half. Then it returns the sum of those left and…

Contents