codeparrot/xlcost-text-to-code huggingface dataset:
Search element in a sorted matrix | C ++ implementation to search an element in a sorted matrix ; This function does Binary search for x in i - th row . It does the search from mat [ i ] [ j_low ] to mat [ i ] [ j_high ] ; Element found ; element not found ; Function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found ; Single row matrix ; Do binary search in middle column . Condition to terminate the loop when the 2 desired rows are found ; element found ; If element is present on the mid of the two rows ; search element on 1 st half of 1 st row ; Search element on 2 nd half of 1 st row ; Search element on 1 st half of 2 nd row ; search element on 2 nd half of 2 nd row ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; void binarySearch ( int mat [ ] [ MAX ] , int i , int j_low , int j_high , int x ) { while ( j_low <= j_high ) { int j_mid = ( j_low + j_high ) / 2 ; if ( mat [ i ] [ j_mid ] == x ) { cout << " Found ▁ at ▁ ( " << i << " , ▁ " << j_mid << " ) " ; return ; } else if ( mat [ i ] [ j_mid ] > x ) j_high = j_mid - 1 ; else j_low = j_mid + 1 ; } cout << " Element ▁ no ▁ found " ; } void sortedMatrixSearch ( int mat [ ] [ MAX ] , int n , int m , int x ) { if ( n == 1 ) { binarySearch ( mat , 0 , 0 , m - 1 , x ) ; return ; } int i_low = 0 ; int i_high = n - 1 ; int j_mid = m / 2 ; while ( ( i_low + 1 ) < i_high ) { int i_mid = ( i_low + i_high ) / 2 ; if ( mat [ i_mid ] [ j_mid ] == x ) { cout << " Found ▁ at ▁ ( " << i_mid << " , ▁ " << j_mid << " ) " ; return ; } else if ( mat [ i_mid ] [ j_mid ] > x ) i_high = i_mid ; else i_low = i_mid ; } if ( mat [ i_low ] [ j_mid ] == x ) cout << " Found ▁ at ▁ ( " << i_low << " , " << j_mid << " ) " ; else if ( mat [ i_low + 1 ] [ j_mid ] == x ) cout << " Found ▁ at ▁ ( " << ( i_low + 1 ) << " , ▁ " << j_mid << " ) " ; else if ( x <= mat [ i_low ] [ j_mid - 1 ] ) binarySearch ( mat , i_low , 0 , j_mid - 1 , x ) ; else if ( x >= mat [ i_low ] [ j_mid + 1 ] && x <= mat [ i_low ] [ m - 1 ] ) binarySearch ( mat , i_low , j_mid + 1 , m - 1 , x ) ; else if ( x <= mat [ i_low + 1 ] [ j_mid - 1 ] ) binarySearch ( mat , i_low + 1 , 0 , j_mid - 1 , x ) ; else binarySearch ( mat , i_low + 1 , j_mid + 1 , m - 1 , x ) ; } int main ( ) { int n = 4 , m = 5 , x = 8 ; int mat [ ] [ MAX ] = { { 0 , 6 , 8 , 9 , 11 } , { 20 , 22 , 28 , 29 , 31 } , { 36 , 38 , 50 , 61 , 63 } , { 64 , 66 , 100 , 122 , 128 } } ; sortedMatrixSearch ( mat , n , m , x ) ; return 0 ; }
Minimum difference between adjacent elements of array which contain elements from each row of a matrix | C ++ program to find the minimum absolute difference between any of the adjacent elements of an array which is created by picking one element from each row of the matrix . ; Return smallest element greater than or equal to the current element . ; Return the minimum absolute difference adjacent elements of array ; Sort each row of the matrix . ; For each matrix element ; Search smallest element in the next row which is greater than or equal to the current element ; largest element which is smaller than the current element in the next row must be just before smallest element which is greater than or equal to the current element because rows are sorted . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define R  2 NEW_LINE #define C  2 NEW_LINE int bsearch ( int low , int high , int n , int arr [ ] ) { int mid = ( low + high ) / 2 ; if ( low <= high ) { if ( arr [ mid ] < n ) return bsearch ( mid + 1 , high , n , arr ) ; return bsearch ( low , mid - 1 , n , arr ) ; } return low ; } int mindiff ( int arr [ R ] [ C ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) sort ( arr [ i ] , arr [ i ] + m ) ; int ans = INT_MAX ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int p = bsearch ( 0 , m - 1 , arr [ i ] [ j ] , arr [ i + 1 ] ) ; ans = min ( ans , abs ( arr [ i + 1 ] [ p ] - arr [ i ] [ j ] ) ) ; if ( p - 1 >= 0 ) ans = min ( ans , abs ( arr [ i + 1 ] [ p - 1 ] - arr [ i ] [ j ] ) ) ; } } return ans ; } int main ( ) { int m [ R ] [ C ] = { 8 , 5 , 6 , 8 , } ; cout << mindiff ( m , R , C ) << endl ; return 0 ; }
Find bitonic point in given bitonic sequence | C ++ program to find bitonic point in a bitonic array . ; Function to find bitonic point using binary search ; base condition to check if arr [ mid ] is bitonic point or not ; We assume that sequence is bitonic . We go to right subarray if middle point is part of increasing subsequence . Else we go to left subarray . ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binarySearch ( int arr [ ] , int left , int right ) { if ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr [ mid - 1 ] < arr [ mid ] && arr [ mid ] > arr [ mid + 1 ] ) return mid ; if ( arr [ mid ] < arr [ mid + 1 ] ) return binarySearch ( arr , mid + 1 , right ) ; else return binarySearch ( arr , left , mid - 1 ) ; } return -1 ; } int main ( ) { int arr [ ] = { 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int index = binarySearch ( arr , 1 , n - 2 ) ; if ( index != -1 ) cout << arr [ index ] ; return 0 ; }
Find the only repeating element in a sorted array of size n | C ++ program to find the only repeating element in an array of size n and elements from range 1 to n - 1. ; Returns index of second appearance of a repeating element The function assumes that array elements are in range from 1 to n - 1. ; low = 0 , high = n - 1 ; ; Check if the mid element is the repeating one ; If mid element is not at its position that means the repeated element is in left ; If mid is at proper position then repeated one is in right . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findRepeatingElement ( int arr [ ] , int low , int high ) { if ( low > high ) return -1 ; int mid = low + ( high - low ) / 2 ; if ( arr [ mid ] != mid + 1 ) { if ( mid > 0 && arr [ mid ] == arr [ mid - 1 ] ) return mid ; return findRepeatingElement ( arr , low , mid - 1 ) ; } return findRepeatingElement ( arr , mid + 1 , high ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int index = findRepeatingElement ( arr , 0 , n - 1 ) ; if ( index != -1 ) cout << arr [ index ] ; return 0 ; }
Find cubic root of a number | C ++ program to find cubic root of a number using Binary Search ; Returns the absolute value of n - mid * mid * mid ; Returns cube root of a no n ; Set start and end for binary search ; Set precision ; If error is less than e then mid is our answer so return mid ; If mid * mid * mid is greater than n set end = mid ; If mid * mid * mid is less than n set start = mid ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double diff ( double n , double mid ) { if ( n > ( mid * mid * mid ) ) return ( n - ( mid * mid * mid ) ) ; else return ( ( mid * mid * mid ) - n ) ; } double cubicRoot ( double n ) { double start = 0 , end = n ; double e = 0.0000001 ; while ( true ) { double mid = ( start + end ) / 2 ; double error = diff ( n , mid ) ; if ( error <= e ) return mid ; if ( ( mid * mid * mid ) > n ) end = mid ; else start = mid ; } } int main ( ) { double n = 3 ; printf ( " Cubic ▁ root ▁ of ▁ % lf ▁ is ▁ % lf STRNEWLINE " , n , cubicRoot ( n ) ) ; return 0 ; }
Find frequency of each element in a limited range array in less than O ( n ) time | C ++ program to count number of occurrences of each element in the array in less than O ( n ) time ; A recursive function to count number of occurrences for each element in the array without traversing the whole array ; If element at index low is equal to element at index high in the array ; increment the frequency of the element by count of elements between high and low ; Find mid and recurse for left and right subarray ; A wrapper over recursive function findFrequencyUtil ( ) . It print number of occurrences of each element in the array . ; create a empty vector to store frequencies and initialize it by 0. Size of vector is maximum value ( which is last value in sorted array ) plus 1. ; Fill the vector with frequency ; Print the frequencies ; Driver function
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; void findFrequencyUtil ( int arr [ ] , int low , int high , vector < int > & freq ) { if ( arr [ low ] == arr [ high ] ) { freq [ arr [ low ] ] += high - low + 1 ; } else { int mid = ( low + high ) / 2 ; findFrequencyUtil ( arr , low , mid , freq ) ; findFrequencyUtil ( arr , mid + 1 , high , freq ) ; } } void findFrequency ( int arr [ ] , int n ) { vector < int > freq ( arr [ n - 1 ] + 1 , 0 ) ; findFrequencyUtil ( arr , 0 , n - 1 , freq ) ; for ( int i = 0 ; i <= arr [ n - 1 ] ; i ++ ) if ( freq [ i ] != 0 ) cout << " Element ▁ " << i << " ▁ occurs ▁ " << freq [ i ] << " ▁ times " << endl ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findFrequency ( arr , n ) ; return 0 ; }
Square root of an integer | A C ++ program to find floor ( sqrt ( x ) ; Returns floor of square root of x ; Base cases ; Starting from 1 , try all numbers until i * i is greater than or equal to x . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int floorSqrt ( int x ) { if ( x == 0 x == 1 ) return x ; int i = 1 , result = 1 ; while ( result <= x ) { i ++ ; result = i * i ; } return i - 1 ; } int main ( ) { int x = 11 ; cout << floorSqrt ( x ) << endl ; return 0 ; }
Maximum number of overlapping rectangles with at least one common point | C ++ program for the above approach ; Function to find the maximum number of overlapping rectangles ; Stores the maximum count of overlapping rectangles ; Stores the X and Y coordinates ; Iterate over all pairs of Xs and Ys ; Store the count for the current X and Y ; Update the maximum count of rectangles ; Returns the total count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxOverlappingRectangles ( int x1 [ ] , int y1 [ ] , int x2 [ ] , int y2 [ ] , int N ) { int max_rectangles = 0 ; vector < int > X , Y ; for ( int i = 0 ; i < N ; i ++ ) { X . push_back ( x1 [ i ] ) ; X . push_back ( x2 [ i ] - 1 ) ; Y . push_back ( y1 [ i ] ) ; Y . push_back ( y2 [ i ] - 1 ) ; } for ( int i = 0 ; i < X . size ( ) ; i ++ ) { for ( int j = 0 ; j < Y . size ( ) ; j ++ ) { int cnt = 0 ; for ( int k = 0 ; k < N ; k ++ ) { if ( X [ i ] >= x1 [ k ] && X [ i ] + 1 <= x2 [ k ] && Y [ j ] >= y1 [ k ] && Y [ j ] + 1 <= y2 [ k ] ) { cnt ++ ; } } max_rectangles = max ( max_rectangles , cnt ) ; } } cout << max_rectangles ; } int main ( ) { int x1 [ ] = { 0 , 50 } ; int y1 [ ] = { 0 , 50 } ; int x2 [ ] = { 100 , 60 } ; int y2 [ ] = { 100 , 60 } ; int N = sizeof ( x1 ) / sizeof ( x1 [ 0 ] ) ; maxOverlappingRectangles ( x1 , y1 , x2 , y2 , N ) ; return 0 ; }
Check if it is possible to reach the point ( X , Y ) using distances given in an array | C ++ program for the above approach ; Function to check if the point ( X , Y ) is reachable from ( 0 , 0 ) or not ; Find the Euclidian Distance ; Calculate the maximum distance ; Case 1. ; Case 2. ; Otherwise , check for the polygon condition for each side ; Otherwise , print Yes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPossibleToReach ( int A [ ] , int N , int X , int Y ) { double distance = sqrt ( double ( X * X + Y * Y ) ) ; double mx = 0 ; for ( int i = 0 ; i < N ; i ++ ) { mx += double ( A [ i ] ) ; } if ( mx < distance ) { cout << " NO " ; return 0 ; } if ( ( mx - distance ) < 0.000001 ) { cout << " YES " ; return 0 ; } for ( int i = 0 ; i < N ; i ++ ) { if ( distance + mx < double ( 2 ) * double ( A [ i ] ) ) { cout << " No " ; return 0 ; } } cout << " Yes " ; return 0 ; } int main ( ) { int A [ ] = { 2 , 5 } ; int X = 5 , Y = 4 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; isPossibleToReach ( A , N , X , Y ) ; return 0 ; }
Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | C ++ program for the above approach ; Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Termination Condition ; Otherwise , recursively call by decrementing 3 ^ i at each step ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canReach ( int X , int Y , int steps ) { if ( X == 0 && Y == 0 ) { return true ; } if ( X < 0 Y < 0 ) { return false ; } return ( canReach ( X - ( int ) pow ( 3 , steps ) , Y , steps + 1 ) | canReach ( X , Y - ( int ) pow ( 3 , steps ) , steps + 1 ) ) ; } int main ( ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y , 0 ) ) { cout << " YES " << endl ; } else cout << " NO " << endl ; return 0 ; }
Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | C ++ program for the above approach ; Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Stores the number of steps performed to reach ( X , Y ) ; Value of X in base 3 ; Value of Y in base 3 ; Check if any has value 2 ; If both have value 1 ; If both have value 0 ; Otherwise , return true ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canReach ( int X , int Y ) { int steps = 0 ; while ( X Y ) { int pos1 = X % 3 ; int pos2 = Y % 3 ; if ( pos1 == 2 pos2 == 2 ) { return false ; } if ( pos1 == 1 && pos2 == 1 ) { return false ; } if ( pos1 == 0 && pos2 == 0 ) { return false ; } X /= 3 ; Y /= 3 ; steps ++ ; } return true ; } int main ( ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y ) ) { cout << " YES " ; } else { cout << " NO " ; } }
Program to calculate Surface Area of Ellipsoid | C ++ program for the above approach ; Function to find the surface area of the given Ellipsoid ; Formula to find surface area of an Ellipsoid ; Print the area ; Driver Code
#include <iomanip> NEW_LINE #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void findArea ( double a , double b , double c ) { double area = 4 * 3.141592653 * pow ( ( pow ( a * b , 1.6 ) + pow ( a * c , 1.6 ) + pow ( b * c , 1.6 ) ) / 3 , 1 / 1.6 ) ; cout << fixed << setprecision ( 2 ) << area ; } int main ( ) { double A = 11 , B = 12 , C = 13 ; findArea ( A , B , C ) ; return 0 ; }
Descartes ' Circle Theorem with implementation | C ++ implementation of the above formulae ; Function to find the fourth circle 's when three radius are given ; Driver code ; Radius of three circles ; Calculation of r4 using formula given above
#include <bits/stdc++.h> NEW_LINE using namespace std ; double findRadius ( double r1 , double r2 , double r3 ) { double r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) ; return r4 ; } int main ( ) { double r1 = 1 ; double r2 = 1 ; double r3 = 1 ; double r4 = findRadius ( r1 , r2 , r3 ) ; cout << " The ▁ radius ▁ of ▁ fourth ▁ circle : ▁ " << r4 ; return 0 ; }
Make N pairs from Array as ( X , Y ) coordinate point that are enclosed inside a minimum area rectangle | C ++ program for tha above approach ; Function to make N pairs of coordinates such that they are enclosed in a minimum area rectangle with sides parallel to the X and Y axes ; A variable to store the answer ; For the case where the maximum and minimum are in different partitions ; For the case where the maximum and minimum are in the same partition ; Return the answer ; Driver code ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumRectangleArea ( int A [ ] , int N ) { int ans ; sort ( A , A + 2 * N ) ; ans = ( A [ N - 1 ] - A [ 0 ] ) * ( A [ 2 * N - 1 ] - A [ N ] ) ; for ( int i = 1 ; i < N ; i ++ ) ans = min ( ans , ( A [ 2 * N - 1 ] - A [ 0 ] ) * ( A [ i + N - 1 ] - A [ i ] ) ) ; return ans ; } int main ( ) { int A [ ] = { 2 , 4 , 1 , 5 , 3 , 6 , 7 , 8 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; N /= 2 ; cout << minimumRectangleArea ( A , N ) << endl ; return 0 ; }
Program to find the length of Latus Rectum of a Hyperbola | C ++ program for the above approach ; Function to calculate the length of the latus rectum of a hyperbola ; Store the length of major axis ; Store the length of minor axis ; Store the length of the latus rectum ; Return the length of the latus rectum ; Driver Code
#include <iostream> NEW_LINE using namespace std ; double lengthOfLatusRectum ( double A , double B ) { double major = 2.0 * A ; double minor = 2.0 * B ; double latus_rectum = ( minor * minor ) / major ; return latus_rectum ; } int main ( ) { double A = 3.0 , B = 2.0 ; cout << lengthOfLatusRectum ( A , B ) ; return 0 ; }
Length of intercept cut off from a line by a Circle | C ++ program for the above approach ; Function to find the radius of a circle ; g and f are the coordinates of the center ; Case of invalid circle ; Apply the radius formula ; Function to find the perpendicular distance between circle center and the line ; Store the coordinates of center ; Stores the perpendicular distance between the line and the point ; Invalid Case ; Return the distance ; Function to find the length of intercept cut off from a line by a circle ; Calculate the value of radius ; Calculate the perpendicular distance between line and center ; Invalid Case ; If line do not cut circle ; Print the intercept length ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double radius ( int a , int b , int c ) { int g = a / 2 ; int f = b / 2 ; if ( g * g + f * f - c < 0 ) return ( -1 ) ; return ( sqrt ( g * g + f * f - c ) ) ; } double centerDistanceFromLine ( int a , int b , int i , int j , int k ) { int g = a / 2 ; int f = b / 2 ; double distance = fabs ( i * g + j * f + k ) / ( sqrt ( i * i + j * j ) ) ; if ( distance < 0 ) return ( -1 ) ; return distance ; } void interceptLength ( int a , int b , int c , int i , int j , int k ) { double rad = radius ( a , b , c ) ; double dist = centerDistanceFromLine ( a , b , i , j , k ) ; if ( rad < 0 dist < 0 ) { cout << " circle ▁ not ▁ possible " ; return ; } if ( dist > rad ) { cout << " Line ▁ not ▁ cutting ▁ circle " ; } else cout << 2 * sqrt ( rad * rad - dist * dist ) ; } int main ( ) { int a = 0 , b = 0 , c = -4 ; int i = 2 , j = -1 , k = 1 ; interceptLength ( a , b , c , i , j , k ) ; return 0 ; }
Determine position of two points with respect to a 3D plane | C ++ program for the above approach ; Function to check position of two points with respect to a plane in 3D ; Put coordinates in plane equation ; If both values have same sign ; If both values have different sign ; If both values are zero ; If either of the two values is zero ; Driver Code ; Given Input ; Coordinates of points ; Function Call
#include <iostream> NEW_LINE using namespace std ; void check_position ( int a , int b , int c , int d , int x1 , int y1 , int z1 , int x2 , int y2 , int z2 ) { int value_1 = a * x1 + b * y1 + c * z1 + d ; int value_2 = a * x2 + b * y2 + c * z2 + d ; if ( ( value_1 > 0 && value_2 > 0 ) || ( value_1 < 0 && value_2 < 0 ) ) cout << " On ▁ same ▁ side " ; if ( ( value_1 > 0 && value_2 < 0 ) || ( value_1 < 0 && value_2 > 0 ) ) cout << " On ▁ different ▁ sides " ; if ( value_1 == 0 && value_2 == 0 ) cout << " Both ▁ on ▁ the ▁ plane " ; if ( value_1 == 0 && value_2 != 0 ) cout << " Point ▁ 1 ▁ on ▁ the ▁ plane " ; if ( value_1 != 0 && value_2 == 0 ) cout << " Point ▁ 2 ▁ on ▁ the ▁ plane " ; } int main ( ) { int a = 1 , b = 2 , c = 3 , d = 4 ; int x1 = -2 , y1 = -2 , z1 = 1 ; int x2 = -4 , y2 = 11 , z2 = -1 ; check_position ( a , b , c , d , x1 , y1 , z1 , x2 , y2 , z2 ) ; return 0 ; }
Check if any pair of semicircles intersect or not | C ++ program for the above approach ; Function to check if any pairs of the semicircles intersects or not ; Stores the coordinates of all the semicircles ; x and y are coordinates ; Store the minimum and maximum value of the pair ; Push the pair in vector ; Compare one pair with other pairs ; Generating the second pair ; Extract all the second pairs one by one ; 1 st condition ; 2 nd condition ; If any one condition is true ; If any pair of semicircles doesn 't exists ; Driver Code
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; bool checkIntersection ( int arr [ ] , int N ) { vector < pair < int , int > > vec ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int x = arr [ i ] , y = arr [ i + 1 ] ; int minn = min ( x , y ) ; int maxx = max ( x , y ) ; vec . push_back ( { minn , maxx } ) ; } for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { pair < int , int > x = vec [ i ] ; for ( int j = 0 ; j < vec . size ( ) ; j ++ ) { pair < int , int > y = vec [ j ] ; bool cond1 = ( x . first < y . first and x . second < y . second and y . first < x . second ) ; bool cond2 = ( y . first < x . first and y . second < x . second and x . first < y . second ) ; if ( cond1 or cond2 ) { return true ; } } } return false ; } int main ( ) { int arr [ ] = { 0 , 15 , 5 , 10 } ; int N = sizeof ( arr ) / sizeof ( int ) ; if ( checkIntersection ( arr , N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the angle between tangents drawn from a given external point to a Circle | C ++ program for the above approach ; Function to find the distance between center and the exterior point ; Find the difference between the x and y coordinates ; Using the distance formula ; Function to find the angle between the pair of tangents drawn from the point ( X2 , Y2 ) to the circle . ; Calculate the distance between the center and exterior point ; Invalid Case ; Find the angle using the formula ; Print the resultant angle ; Driver Code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; double point_distance ( int x1 , int y1 , int x2 , int y2 ) { int p = ( x2 - x1 ) ; int q = ( y2 - y1 ) ; double distance = sqrt ( p * p + q * q ) ; return distance ; } void tangentAngle ( int x1 , int y1 , int x2 , int y2 , double radius ) { double distance = point_distance ( x1 , y1 , x2 , y2 ) ; if ( radius / distance > 1 radius / distance < -1 ) { cout << -1 ; } double result = 2 * asin ( radius / distance ) * 180 / 3.1415 ; cout << result << " ▁ degrees " ; } int main ( ) { int radius = 4 ; int x1 = 7 , y1 = 12 ; int x2 = 3 , y2 = 4 ; tangentAngle ( x1 , y1 , x2 , y2 , radius ) ; return 0 ; }
Ratio of area of two nested polygons formed by connecting midpoints of sides of a regular N | C ++ code for the above approach ; Function to calculate the ratio of area of N - th and ( N + 1 ) - th nested polygons formed by connecting midpoints ; Stores the value of PI ; Calculating area the factor ; Printing the ratio precise upto 6 decimal places ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void AreaFactor ( int n ) { double pi = 3.14159265 ; double areaf = 1 / ( cos ( pi / n ) * cos ( pi / n ) ) ; cout << fixed << setprecision ( 6 ) << areaf << endl ; } int main ( ) { int n = 4 ; AreaFactor ( n ) ; return 0 ; }
Find the length of Kth N | C ++ program for the above approach ; Function to calculate the interior angle of a N - sided regular polygon ; Function to find the K - th polygon formed inside the ( K - 1 ) th polygon ; Stores the interior angle ; Stores the side length of K - th regular polygon ; Return the length ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define PI  3.14159265 NEW_LINE double findInteriorAngle ( int n ) { return ( n - 2 ) * PI / n ; } double calculateSideLength ( double L , int N , int K ) { double angle = findInteriorAngle ( N ) ; double length = L * pow ( sin ( angle / 2 ) , ( K - 1 ) ) ; return length ; } int main ( ) { double N = 5 , L = 21 , K = 7 ; cout << calculateSideLength ( L , N , K ) ; return 0 ; }