codeparrot/xlcost-text-to-code huggingface dataset:
Minimum operations required to make all the elements distinct in an array | C ++ program to find Minimum number of changes to make array distinct ; Function that returns minimum number of changes ; Hash - table to store frequency ; Increase the frequency of elements ; Traverse in the map to sum up the ( occurrences - 1 ) of duplicate elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumOperations ( int a [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ a [ i ] ] += 1 ; int count = 0 ; for ( auto it = mp . begin ( ) ; it != mp . end ( ) ; it ++ ) { if ( ( * it ) . second > 1 ) count += ( * it ) . second - 1 ; } return count ; } int main ( ) { int a [ ] = { 2 , 1 , 2 , 3 , 3 , 4 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << minimumOperations ( a , n ) ; return 0 ; }
Check if a M | C ++ program to check if M - th fibonacci divides N - th fibonacci ; exceptional case for F ( 2 ) ; if none of the above cases , hence not divisible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( int n , int m ) { if ( n == 2 m == 2 n % m == 0 ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } } int main ( ) { int m = 3 , n = 9 ; check ( n , m ) ; return 0 ; }
Surface Area and Volume of Hexagonal Prism | C ++ program to find the Surface Area and Volume of Hexagonal Prism . ; Function to calculate Surface area ; Formula to calculate surface area ; Display surface area ; Function to calculate Volume ; formula to calculate Volume ; Display Volume ; Driver Code ; surface area function call ; volume function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSurfaceArea ( float a , float h ) { float Area ; Area = 6 * a * h + 3 * sqrt ( 3 ) * a * a ; cout << " Surface ▁ Area : ▁ " << Area ; cout << " STRNEWLINE " ; } void findVolume ( float a , float h ) { float Volume ; Volume = 3 * sqrt ( 3 ) * a * a * h / 2 ; cout << " Volume : ▁ " << Volume ; } int main ( ) { float a = 5 , h = 10 ; findSurfaceArea ( a , h ) ; findVolume ( a , h ) ; return 0 ; }
Minimum number of mails required to distribute all the questions | C ++ code to find the minimum number of mails ; Function returns the min no of mails required ; Using the formula derived above ; Driver Code ; no of questions ; no of students ; maximum no of questions a mail can hold ; Calling function
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; long long int MinimumMail ( int n , int k , int x ) { ll m = ( n - 1 ) + ( ll ) ceil ( ( n - 1 ) * 1.0 / x ) * ( n - 1 ) + ( ll ) ceil ( n * 1.0 / x ) * ( k - n ) ; return m ; } int main ( ) { int N = 4 ; int K = 9 ; int X = 2 ; cout << MinimumMail ( N , K , X ) << endl ; return 0 ; }
Program to find the Area of an Ellipse | C ++ program to find area of an Ellipse . ; Function to find area of an ellipse . ; formula to find the area of an Ellipse . ; Display the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findArea ( float a , float b ) { float Area ; Area = 3.142 * a * b ; cout << " Area : ▁ " << Area ; } int main ( ) { float a = 5 , b = 4 ; findArea ( a , b ) ; return 0 ; }
Compute power of power k times % m | C ++ program for computing x ^ x ^ x ^ x . . % m ; Function to compute the given value ; compute power k times ; Driver Code ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculate ( int x , int k , int m ) { int result = x ; k -- ; while ( k -- ) { result = pow ( result , x ) ; if ( result > m ) result %= m ; } return result ; } int main ( ) { int x = 5 , k = 2 , m = 3 ; cout << calculate ( x , k , m ) ; return 0 ; }
Recursive program to check if number is palindrome or not | Recursive C ++ program to check if the number is palindrome or not ; recursive function that returns the reverse of digits ; base case ; stores the reverse of a number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int rev ( int n , int temp ) { if ( n == 0 ) return temp ; temp = ( temp * 10 ) + ( n % 10 ) ; return rev ( n / 10 , temp ) ; } int main ( ) { int n = 121 ; int temp = rev ( n , 0 ) ; if ( temp == n ) cout << " yes " << endl ; else cout << " no " << endl ; return 0 ; }
Program to find greater value between a ^ n and b ^ n | C ++ code for finding greater between the a ^ n and b ^ n ; Function to find the greater value ; If n is even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findGreater ( int a , int b , int n ) { if ( ! ( n & 1 ) ) { a = abs ( a ) ; b = abs ( b ) ; } if ( a == b ) cout << " a ^ n ▁ is ▁ equal ▁ to ▁ b ^ n " ; else if ( a > b ) cout << " a ^ n ▁ is ▁ greater ▁ than ▁ b ^ n " ; else cout << " b ^ n ▁ is ▁ greater ▁ than ▁ a ^ n " ; } int main ( ) { int a = 12 , b = 24 , n = 5 ; findGreater ( a , b , n ) ; return 0 ; }
Print first n Fibonacci Numbers using direct formula | C ++ code to print fibonacci numbers till n using direct formula . ; Function to calculate fibonacci using recurrence relation formula ; Using direct formula ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void fibonacci ( int n ) { long long int fib ; for ( long long int i = 0 ; i < n ; i ++ ) { fib = ( pow ( ( 1 + sqrt ( 5 ) ) , i ) - pow ( ( 1 - sqrt ( 5 ) ) , i ) ) / ( pow ( 2 , i ) * sqrt ( 5 ) ) ; cout << fib << " ▁ " ; } } int main ( ) { long long int n = 8 ; fibonacci ( n ) ; return 0 ; }
Centered Hexadecagonal Number | C ++ Program to find nth centered hexadecagonal number ; centered hexadecagonal function ; Formula to calculate nth centered hexadecagonal number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int center_hexadecagonal_num ( long int n ) { return 8 * n * n - 8 * n + 1 ; } int main ( ) { long int n = 2 ; cout << n << " th ▁ centered ▁ hexadecagonal ▁ number ▁ : ▁ " << center_hexadecagonal_num ( n ) ; cout << endl ; n = 12 ; cout << n << " th ▁ centered ▁ hexadecagonal ▁ numbe ▁ : ▁ " << center_hexadecagonal_num ( n ) ; return 0 ; }
Check if the n | CPP Program to check if the nth is odd or even in a sequence where each term is sum of previous two term ; Return if the nth term is even or odd . ; Return true if odd ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE bool findNature ( int a , int b , int n ) { int seq [ MAX ] = { 0 } ; seq [ 0 ] = a ; seq [ 1 ] = b ; for ( int i = 2 ; i <= n ; i ++ ) seq [ i ] = seq [ i - 1 ] + seq [ i - 2 ] ; return ( seq [ n ] & 1 ) ; } int main ( ) { int a = 2 , b = 4 ; int n = 3 ; ( findNature ( a , b , n ) ? ( cout << " Odd " << " ▁ " ) : ( cout << " Even " << " ▁ " ) ) ; return 0 ; }
Program to compare m ^ n and n ^ m | CPP program to compare which is greater m ^ n or n ^ m ; function to compare m ^ n and n ^ m ; m ^ n ; n ^ m ; Drivers Code ; function call to compare m ^ n and n ^ m
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( unsigned long long m , unsigned long long int n ) { double RHS = m * ( double ) log ( n ) ; double LHS = n * ( double ) log ( m ) ; if ( LHS > RHS ) cout << " m ^ n ▁ > ▁ n ^ m " ; else if ( LHS < RHS ) cout << " m ^ n ▁ < ▁ n ^ m " ; else cout << " m ^ n ▁ = ▁ n ^ m " ; } int main ( ) { unsigned long long m = 987654321 , n = 123456987 ; check ( m , n ) ; return 0 ; }
Hilbert Matrix | C ++ program for Hilbert Matrix ; Function that generates a Hilbert matrix ; using the formula to generate hilbert matrix ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMatrix ( int n ) { float H [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { H [ i ] [ j ] = ( float ) 1.0 / ( ( i + 1 ) + ( j + 1 ) - 1.0 ) ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) cout << H [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int n = 3 ; printMatrix ( n ) ; return 0 ; }
Find the GCD that lies in given range | CPP Program to find the Greatest Common divisor of two number which is in given range ; Return the greatest common divisor of two numbers ; Return the gretest common divisor of a and b which lie in the given range . ; Loop from 1 to sqrt ( GCD ( a , b ) . ; if i divides the GCD ( a , b ) , then find maximum of three numbers res , i and g / i ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int maxDivisorRange ( int a , int b , int l , int h ) { int g = gcd ( a , b ) ; int res = -1 ; for ( int i = l ; i * i <= g && i <= h ; i ++ ) if ( g % i == 0 ) res = max ( { res , i , g / i } ) ; return res ; } int main ( ) { int a = 3 , b = 27 , l = 1 , h = 5 ; cout << maxDivisorRange ( a , b , l , h ) << endl ; return 0 ; }
Number expressed as sum of five consecutive integers | CPP Program to check if a number can be expressed as sum of five consecutive integers . ; function to check if a number can be expressed as sum of five consecutive integers . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 4 ; check if sum of five consecutive integer is equal to n . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checksum ( int n ) { if ( n == 0 ) { cout << " - 2 ▁ - 1 ▁ 0 ▁ 1 ▁ 2" << endl ; return ; } int inc ; if ( n > 0 ) inc = 1 ; else inc = -1 ; for ( int i = 0 ; i <= n - 4 ; i += inc ) { if ( i + i + 1 + i + 2 + i + 3 + i + 4 == n ) { cout << i << " ▁ " << i + 1 << " ▁ " << i + 2 << " ▁ " << i + 3 << " ▁ " << i + 4 ; return ; } } cout << " - 1" ; } int main ( ) { int n = 15 ; checksum ( n ) ; return 0 ; }
Number expressed as sum of five consecutive integers | CPP Program to check if a number can be expressed as sum of five consecutive integer . ; function to check if a number can be expressed as sum of five consecutive integers . ; if n is multiple of 5 ; else print " - 1" . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checksum ( int n ) { if ( n % 5 == 0 ) cout << n / 5 - 2 << " ▁ " << n / 5 - 1 << " ▁ " << n / 5 << " ▁ " << n / 5 + 1 << " ▁ " << n / 5 + 2 ; else cout << " - 1" ; } int main ( ) { int n = 15 ; checksum ( n ) ; return 0 ; }
Number of Transpositions in a Permutation | CPP Program to find the number of transpositions in a permutation ; This array stores which element goes to which position ; This function returns the size of a component cycle ; If it is already visited ; This functio returns the number of transpositions in the permutation ; Initializing visited [ ] array ; building the goesTo [ ] array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000001 NEW_LINE int visited [ N ] ; int goesTo [ N ] ; int dfs ( int i ) { if ( visited [ i ] == 1 ) return 0 ; visited [ i ] = 1 ; int x = dfs ( goesTo [ i ] ) ; return ( x + 1 ) ; } int noOfTranspositions ( int P [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) visited [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) goesTo [ P [ i ] ] = i + 1 ; int transpositions = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( visited [ i ] == 0 ) { int ans = dfs ( i ) ; transpositions += ans - 1 ; } } return transpositions ; } int main ( ) { int permutation [ ] = { 5 , 1 , 4 , 3 , 2 } ; int n = sizeof ( permutation ) / sizeof ( permutation [ 0 ] ) ; cout << noOfTranspositions ( permutation , n ) ; return 0 ; }
n | CPP program to find n - th term of series ; Function to find the nth term of series ; Loop to add 4 th powers ; Driver code
#include <iostream> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += i * i * i * i ; return ans ; } int main ( ) { int n = 4 ; cout << sumOfSeries ( n ) ; return 0 ; }
Number of unmarked integers in a special sieve | C ++ Program to determine the number of unmarked integers in a special sieve ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countUnmarked ( int N ) { if ( N % 2 == 0 ) return N / 2 ; else return N / 2 + 1 ; } int main ( ) { int N = 4 ; cout << " Number ▁ of ▁ unmarked ▁ elements : ▁ " << countUnmarked ( N ) << endl ; return 0 ; }
Sum of series 1 * 1 ! + 2 * 2 ! + …… . . + n * n ! | CPP program to find sum of the series . ; Function to calculate required series ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int calculateSeries ( int n ) { return factorial ( n + 1 ) - 1 ; } int main ( ) { int n = 3 ; cout << calculateSeries ( n ) ; return 0 ; }
Sum of series 1 * 1 * 2 ! + 2 * 2 * 3 ! + …… . . + n * n * ( n + 1 ) ! | CPP program to find sum of the series . ; Function to calculate required series ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int calculateSeries ( int n ) { return 2 + ( n * n + n - 2 ) * factorial ( n + 1 ) ; } int main ( ) { int n = 3 ; cout << calculateSeries ( n ) ; return 0 ; }
Aspiring Number | C ++ implementation to check whether a number is aspiring or not ; Function to calculate sum of all proper divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; calculate sum of all proper divisors only ; Function to get last number of Aliquot Sequence . ; Calculate next term from previous term ; Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Returns true if n is aspiring else returns false ; checking condition for aspiring ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } int getAliquot ( int n ) { unordered_set < int > s ; s . insert ( n ) ; int next = 0 ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . find ( n ) != s . end ( ) ) return n ; s . insert ( n ) ; } return 0 ; } bool isPerfect ( int n ) { long long int sum = 1 ; for ( long long int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) sum = sum + i + n / i ; if ( sum == n && n != 1 ) return true ; return false ; } bool isAspiring ( int n ) { int alq = getAliquot ( n ) ; if ( isPerfect ( alq ) && ! isPerfect ( n ) ) return true ; else return false ; } int main ( ) { int n = 25 ; if ( isAspiring ( n ) ) cout << " Aspiring " << endl ; else cout << " Not ▁ Aspiring " << endl ; return 0 ; }
Forming smallest array with given constraints | C ++ program to find the length of smallest array begin with x , having y , ends with z and having absolute difference between adjacent elements <= 1. ; Return the size of smallest array with given constraint . ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumLength ( int x , int y , int z ) { return 1 + abs ( x - y ) + abs ( y - z ) ; } int main ( ) { int x = 3 , y = 1 , z = 2 ; cout << minimumLength ( x , y , z ) ; return 0 ; }
Find the other | CPP program to find the other - end point of diameter ; function to find the other - end point of diameter ; find end point for x coordinates ; find end point for y coordinates ; Driven Program
#include <iostream> NEW_LINE using namespace std ; void endPointOfDiameterofCircle ( int x1 , int y1 , int c1 , int c2 ) { cout << " x2 ▁ = ▁ " << ( float ) ( 2 * c1 - x1 ) << " ▁ " ; cout << " y2 ▁ = ▁ " << ( float ) ( 2 * c2 - y1 ) ; } int main ( ) { int x1 = -4 , y1 = -1 ; int c1 = 3 , c2 = 5 ; endPointOfDiameterofCircle ( x1 , y1 , c1 , c2 ) ; return 0 ; }
Newton 's Divided Difference Interpolation Formula | CPP program for implementing Newton divided difference formula ; Function to find the product term ; Function for calculating divided difference table ; Function for applying Newton 's divided difference formula ; Function for displaying divided difference table ; Driver Function ; number of inputs given ; y [ ] [ ] is used for divided difference table where y [ ] [ 0 ] is used for input ; calculating divided difference table ; displaying divided difference table ; value to be interpolated ; printing the value
#include <bits/stdc++.h> NEW_LINE using namespace std ; float proterm ( int i , float value , float x [ ] ) { float pro = 1 ; for ( int j = 0 ; j < i ; j ++ ) { pro = pro * ( value - x [ j ] ) ; } return pro ; } void dividedDiffTable ( float x [ ] , float y [ ] [ 10 ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) { y [ j ] [ i ] = ( y [ j ] [ i - 1 ] - y [ j + 1 ] [ i - 1 ] ) / ( x [ j ] - x [ i + j ] ) ; } } } float applyFormula ( float value , float x [ ] , float y [ ] [ 10 ] , int n ) { float sum = y [ 0 ] [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum = sum + ( proterm ( i , value , x ) * y [ 0 ] [ i ] ) ; } return sum ; } void printDiffTable ( float y [ ] [ 10 ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) { cout << setprecision ( 4 ) << y [ i ] [ j ] << " TABSYMBOL ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { int n = 4 ; float value , sum , y [ 10 ] [ 10 ] ; float x [ ] = { 5 , 6 , 9 , 11 } ; y [ 0 ] [ 0 ] = 12 ; y [ 1 ] [ 0 ] = 13 ; y [ 2 ] [ 0 ] = 14 ; y [ 3 ] [ 0 ] = 16 ; dividedDiffTable ( x , y , n ) ; printDiffTable ( y , n ) ; value = 7 ; cout << " Value at " ▁ < < ▁ value ▁ < < ▁ " is " << applyFormula ( value , x , y , n ) << endl ; return 0 ; }
Centered heptagonal number | CPP program to find n - th Centered heptagonal number ; Function to find Centered heptagonal number ; Formula to calculate nth Centered heptagonal number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int centered_heptagonal_num ( long int n ) { return ( 7 * n * n - 7 * n + 2 ) / 2 ; } int main ( ) { long int n = 5 ; cout << n << " th ▁ Centered ▁ heptagonal ▁ number ▁ : ▁ " ; cout << centered_heptagonal_num ( n ) ; return 0 ; }
Sum of square | CPP Program to find the sum of sum of squares of first n natural number ; Function to find sum of sum of square of first n natural number ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( ( i * ( i + 1 ) * ( 2 * i + 1 ) ) / 6 ) ; return sum ; } int main ( ) { int n = 3 ; cout << findSum ( n ) << endl ; return 0 ; }
Check if a given matrix is Hankel or not | C ++ Program to check if given matrix is Hankel Matrix or not . ; Function to check if given matrix is Hankel Matrix or not . ; for each row ; for each column ; checking if i + j is less than n ; checking if the element is equal to the corresponding diagonal constant ; checking if the element is equal to the corresponding diagonal constant ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  4 NEW_LINE bool checkHankelMatrix ( int n , int m [ N ] [ N ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i + j < n ) { if ( m [ i ] [ j ] != m [ i + j ] [ 0 ] ) return false ; } else { if ( m [ i ] [ j ] != m [ i + j - n + 1 ] [ n - 1 ] ) return false ; } } } return true ; } int main ( ) { int n = 4 ; int m [ N ] [ N ] = { { 1 , 2 , 3 , 5 } , { 2 , 3 , 5 , 8 } , { 3 , 5 , 8 , 0 } , { 5 , 8 , 0 , 9 } } ; checkHankelMatrix ( n , m ) ? ( cout << " Yes " ) : ( cout << " No " ) ; return 0 ; }
Check if a number can be expressed as power | Set 2 ( Using Log ) | CPP program to find if a number can be expressed as x raised to power y . ; Find Log n in different bases and check if the value is an integer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( unsigned int n ) { for ( int x = 2 ; x <= sqrt ( n ) ; x ++ ) { float f = log ( n ) / log ( x ) ; if ( ( f - ( int ) f ) == 0.0 ) return true ; } return false ; } int main ( ) { for ( int i = 2 ; i < 100 ; i ++ ) if ( isPower ( i ) ) cout << i << " ▁ " ; return 0 ; }
Queries on sum of odd number digit sums of all the factors of a number | CPP Program to answer queries on sum of sum of odd number digits of all the factors of a number ; finding sum of odd digit number in each integer . ; for each number ; using previous number sum , finding the current number num of odd digit also , adding last digit if it is odd . ; finding sum of sum of odd digit of all the factors of a number . ; for each possible factor ; adding the contribution . ; Wrapper function ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000005 NEW_LINE void sumOddDigit ( int digitSum [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { digitSum [ i ] = digitSum [ i / 10 ] + ( i & 1 ) * ( i % 10 ) ; } } void sumFactor ( int digitSum [ ] , int factorDigitSum [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j += i ) { factorDigitSum [ j ] += digitSum [ i ] ; } } } void wrapper ( int q , int n [ ] ) { int digitSum [ N ] ; int factorDigitSum [ N ] ; sumOddDigit ( digitSum ) ; sumFactor ( digitSum , factorDigitSum ) ; for ( int i = 0 ; i < q ; i ++ ) cout << factorDigitSum [ n [ i ] ] << " ▁ " ; } int main ( ) { int q = 2 ; int n [ ] = { 10 , 36 } ; wrapper ( q , n ) ; return 0 ; }
Program for Gauss | C ++ Implementation for Gauss - Jordan Elimination Method ; Function to print the matrix ; function to reduce matrix to reduced row echelon form . ; Performing elementary operations ; Excluding all i == j ; Converting Matrix to reduced row echelon form ( diagonal matrix ) ; Function to print the desired result if unique solutions exists , otherwise prints no solution or infinite solutions depending upon the input given . ; Printing the solution by dividing constants by their respective diagonal elements ; To check whether infinite solutions exists or no solution exists ; flag == 2 for infinite solution flag == 3 for No solution ; Driver code ; Order of Matrix ( n ) ; Performing Matrix transformation ; Printing Final Matrix ; Printing Solutions ( if exist )
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  10 NEW_LINE void PrintMatrix ( float a [ ] [ M ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) cout << a [ i ] [ j ] << " ▁ " ; cout << endl ; } } int PerformOperation ( float a [ ] [ M ] , int n ) { int i , j , k = 0 , c , flag = 0 , m = 0 ; float pro = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] [ i ] == 0 ) { c = 1 ; while ( ( i + c ) < n && a [ i + c ] [ i ] == 0 ) c ++ ; if ( ( i + c ) == n ) { flag = 1 ; break ; } for ( j = i , k = 0 ; k <= n ; k ++ ) swap ( a [ j ] [ k ] , a [ j + c ] [ k ] ) ; } for ( j = 0 ; j < n ; j ++ ) { if ( i != j ) { float pro = a [ j ] [ i ] / a [ i ] [ i ] ; for ( k = 0 ; k <= n ; k ++ ) a [ j ] [ k ] = a [ j ] [ k ] - ( a [ i ] [ k ] ) * pro ; } } } return flag ; } void PrintResult ( float a [ ] [ M ] , int n , int flag ) { cout << " Result ▁ is ▁ : ▁ " ; if ( flag == 2 ) cout << " Infinite ▁ Solutions ▁ Exists " << endl ; else if ( flag == 3 ) cout << " No ▁ Solution ▁ Exists " << endl ; else { for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] [ n ] / a [ i ] [ i ] << " ▁ " ; } } int CheckConsistency ( float a [ ] [ M ] , int n , int flag ) { int i , j ; float sum ; flag = 3 ; for ( i = 0 ; i < n ; i ++ ) { sum = 0 ; for ( j = 0 ; j < n ; j ++ ) sum = sum + a [ i ] [ j ] ; if ( sum == a [ i ] [ j ] ) flag = 2 ; } return flag ; } int main ( ) { float a [ M ] [ M ] = { { 0 , 2 , 1 , 4 } , { 1 , 1 , 2 , 6 } , { 2 , 1 , 1 , 7 } } ; int n = 3 , flag = 0 ; flag = PerformOperation ( a , n ) ; if ( flag == 1 ) flag = CheckConsistency ( a , n , flag ) ; cout << " Final ▁ Augumented ▁ Matrix ▁ is ▁ : ▁ " << endl ; PrintMatrix ( a , n ) ; cout << endl ; PrintResult ( a , n , flag ) ; return 0 ; }