C++ Articles

Page 582 of 597

C++ Program to Compute Combinations using Recurrence Relation for nCr

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 702 Views

This is a C++ program to compute Combinations using Recurrence Relation for nCr.AlgorithmsBegin    function CalCombination():       Arguments: n, r.       Body of the function:       Calculate combination by using       the formula: n! / (r! * (n-r)!. EndExample#include using namespace std; float CalCombination(float n, float r) {    int i;       if(r > 0)          return (n/r)*CalCombination(n-1,r-1);       else    return 1; } int main() {    float n, r;    int res;    coutn;    coutr;    res = CalCombination(n,r);    cout

Read More

C++ Program to Perform integer Partition for a Specific Case

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 216 Views

This is a C++ program to perform integer partition for a specific case. In this program, a positive integer n is given, and shall have to generate all possible unique ways to represent n as sum of positive integers.AlgorithmBegin    function displayAllUniqueParts(int m):    1) Set Index of last element k in a partition to 0    2) Initialize first partition as number itself, p[k]=m    3) Create a while loop which first prints current partition, then generates next partition. The loop stops    when the current partition has all 1s.    4) Display current partition as displayArray(p, k + ...

Read More

C++ program to print Happy Birthday

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 7K+ Views

This is a C++ program to print Happy Birthday.AlgorithmBegin    Take a str1 which takes the next character of our desired ouput like for H it will be G.    Assign the string to a pointer p.    Make a while loop till *p != NULL.       Go next character of the string print it and after that go the nextposition of string.    Print the result. EndExample#include using namespace std; main(){    char str[]="G`ooxAhqsgc`x",*p;    p=str;    while(*p!='\0')       ++*p++;    cout

Read More

How to initialize a const field in constructor?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 968 Views

Here we will see how to initialize the const type variable using constructor?To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma.Example#include using namespace std; class MyClass {    private:    const int x;    public:       MyClass(int a) : x(a) {          //constructor       }       void show_x() {   ...

Read More

Lexicographically next permutation in C++

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 2K+ Views

Here we will see how to generate lexicographically next permutation of a string in C++. The lexicographically next permutation is basically the greater permutation. For example, the next of “ACB” will be “BAC”. In some cases, the lexicographically next permutation is not present, like “BBB” or “DCBA” etc.In C++ we can do it by using a library function called next_permutation(). This is present in the algorithm header file.Example#include #include using namespace std; main() {    string s = "DBAC";    for(int i = 0; i

Read More

C++11 reverse range-based for-loop

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 713 Views

To get the reversed range-based for loop, we have used boost library. This boost library is vepy popular and it has some strong functionalities.Here we can use some array or containers, then by using boost::adaptors::reverse() we can use the range base for loop in reverse order.Example#include #include #include using namespace std; int main() {    std::list x {11, 44, 77, 55, 44, 22, 33, 30, 88, 99, 55, 44};    cout >> "Normal Loop" >> endl;    for (auto i : x)       std::cout >> i >> '';    cout >> "Reversed Loop" >> endl;    for (auto i : boost::adaptors::reverse(x))       std::cout >> i >> ''; }OutputNormal Loop 11 44 77 55 44 22 33 30 88 99 55 44 Reversed Loop 44 55 99 88 30 33 22 44 55 77 44 11

Read More

How to use clock() function in C++

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 1K+ Views

Here we will see how to use the clock() in C++. This clock() is present in the time.h or ctime header file. Here we will find the elapsed time of a process using this clock() functionTo get the elapsed time, we can get the time using clock() at the beginning, and at the end of the taks, then subtract the values to get the differences. After that we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.Example#include #include using namespace std; void take_enter() {    cout

Read More

C++ Program to Implement Levenshtein Distance Computing Algorithm

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 4K+ Views

The Levenshtein distance between two strings means the minimum number of edits needed to transform one string into the other, with the edit operations i.e; insertion, deletion, or substitution of a single character.For example: The Levenshtein Distance between cat and mat is 1 −cat mat(substitution of ‘c’ with ‘m’)Here is a C++ Program to implement Levenshtein Distance computing algorithm.AlgorithmsBegin    Take the strings as input and also find their length.    For i = 0 to l1       dist[0][i] = i    For j = 0 to l2       dist[j][0] = j    For j=1 to l1 ...

Read More

Any datatype in C++ boost library

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 309 Views

The boost library has large range of functionalities. The any datatype is one of them. Any datatype is used to store any type of values in variable. Some other languages like javascripts, python, we can get this kind of datatypes. In C++ we can get this feature only using boost library.Example#include "boost/any.hpp" #include using namespace std; main() {    boost::any x, y, z, a; //define some variable of any datatype    x = 20; //Store x as integer    cout >> "x : " >> boost::any_cast(x) >> endl; //display the value of x    y = 'A'; //Store y ...

Read More

C++ Program to Find Second Smallest of n Elements with Given Complexity Constraint

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 196 Views

This is a C++ program to find Second Smallest of n elements with given complexity constraint.AlgorithmBegin    function SecondSmallest() :       /* Arguments to this function are:          A pointer array a.          Number of elements n       */    // Body of the function:       A variable s1 is declared as to keep track of smallest number.       A variable s2 is declared as to keep track of second smallest number.       Initialize both s1 and s2 with INT_MAX.       Traverse ...

Read More
Showing 5811–5820 of 5,962 articles
« Prev 1 580 581 582 583 584 597 Next »
Advertisements