## C++ Program to Create the Prufer Code for a Tree

```#include<iostream>

using namespace std;

int main() {
int i, j, ver, edg, minimum, p;

cout<<"Enter the number of vertexes: ";
cin>>ver;
cout<<endl;

edg = ver-1;
int EDG[edg][2], DG[ver+1] = {0};

cout<<"This tree has "<<edg<<" edges for "<<ver<<"vertexes.\n";

cout<<"There are "<<edg<<" pairs of vertexes in the three.\n";
for(i = 0; i < edg; i++) {
cout<<"Enter the value of vertex pair for edge "<<i+1<<":\n";
cout<<"Enter the value of V(1) vertex: ";
cin>>EDG[i][0];
cout<<"Enter the value of V(2) vertex: ";
cin>>EDG[i][1];

DG[EDG[i][0]]++;
DG[EDG[i][1]]++;
}

cout<<"\nThe Prufer code for the tree is: { ";  // Print the prufer code of the given tree.
for(i = 0; i < ver-2; i++) {
minimum = 10000;

for(j = 0; j < edg; j++) {

if(DG[EDG[j][0]] == 1) {

if(minimum > EDG[j][0]) {
minimum = EDG[j][0];
p = j;
}
}
if(DG[EDG[j][1]] == 1) {

if(minimum > EDG[j][1]) {

minimum = EDG[j][1];
p = j;
}
}
}

DG[EDG[p][0]]--;  // Remove the selected vertex by decreasing its degree to 0.

DG[EDG[p][1]]--;  	// Decrement the degree of other vertex, since we have removed the EDG.

if(DG[EDG[p][0]] == 0)
cout<<EDG[p][1]<<" ";
else
cout<<EDG[p][0]<<" ";
}
cout<<"}";

return 0;
}```

## Print prime numbers in a given range using C++

```#include<bits/stdc++.h>
using namespace std;

typedef unsigned long long int stl;

vector<stl> number(stl a)
{

vector<bool> Prime_Number(a+1,true);

Prime_Number[0] = false;
Prime_Number[1] = false;
int b = sqrt(a);

for (stl pr=2; pr<=b; pr++) {

if (Prime_Number[pr]) {
for (stl i=pr*2; i<=a; i += pr)
Prime_Number[i] = false;
}
}

vector<stl> result;
for (int i=0;i<a;i++)
if (Prime_Number[i])
result.push_back(i);
return result;
}

bool remove_zero(stl i) // Used to remove zeros from a vector
{
return i == 0;
}

vector<stl> Number_Range(stl First_Num,stl Last_Num)
{
vector<stl> s1 = number(First_Num);  // find primes from o to First_Num

vector<stl> s2 = number(Last_Num);  // find primes from o to Last_Num

vector<stl> result(Last_Num-First_Num);
set_difference(s2.begin(), s2.end(), s1.begin(), s2.end(), result.begin()); // find set //difference of two vectors

vector<stl>::iterator itr = remove_if(result.begin(),result.end(),remove_zero); //remove extra zeros.

result.resize(itr-result.begin());

return result;
}

int main(void) {
stl First_Num = 20, Last_Num = 50;
vector<stl> result = Number_Range(First_Num,Last_Num);
cout<<"The Prime Numbers from "<<First_Num<<" to "<<Last_Num<< " are: ";
for (auto i:result)
cout<<i<<' ';
return 0;
}```

## Compile and Execute C++ Online

```#include <iostream>
#include <cfloat>
#include <cstdlib>
#include <cmath>
using namespace std;

struct poi {
double poi1, poi2;
};

inline int Comp_poi1(const void* x, const void* b) {
poi *p1 = (poi *)x,  *pnt2 = (poi *)b;
return (p1->poi1 - pnt2->poi1);
}

inline int Comp_poi2(const void* x, const void* y) {
poi *pnt1 = (poi *)x,   *pnt2 = (poi *)y;
return (pnt1->poi2 - pnt2->poi2);
}

inline double Distance(poi pnt1, poi pnt2)   // Calculate the distance between two points
{
return sqrt( (pnt1.poi1 - pnt2.poi1)*(pnt1.poi1 - pnt2.poi1) +
(pnt1.poi2 - pnt2.poi2)*(pnt1.poi2 - pnt2.poi2) );
}

double S_Distance(poi P[], int n, poi &pnt1, poi &pnt2)
{
double min = DBL_MAX;
for (int i = 0; i < n; ++i)
for (int j = i+1; j < n; ++j)
if (Distance(P[i], P[j]) < min) {
min = Distance(P[i], P[j]);
pnt1.poi1 = P[i].poi1, pnt1.poi2 = P[i].poi2;
.poi1 = P[j].poi1, pnt2.poi2 = P[j].poi2;
}
return min;
}

inline double Minimum(double poi1, double poi2)   // Find minimum between two values
{
return (poi1 < poi2)? poi1 : poi2;
}

double Closest_dist_Spoint(poi stp[], int s, double dist, poi &pnt1, poi &pnt2)   // Calculate distance beween the closest points
{
double Minimum = dist;  // Initialize the minimum distance as dist

qsort(stp, s, sizeof(poi), Comp_poi2);

for (int i = 0; i < s; ++i)
for (int j = i+1; j < s && (stp[j].poi2 - stp[i].poi2) < Minimum; ++j)
if (Distance(stp[i],stp[j]) < Minimum) {
Minimum = Distance(stp[i], stp[j]);
pnt1.poi1 = stp[i].poi1, pnt1.poi2 = stp[i].poi2;
pnt2.poi1 = stp[j].poi1, pnt2.poi2 = stp[j].poi2;
}

return Minimum;
}

double Closest_dist(poi P[], poi stp[], int n, poi &pnt1, poi &pnt2)  // Calculate smallest distance.
{
static poi pt1, pt2, pt3, pt4;

if (n <= 3)
return S_Distance(P, n, pt1, pt2);

int medium = n/2;  // Calculate the mid point
poi mediumPoint = P[medium];

double D_Left = Closest_dist(P, stp, medium, pt1, pt2);   // D_Left: left of medium point
double D_Right = Closest_dist(P + medium, stp, n-medium, pt3, pt4);   // D_Right: right side of the medium point

if(D_Left < D_Right) {
pnt1.poi1 = pt1.poi1; pnt1.poi2 = pt1.poi2;   // Store the pair that has smaller distance
pnt2.poi1 = pt2.poi1; pnt2.poi2 = pt2.poi2;
} else {
pnt1.poi1 = pt3.poi1; pnt1.poi2 = pt3.poi2;
pnt2.poi1 = pt4.poi1; pnt2.poi2 = pt4.poi2;
}

double min_dist = Minimum(D_Left, D_Right);

int j = 0;
for (int i = 0; i < n; i++)
if (abs(P[i].poi1 - mediumPoint.poi1) < min_dist)
stp[j++] = P[i];

double min_dist_strip = Closest_dist_Spoint(stp, j, min_dist, pt1, pt2);
double F_Min = min_dist;
if(min_dist_strip < min_dist) {
pnt1.poi1 = pt1.poi1; pnt1.poi2 = pt1.poi2;
pnt2.poi1 = pt2.poi1; pnt2.poi2 = pt2.poi2;
F_Min = min_dist_strip;
}
return F_Min;
}

int main() {
poi P[] = {{4, 1}, {15, 20}, {30, 40}, {8, 4}, {13, 11}, {5, 6}};

poi pnt1 = {DBL_MAX, DBL_MAX}, pnt2 = {DBL_MAX, DBL_MAX};  // Closest pair of points in array

int n = sizeof(P) / sizeof(P[0]);
qsort(P, n, sizeof(poi), Comp_poi1);

poi *stp = new poi[n];

cout << "The closest distance of point in array is: " << Closest_dist(P, stp, n, pnt1, pnt2) << endl;
cout << "The closest pair of point in array: (" << pnt1.poi1 << "," << pnt1.poi2 << ") and ("
<< pnt2.poi1 << "," << pnt2.poi2 << ")" << endl;

delete[] stp;

return 0;
}
```

## C++ Program to Implement Sparse Array

```#include<iostream>
using namespace std;
int main () {
int a[10][10] = { {0, 0, 9} , {5, 0, 8} , {7, 0, 0} };
int i, j, count = 0;
int row = 3, col = 3;
for (i = 0; i < row; ++i) {
for (j = 0; j < col; ++j){
if (a[i][j] == 0)
count++;
}
}
cout<<"The matrix is:"<<endl;
for (i = 0; i < row; ++i) {
for (j = 0; j < col; ++j) {
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"The number of zeros in the matrix are "<< count <<endl;
if (count > ((row * col)/ 2))
cout<<"This is a sparse matrix"<<endl;
else
cout<<"This is not a sparse matrix"<<endl;
return 0;
}```

## C++ Program to Find the maximum subarray sum using Binary Search approach

```#include<iostream>

using namespace std;

int maximum(int val1, int val2)  // find the maximum of two integers
{
return (val1 > val2)? val1:val2;
}

int MCS(int array[], int l, int m, int h)  // find the maximum sum sub-array which includes medium of the sub-array.
{
int s = 0;
int sum_of_left_part = -1;
for (int i = m; i >= l; i--) {
s = s + array[i];
if (s > sum_of_left_part)
sum_of_left_part = s;
}

s = 0;
int sum_of_right_part = -1;
for (int i = m+1; i <= h; i++) {
s = s + array[i];
if (s > sum_of_right_part)
sum_of_right_part = s;
}

return sum_of_left_part + sum_of_right_part;   // Return sum of elements on left and right of medium.
}

int MaximumSum_of_SubArray(int array[], int l, int h) {
int m;

if (l == h)
return array[l];

m = (l + h)/2;

return maximum(maximum(MaximumSum_of_SubArray(array, l, m), MaximumSum_of_SubArray(array, m+1, h)), MCS(array, l, m, h));
}

int main() {
int number_of_elements, i;
cout<<"Enter the number of elements of array: ";
cin>> number_of_elements;
cout<<endl;

int a[number_of_elements];
for(i = 0; i < number_of_elements; i++) {
cout<<"Enter the element of "<<i+1<<": ";
cin>>a[i];
}

cout<<"\nMaximum sum of Sub-Array is: "<<MaximumSum_of_SubArray(a, 0, number_of_elements -1);   // Print the maximum sum sub-array.

return 0;
}```

## Compile and Execute C++ Online

```#include <iostream>

using namespace std;

int main()
{
cout << "Hello World" << endl;

return 0;
}

```

## pass a Pointer by Reference in C++

```#include <iostream>
using namespace std;
void Decrement( int*& d ) {
--d;
}

int main( void ) {
int a = 26;
int* ptr = &a; // pointer to pass

// print before decrement
cout<<"Before: "<< ptr << endl;

Decrement( ptr);

// print after increment
cout<<"After: " << ptr;

return 0;
}```

## NULL pointers in C++

```#include <iostream>
using namespace std;
int main() {
int *p= NULL;       //initialize the pointer as null.
cout<<"The value of pointer is ";
cout<<p;

return 0;
}```

## NULL pointers in C

```#include <stdio.h>
int main() {
int *p= NULL; //initialize the pointer as null.
printf("The value of pointer is %u",p);
return 0;
}```

## this pointer in C++

```#include<iostream>
using namespace std;
class AB {
int x;
public:
AB() {
x = 6;
}

// here Local parameter 'x' hides object's member
// 'x', we can access it using this.
void print(int x)
{
cout<<"the number is: " << this->x;
}
};

int main() {
AB ob;
int m = 7 ;
ob.print(m);
return 0;
}```