Found 7197 Articles for C++

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

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

867 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

139 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

621 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

145 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

194 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

C++ tricks for competitive programming (for C++ 11)?

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

303 Views

Here we will see some good tricks of C++ programming language that can help us in different area. Like if we want to participate in some competitive programming events, then these tricks will help us to reduce the time for writing codes. Let us see some of these examples one by one.Checking whether a number is odd or even without using % operator. This trick is simple. We can perform bitwise AND operation with the number and 1. If the result is non-zero then this is odd, otherwise this is even. The logic is too simple. All odd numbers have ... Read More

C++ program to read file word by word?

Arnab Chakraborty
Updated on 13-Aug-2019 10:28:27

5K+ Views

In this section we will see how we can read file content word by word using C++. The task is very simple. we have to use the file input stream to read file contents. The file stream will open the file by using file name, then using FileStream, load each word and store it into a variable called word. Then print each word one by one.Algorithmread_word_by_word(filename)begin    file = open file using filename    while file has new word, do       print the word into the console    done endFile Content (test_file.txt)This is a test file. There are ... Read More

C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!

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

2K+ Views

Here we will see how we can get the sum of the given series. The value of n will be given by user. We can solve this problem by making a factorial function, and get factorial in each step in the loop. But factorial calculation is costlier task than normal addition. We will use the previous factorial term in the next one. Like 3! is (3 * 2 * 1), and 4! is 4 * 3!. So if we store 3! into some variable, we can use that and add the next number only to get the next factorial easily.Algorithmsum_series_fact(n)begin ... Read More

C++ Program to count Vowels in a string using Pointer?

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

579 Views

To get the vowels from a string, we have to iterate through each character of the string. Here we have to use pointers to move through the string. For this we need C style strings. If the string is pointed by str, then *str will hold the first character at the beginning. Then if str is increased, the *str will point next character and so on. If the character is in [a, e, i, o, u] or [A, E, I, O, U] then it is vowel. So we will increase the countAlgorithmcountVowels(str)begin    count := 0    for each character ... Read More

C++ program to concatenate a string given number of times?

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

600 Views

Here we will see how we can concatenate a string n number of times. The value of n is given by the user. This problem is very simple. In C++ we can use + operator for concatenation. Please go through the code to get the idea.AlgorithmconcatStrNTimes(str, n)begin    res := empty string    for i in range 1 to n, do       res := concatenate res and res    done    return res endExample Live Demo#include using namespace std; main() {    string myStr, res = "";    int n;    cout > myStr;    cout > n;    for(int i= 0; i < n; i++) {       res += myStr;    }    cout

Advertisements