Found 7347 Articles for C++

Area of the Largest Triangle inscribed in a Hexagon in C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:35:03

232 Views

Here we will see the area of largest triangle which is inscribed in regular hexagon. Each side of the hexagon is ‘a’, and each side of the triangle is ‘b’.From this diagram we can see that if we make one triangle using one side of hexagon, then these two triangles are making each sides into two parts. We can see two right angled triangles also. From the Pythagorus formula, we can say that −So the area is −Example#include #include using namespace std; float area(float a) {    if (a < 0 ) //if value is negative it is ... Read More

Area of the Largest square that can be inscribed in an ellipse in C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:32:24

69 Views

Here we will see the area of largest square that can be inscribed in an ellipse. The square in ellipse will be like below −The area of ellipse is −Now, if x and y are same, thenSo area is −Example#include #include using namespace std; float area(float a, float b) {    if (a < 0 || b < 0 ) //if values are is negative it is invalid       return -1;    float area = (4*(a*a + b*b)) / (a*a*b*b);    return area; } int main() {    float a = 4, b = 2;    cout

Area of the circumcircle of any triangles with sides given in C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:28:26

70 Views

Here we will see how to get the area of the circumcircle of any triangle whose sides are given. Here the side AB is a, BC is b and CA is c, the radius is ‘r’.The radius r is same as −Example#include #include using namespace std; float area(float a, float b, float c) {    if (a < 0 || b < 0 || c < 0) //if values are is negative it is invalid       return -1;    float s = (a + b + c) /2;    float triangle = sqrt(s * (s - ... Read More

All unique triplets that sum up to a given value in C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:26:11

318 Views

Here we will see one interesting problem. We have an array with some elements. One sum value is given. Our task is to find triplets from the array, and whose sum is same as the given sum. Suppose the array is {4, 8, 63, 21, 24, 3, 6, 1, 0}, and the sum value is S = 18. So the triplets will be {4, 6, 8}. If more than one triplet is present, it will show all of them.AlgorithmgetTriplets(arr, n, sum) −Begin    define one array to store triplets, say trip_arr    define one set unique_trip to store unique triplets. ... Read More

Advantages of vector over array in C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:21:02

309 Views

Here we will see some advantaged and disadvantages of vector over array in C++.The vector is template class. This is C++ only constructs. The Arrays are built-in language construct. Arrays are present in different languages.Vectors are implemented as dynamic arrays with list interface, arrays can be implemented using static or dynamic way with primitive datatypes.Example#include #include using namespace std; int main() {    int array[10]; //statically allocated array    int* arr = new int[10]; //dynamically allocated array    vector vec; }Array sizes are fixed. When one array is created, we cannot change the size. The size of the vector is ... Read More

Addition and Subtraction of Matrix using pthreads in C/C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:19:09

705 Views

Here we will see how to perform the matrix addition and subtraction using multithreaded environment. The pthread is used to execute multiple threads simultaneously in C or C++.There are two matrices A and B. Order of each matrix is (m x n). Each thread will take each row, and perform addition or subtraction. So for m rows, there are m different threads.Example#include #include #include #include #define CORE 3 #define MAX 3 using namespace std; int AMat[MAX][MAX] = {{10, 20, 30},    {40, 50, 60},    {70, 80, 50} }; int BMat[MAX][MAX] = {{80, 60, 20},    {30, ... Read More

C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2

Arnab Chakraborty
Updated on 20-Aug-2019 07:15:14

113 Views

Here we will see how to get the sum of the series with n-th term as n2 – (n-1)2. The recurrence relation is like below −Tn = n2 − (n−1)2So the series is −We need to find S mod (109 + 7), where S is the sum of all terms of the given series.Example#include #define X 1000000007 using namespace std; long long getSum(long long n) {    return ((n % X) * (n % X)) % X; } int main() {    long long n = 56789;    cout

Hidden tricks of C++ related to STL

Arnab Chakraborty
Updated on 20-Aug-2019 07:07:31

178 Views

Here we will see some hidden tricks of C++ related to STL.Assign value of pairs using braces ‘{}’. We can use them to assign into tuples also.pair my_pair = make_pair(10, 20); pair my_pair2 = { 10, 20 }; //using braces pair my_pair3 = { 10, { 'A', 20 } }; //complex pairSometimes we do not remember to include lots of headers, or sometimes we forget the names of the headers, in that time we can follow this trick to include all headers.#include C++ has inbuilt GCD function. That function is not so popular so we do not know about it. ... Read More

C++: Methods of code shortening in competitive programming?

Arnab Chakraborty
Updated on 19-Aug-2019 14:35:42

297 Views

In this section we will see some examples of code shortening strategy for competitive programming. Suppose we have to write some large amount of codes. In that code, we can follow some strategy to make them more short.We can change the type-name to make it short. Please check the code to get the ideaExample Code#include using namespace std; int main() {    long long x = 10;    long long y = 50;    cout

A C/C++ Pointer Puzzle?

Arnab Chakraborty
Updated on 19-Aug-2019 14:15:22

234 Views

Suppose we have one integer variable whose size is 4 byte, another pointer variable is there, whose size is 8 bytes. So what will be the output of the following?Example#include using namespace std; main() {    int a[4][5][6];    int x = 0;    int* a1 = &x;    int** a2 = &a1;    int*** a3 = &a2;    cout

Advertisements