Found 1339 Articles for C

A backtracking approach to generate n bit Gray Codes ?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

494 Views

In this section we will see how we can generate the gray codes of n bits using backtracking approach? The n bit gray code is basically bit patterns from 0 to 2^n – 1 such that successive patterns differ by one bit. So for n = 2, the gray codes are (00, 01, 11, 10) and decimal equivalent is (0, 1, 3, 2). The program will generate the decimal equivalent of the gray code values.AlgorithmgenerateGray(arr, n, num)begin    if n = 0, then       insert num into arr       return    end if    generateGray(arr, n-1, ... Read More

What is a function specifier in C?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

654 Views

In C and C++ there are some function specifiers. The function specifiers are used to specify the functions property. C++ has inline function specifier. In C there is _Noreturn function specifier. This is used to denote that one function will not return anything.Example Live Demo#include int myAdd(int a, int b){    return a + b; } main() {    int x = 10, y = 20;    printf("The value is: %d", myAdd(x, y)); }OutputThe value is: 30If the _Noreturn is used it will display some warning and the program will be terminated with some error.Example#include _Noreturn int myAdd(int a, int b){ ... Read More

Generic keyword in C ?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

349 Views

As we know that the Macros are used in C or C++, but there is no facility for type checking. The macros can take any type of argument in it. The following example will show this case clearly.Example Live Demo#include #define INCREMENT(X) ++X main() {    int x = 5; float y = 2.56; char z = 'A';    printf("Integer Increment: %d", INCREMENT(x));    printf("Float Increment: %f", INCREMENT(y));    printf("Character Increment: %c", INCREMENT(z)); }OutputInteger Increment: 6 Float Increment: 3.560000 Character Increment: BThat is the problem of macro. In the later version of C, we can use macro by using ‘_Generic’ keyword. ... Read More

C Program for Egg Dropping Puzzle - DP-11?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

354 Views

This is a famous puzzle problem. Suppose there is a building with n floors, if we have m eggs, then how can we find the minimum number of drops needed to find a floor from which it is safe to drop an egg without breaking it.There some important points to remember −When an egg does not break from a given floor, then it will not break for any lower floor also.If an egg breaks from a given floor, then it will break for all upper floors.When an egg breaks, it must be discarded, otherwise we can use it again.Input - The ... Read More

C Program for Basic Euclidean algorithms?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

252 Views

Here we will see the Euclidean algorithm to find the GCD of two numbers. The GCD (Greatest Common Divisor) can easily be found using Euclidean algorithm. There are two different approach. One is iterative, another one is recursive. Here we are going to use the recursive Euclidean algorithm.AlgorithmEuclideanAlgorithm(a, b)begin    if a is 0, then       return b    end if    return gcd(b mod a, a) endExample Live Demo#include using namespace std; int euclideanAlgorithm(int a, int b) {    if (a == 0)       return b;    return euclideanAlgorithm(b%a, a); } main() {    int a, b;    cout > a >> b;    cout

C/C++ Program for Largest Sum Contiguous Subarray?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

458 Views

An array of integers is given. We have to find sum of all elements which are contiguous. Whose sum is largest, that will be sent as output.Using dynamic programming we will store the maximum sum up to current term. It will help to find sum for contiguous elements in the array.Input: An array of integers. {-2, -3, 4, -1, -2, 1, 5, -3} Output: Maximum Sum of the Subarray is : 7AlgorithmmaxSum(array, n)Input − The main array, the size of the array.Output − maximum sum.Begin    tempMax := array[0]    currentMax = tempMax    for i := 1 to n-1, ... Read More

C/C++ program to shutdown a system?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

777 Views

Here we will see how we can shut down the system by writing a simple C or C++ code. The shutdown process varies in different OS. If we are Linux user, we can use this terminal command to shut down.shutdown –P nowIf we are using Windows system, we can use this command −c:\windows\system32\shutdown /iWe will see the code for Linux and WindowsExample(Linux)#include using namespace std; int main() {    system("shutdown -P now"); }Example(Windows)#include using namespace std; int main() {    system("c:\windows\system32\shutdown /i "); }

C/C++ Program to Count set bits in an integer?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see how we can check number of set bits in an integer number. The set bits are 1’s in the binary representation of a number. For an example the number 13 has three set bits 1101. So the count will be 3.To solve this problem, we will shift the number to the right, and if the LSb is 1, then increase count. Until the number becomes 0, it will run.AlgorithmcountSetBit()begin    count := 0    while count is not 0, do       if LSb of n is set, then          count := ... Read More

C/C++ Program to Count Inversions in an array using Merge Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

865 Views

The inversions of an array indicate; how many changes are required to convert the array into its sorted form. When an array is already sorted, it needs 0 inversions, and in other case, the number of inversions will be maximum, if the array is reversed.To solve this problem, we will follow the Merge sort approach to reduce the time complexity, and make it in Divide and Conquer algorithm.InputA sequence of numbers. (1, 5, 6, 4, 20).OutputThe number of inversions required to arrange the numbers into ascending order.Here the number of inversions are 2. First inversion: (1, 5, 4, 6, 20) ... Read More

C/C++ Program for Triangular Matchstick Number?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

138 Views

Here we will see how to count number of matchsticks are required to make the pyramid-like below. The base of the pyramid is given. So if the base is 1, it will take 3 matchsticks to make a pyramid, for base 2, 9 matchsticks are needed, for base size 3, it will take 18 matchsticks.To solve this problem, we have to use this formula −Example Live Demo#include using namespace std; int main(){    int x;    cout > x;    int count = 3*x*(x+1)/2;    cout

Advertisements