Found 7347 Articles for C++

Convex Hull Jarvis’s Algorithm or Wrapping in C++

Ayush Gupta
Updated on 29-Jan-2020 07:37:54

517 Views

In this tutorial, we will be discussing a program to find the convex hull of a given set of points using Jarvis’s Algorithm.Convex hull is the smallest polygon convex figure containing all the given points either on the boundary on inside the figure.In Jarvis’s algorithm, we select the leftmost point and keep wrapping points moving in the clockwise direction.Example Live Demo#include using namespace std; //structure of the point struct Point{    int x, y; }; //calculating the position of the points int cal_orientation(Point p, Point q, Point r){    int val = (q.y - p.y) * (r.x - q.x) - ... Read More

Converting seconds into days, hours, minutes and seconds in C++

Ayush Gupta
Updated on 29-Jan-2020 07:35:56

986 Views

In this tutorial, we will be discussing a program to convert seconds into days, hours, minutes and seconds.For this we will be provided with a random number of seconds. Our task is to convert it into proper number of days, hours, minutes and seconds respectively.Example Live Demo#include using namespace std; //converting into proper format void convert_decimal(int n) {    int day = n / (24 * 3600);    n = n % (24 * 3600);    int hour = n / 3600;    n %= 3600;    int minutes = n / 60 ;    n %= 60;    int seconds = n;    cout

All combinations of strings that can be used to dial a number in C/C++?

Arnab Chakraborty
Updated on 29-Jan-2020 07:43:50

83 Views

With respect of a given number, display or print all possible combinations of strings that can be implemented to dial the given number in a phone with the help of following pecifications.In the given phone, we can dial, 2 implementing A or B or C, 3 implementing D or E or F, ……………….8 implementing T or U or V, 9 implementing W or X or Y or Z, 1 implementing only 10 implementing 0.For example if 89, is the given phone number, the program should printTW, TX, TY, TZ, UW, UX, UY, UZ, VW, VX, VY, VZ#include #include ... Read More

Some Interesting Observations about C/C++ Ternary Operator

Arnab Chakraborty
Updated on 29-Jan-2020 07:29:59

215 Views

We know that ternary operator is implemented instead of if..else clause. It is denoted by ?: . '?' symbol is equivalent to if part and ':' is equivalent to else part. The following 3 programs explain some interesting observation in case of ternary operator.The following program is able to compile without any error. Ternary expression’s return type is expected to be float (as that of exp2) and exp3 (i.e. literal zero - int type) is able to implicitly convert to float.#include using namespace std; int main(){    int test1 = 0;    float fvalue = 3.111f;    cout

C vs BASH Fork bomb in C/C++?

Arnab Chakraborty
Updated on 29-Jan-2020 07:25:12

330 Views

It is already cleared that the BASH fork bomb is much more powerful than its version of C program. The main cause is that in BASH the created process is detached from the parent. If the parent process (the one we initially started) is destroyed or killed, the remaining or rest of the processes live on. But in case of the C implementation, the listed child processes die automatically if the parent is destroyed or killed. A script is responsible to communicate with the system directly.The fork bomb program in C can be updated or modified. We can be able ... Read More

Coordinates of rectangle with given points lie inside in C++

Ayush Gupta
Updated on 29-Jan-2020 07:30:11

183 Views

In this tutorial, we will be discussing a program to find the coordinates of rectanglewith given points lying inside.For this we will be provided with some coordinate points. Our task is to find the smallest rectangle such that all the points lie inside it and it should have its sides parallel to the coordinate axis.Example Live Demo#include using namespace std; //calculating the coordinates of smallest rectangle void print_rectangle(int X[], int Y[], int n){    //finding minimum and maximum points    int Xmax = *max_element(X, X + n);    int Xmin = *min_element(X, X + n);    int Ymax = *max_element(Y, ... Read More

Convex Hull using Divide and Conquer Algorithm in C++

Ayush Gupta
Updated on 29-Jan-2020 07:28:58

743 Views

In this tutorial, we will be discussing a program to find the convex hull of a given set of points.Convex hull is the smallest polygon convex figure containing all the given points either on the boundary on inside the figure.In this program, we will use brute force to divide the given points into smaller segments and then finally merging the ones that follow on to construct the convex hull.Example Live Demo#include using namespace std; //storing the center point of polygon pair mid; //calculating the quadrant of //a particular point int quad(pair p){    if (p.first >= 0 && p.second >= 0) ... Read More

Convex Hull Monotone chain algorithm in C++

Ayush Gupta
Updated on 29-Jan-2020 07:23:54

139 Views

In this tutorial, we will be discussing a program to find the convex hull of a given set of points.Convex hull is the smallest polygon convex figure containing all the given points either on the boundary on inside the figure.Example Live Demo#include #define llu long long int using namespace std; //structure for the given point struct Point {    llu x, y;    bool operator= t && calc_crossproduct(ans[k - 2],       ans[k - 1], A[i - 1])

Convex Hull Graham Scan in C++

Ayush Gupta
Updated on 29-Jan-2020 07:21:35

687 Views

In this tutorial, we will be discussing a program to find the convex hull of a given set of points.Convex hull is the smallest polygon convex figure containing all the given points either on the boundary on inside the figure.In Graham Scan, firstly the pointes are sorted to get to the bottommost point. Then the points are traversed in order and discarded or accepted to be on the boundary on the basis of their order.Example#include #include #include using namespace std; struct Point{    int x, y; }; //point reference for sorting other points Point p0; //moving to ... Read More

Copy set bits in a range in C++

Ayush Gupta
Updated on 29-Jan-2020 07:18:33

375 Views

In this tutorial, we will be discussing a program to copy set bits of one number to another in the given range.For this we will be provided with two integers. Our task is to see the bits in the first number and set those bits in the second number as well if they are in the given range. Finally returning the digit produced.Example Live Demo#include using namespace std; //copying set bits from y to x void copySetBits(unsigned &x, unsigned y, unsigned l, unsigned r){    //l and r should be between 1 and 32    if (l < 1 || ... Read More

Advertisements