Question: Recursive Maximum Subarray C++ I have two separate .cpp included recursive and main. Where am I doing wrong? Please expert to help me out. I

Recursive Maximum Subarray C++ I have two separate .cpp included recursive and main. Where am I doing wrong? Please expert to help me out. I really appreciate your time and work.

--------------------------------------------------------------

// file : recursive.cpp #include

using namespace std;

int max(int a, int b) { return (a > b) ? a : b; }

// A function to find the maximum sum sub-array which includes mid of the sub-array. int MaxCrossingSum(int arr[], int low, int mid, int high) { // Include elements having index value less than or equal to the mid. int sum = 0; int leftpartsum = -1; for (int i = mid; i >= low; i--) { sum = sum + arr[i]; if (sum > leftpartsum) leftpartsum = sum; }

// Include elements having index value greater mid. sum = 0; int rightpartsum = -1; for (int i = mid + 1; i <= high; i++) { sum = sum + arr[i]; if (sum > rightpartsum) rightpartsum = sum; }

// Return sum of elements on left and right of mid. return leftpartsum + rightpartsum; }

// Returns sum of maxium subarray sum. int MaxSubArraySum(int arr[], int low, int high) { int mid; // If low index is equal to the high index h then the subarray contains only one element. if (low == high) return arr[low];

// Otherwise find the mid index and proceed. mid = (low + high) / 2;

// Maximum sum sub-array can be either in the left part, right part or covering elements from both parts. return max(max(MaxSubArraySum(arr, low, mid), MaxSubArraySum(arr, mid + 1, high)), MaxCrossingSum(arr, low, mid, high)); }

-------------------------------------------------------------------

// file : main.cpp

#include

using namespace std;

int main(int argc, char* argv[]) {

int a[] = { 1, 2, 3, 4, 5 }; int n = sizeof(a) / sizeof(a[0]);

//find_maximum_subarray(a, 0, n - 1); cout << " Recursive: bestSum:" << MaxSubArraySum(a, 0, n-1);

system("pause");

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!