 # C++ Programming Code Examples

## C++ > Sorting Searching Code Examples

### Find the maximum subarray sub using Divide and Conquer Approach

``` Find the maximum subarray sub using Divide and Conquer Approach - This algorithm implements the divide and conquer approach to find the sub-array having a maximum sum. - The worst case time complexity of the algorithm is O(n*log(n)). - Take the input of the integer array. - Using divide and conquer approach break the array. - Compute the individual sum and combine them, to get the global maximum sum. - Exit. #include<iostream> using namespace std; // A function to find the maximum of two integers. 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)); } int main() { int n, i; cout<<"Enter the number of data element in the array: "; cin>>n; int a[n]; for(i = 0; i < n; i++) { cout<<"Enter element "<<i+1<<": "; cin>>a[i]; } // Print the maximum sub-array sum. cout<<"\nMaximum sub-array sum is: "<<MaxSubArraySum(a, 0, n-1); return 0; } ```

C++ Finds Maximum Element in an Arrays - Construct "Binary Search" tree for "unsorted" data array. For the 'Maximum element' move the pointer to the rightmost child node. This value will be the minimum value among data.

C++ Switch Statement Control Statement - "Switch statement" is multi-way decision that tests whether an expression 'matches' one of a number of "constant integer", and branches accordingly. 'Switch statement' that allows us

C++ Two-Dimension Transformations In - This C++ Program code example deals with all two-d transformation such as translation, 'scaling', 'rotation', 'reflection', 'shearing' in homogeneous coordinates. Transformation

Implement Doubly Linked list using Singly - Program implements doubly linked list using singly Linked List. It makes use of 2 pointers, one points at the current node, other points at the head and when user requests to move

C++ Detecting a Keypress and ASCII Code - Program which detects a key pressed and its 'ASCII' value with out pressing enter key and using cin>> on press Esc key program should exit. When press a key its ASCII value should

Illustrate Stack Operations using MACROS - C Language Use #define function to define the macros. Define separate functions for the operations like push, pop, display etc. Use switch statement to access these functions.

C++ Language Implementations For Stack - Test if the stack is logically full. Return true if full, false otherwise. Get the "most recently" inserted item in the stack. Does not alter the stack. Return the most recently inserted item

Check Whether the Number is Armstrong - This C Program code checks whether a given number is armstrong number. An Armstrong number is an n-digit base b number such that the Sum of its Digits raised to the Power N is