Server Side Programming Articles

Page 2092 of 2109

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 392 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 for Comb Sort?

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 424 Views

The basic idea of comb sort and the bubble sort is same. In other words, comb sort is an improvement on the bubble sort. In the bubble sorting technique, the items are compared with the next item in each phase. But for the comb sort, the items are sorted in a specific gap. After completing each phase, the gap is decreased. The decreasing factor or the shrink factor for this sort is 1.3. It means that after completing each phase the gap is divided by 1.3. Time Complexity is O(n log n) for best case. O(n2/2nP) (p is number of ...

Read More

C++ Program to Generate a Sequence of N Characters for a Given Specific Case

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

This is a C++ program to generate a sequence of N characters for a given specific case.AlgorithmsBegin    function GenerateSequence() generate a Sequence of N Characters for a Given Specific Case:       Use rand() for generating random indexes.       Store the first character directly into the sequence.       If that sequence is used earlier, then it discards that and generates random index again. EndExample#include #include #include using namespace std; void GenerateSequence(char string[], int n, int l, char *sequence) {    int i, j=0, k, in;    for(i = 0; i < n; i++) { ...

Read More

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 217 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
Showing 20911–20920 of 21,090 articles
Advertisements