 # C++ Programming Code Examples

## C++ > Arrays and Matrices Code Examples

### Program to Find kth Smallest Element by the Method of Partitioning the Array

``` Program to Find kth Smallest Element by the Method of Partitioning the Array 1. Implement partitioning to find the Kth smallest number from a dataset of n element. 2. The time complexity of this algorithm is O(n*log(n)). 1. Take the input of the data set. 2. Use the partition algorithm. 3. As we place the pivot at the (k-1)th index it will be the kth smallest number. 3. Exit. #include<iostream> using namespace std; // Swapping two values. void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } // Partitioning the array on the basis of values at high as pivot value. int CreatePartition(int a[], int low, int high) { int pivot, index, x; index = low; pivot = high; // Getting index of pivot. for(x=low; x < high; x++) { if(a[x] < a[pivot]) { swap(&a[x], &a[index]); index++; } } // Swapping value at high and at the index obtained. swap(&a[pivot], &a[index]); return index; } // Implementing Partition. int Partition(int a[], int low, int high, int k) { int pindex; if(low < high) { // Partitioning array using last element as a pivot. // Recursively implementing partitioning in the direction to place the pivot at (k-1)th pivot. pindex = CreatePartition(a, low, high); if(pindex == k-1) return k-1; else if(pindex > k-1) Partition(a, low, pindex-1, k); else Partition(a, pindex+1, high, k); } } int main() { int n, x, k, kk; cout<<"\nEnter the number of data element: "; cin>>n; int arr[n]; for(x = 0; x < n; x++) { cout<<"Enter element "<<x+1<<": "; cin>>arr[x]; } cout<<"\nEnter the k for the kth smallest element: "; cin>>k; kk = Partition(arr, 0, n-1, k); // Printing the result. cout<<"\nThe kth smallest element: "<<arr[kk]; return 0; } ```

Count total number of Vowels Consonants - C program to find total number of vowels and consonants in a string using 'Loop and If Else'. 'Input String', store it in some variable say str. Initialize 2 other variables to store vowel and

C Programming Code Use Goto Statement - The goto statement is rarely used because it makes program confusing, less readable and complex. Also, when this is used, the control of the program won't be easy to trace, hence

Program to Generate All Pairs of Subsets - The "time complexity" of this algorithm is O (n*2^n). This algorithm takes the input of 'n' data element and prints unique subset pairs whose union gives the set. It generates 0 to

Calculate Display the Grade of the Student - To calculate "grade of a student" on the basis of his total marks in C++ Programming, ask to the user to enter marks obtained in some subjects (5 subjects here). To calculate grade

Swapping Two Numbers Without Variable - Imagine we have two number 1 and 2 stored in x and j respectively now. If we add x and j (1 + 2) and store it to x then x will become 3 and j is still 2. We subtract j (2) from new value of

Binary Search Tree Code in C++ Language - Smaller elements go left. Larger elements go right. All insertions are as leaf nodes. Remove a leaf node and remove a node with a single child. Remove a node with 2 children, replace

Check If a Given Number is Prime Number - Program takes a number and checks whether a given number is Prime or not. Take number as Input & Check if the number is Divisible by any of the natural numbers starting from 2. If

Code find maximum using ladder if.else.if - Instead of using Nested If Else. Lets combine 2 or more conditions together using Logical Operators. A number j1 among 3 numbers j1, j2 and j3 is said maximum if j1 > j2 and j1 > j3.