Found 1401 Articles for C

C/C++ program to shutdown a system?

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

517 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 Find the Number Occurring Odd Number of Times?

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

104 Views

In this program we will see how we can get a number that is occurring odd number of times in an array. There are many different approaches. One of the easiest approach is performing ZOR operation. If a number is XORed with itself, it will be 0. So if a number XORed even number of times, it will be 0, otherwise the number itself.This solution has one problem, if more than one element has odd number of occurrences, it will return one of them.AlgorithmgetNumOccurredOdd(arr, n)begin    res := 0    for each element e from arr, do       ... Read More

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

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

899 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

636 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

87 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

C/C++ Program for Maximum height when coins are arranged in a triangle?

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

525 Views

In this section, we will see one interesting problem. There are N coins. we have to find what is the max height we can make if we arrange the coins as pyramid. In this fashion, the first row will hold 1 coin, second will hold 2 coins and so on.In the given diagram, we can see to make a pyramid of height three we need minimum 6 coins. We cannot make height 4 until we have 10 coins. Now let us see how to check the maximum height.We can get the height by using this formula.Example Live Demo#include #include using namespace ... Read More

c32rtomb() function in C/C++?

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

92 Views

In C++, we can use 32-bit character representations. The c32rtomb() function is used to convert 32-bit character representation to narrow multi-byte character representation. We can find this function inside the uchar.h header file.This function takes three parameters. These are −The string where multi-byte character will be stored32-bit character to convertThe pointer of type mbstate_t object. which is used to interpret multibyte string.This function returns number of bytes written to the character array, when it is successful, otherwise returns -1. Let us see an example to get better idea.Example Live Demo#include #include #include using namespace std; int main() { ... Read More

c16rtomb() function in C/C++?

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

128 Views

In C++, we can use 16-bit character representations. The c16rtomb() function is used to convert 16-bit character representation to narrow multi-byte character representation. We can find this function inside the uchar.h header file.This function takes three parameters. These are −The string where multi-byte character will be stored16-bit character to convertThe pointer of type mbstate_t object. which is used to interpret multibyte string.This function returns number of bytes written to the character array, when it is successful, otherwise returns -1. Let us see an example to get better idea.Example Live Demo#include #include #include using namespace std; int main() { ... Read More

A data structure for n elements and O(1) operations?

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

427 Views

Here we will see one data-structure with n elements, and O(1) operations. So the operations will take constant amount of time to execute.The data structure will hold n elements (from 0 to n-1). The data can be in any order. The Insertion, deletion and searching will take O(1) amount of time.To solve this problem, we will use one Boolean array. This will indicate that the item is present or not at position i. If the item is present, it will hold 1, otherwise 0.Algorithminitialization(n)begin    fill all elements of the Boolean array as 0 endinsert(i)begin    set element at index ... Read More

A C/C++ Function Call Puzzle?

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

175 Views

We know that C and C++ are very much similar in different aspects. The C++ has additional object oriented feature in it, but most of the C programs can also be correct in C++. Here we will see one program related to function call, that can be run when it is written in C, but will not work in C++.Example Live Demo#include void myFunction() {    printf("Function called"); } int main() {    myFunction();    myFunction(2); }OutputFunction called Function calledThis program will run in C and generates output, but when we want to compile in C++, it will return an error ... Read More

Advertisements