Found 7346 Articles for C++

C++ Program to Perform Stooge Sort

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

238 Views

Stooge Sort is used to sort the given data. It is a recursive sorting algorithm. Stooge Sort divides the array into two overlapping parts, 2/3 each and Sort the array in three steps by sorting I then II and again I part. The worst case time complexity of this algorithm is O(n^2.7095).AlgorithmBegin Take input of data. Call StoogeSort() function with ‘a’ the array of data and ‘n’ the number of values, in the argument list. Implement Sorting using recursive approach. Divide the array into first 2/3 element as ... Read More

C++ Program to Perform the Shaker Sort

George John
Updated on 30-Jul-2019 22:30:25

1K+ Views

Shaker sort is used to sort the given data. Shaker sort, unlike Bubble sort, orders the array in both directions. The worst complexity of this algorithm is O(n^2).AlgorithmBegin    ShakerSort() function has ‘arr’ the array of data and ‘n’ the number of values, in the argument list.    // Implement Sorting algorithm using nested for loops.    The parent loop will run on ‘i’ from 0 to n-1 and contains two loops inside.    The first loop will run on ‘j’ from i+1 to n-1 and use swap() if a[j] < a[j-1].    Decrement n.    The second loop will ... Read More

C++ Program to Solve Knapsack Problem Using Dynamic Programming

Chandu yadav
Updated on 30-Jul-2019 22:30:25

13K+ Views

This is a C++ program to solve 0-1 knapsack problem using dynamic programming. In 0-1 knapsack problem, a set of items are given, each with a weight and a value. We need to determine the number of each item to include in a collection so that the total weight is less than or equal to the given limit and the total value is large as possible.AlgorithmBegin Input set of items each with a weight and a value Set knapsack capacity Create a function that returns maximum of two integers. Create a function which returns the maximum value that can be ... Read More

Does Ternary operation exist in MySQL just like C or C++?

Samual Sam
Updated on 30-Jul-2019 22:30:25

49 Views

Yes, let us first see the working of ternary operator in C or C++ language.X=(X > 10 && ( X-Y) < 0) ?: X:(X-Y);Here is the demo code in C language. After that we will check in MySQL. The C code is as follows −#include int main() {    int X;    int Y;    int result;    printf("Enter the value for X:");    scanf("%d", &X);    printf("Enter the value for Y:");    scanf("%d", &Y);    result=( X > 1 && (X-Y) < 0) ? X: (X-Y);    printf("The Result is=%d", result);    return 0; }The snapshot of C ... Read More

How to create a static class in C++?

Arjun Thakur
Updated on 13-Apr-2023 00:15:52

21K+ Views

There is no such thing as a static class in C++. The closest approximation is a class that only contains static data members and static methods.Static data members in a class are shared by all the class objects as there is only one copy of them in the memory, regardless of the number of objects of the class. Static methods in a class can only access static data members, other static methods or any methods outside the class.A program that demonstrates static data members and static methods in a class in C++ is given as follows. Example #include ... Read More

Why array index starts from zero in C/C++ ?

Ankith Reddy
Updated on 26-Jun-2020 14:18:25

6K+ Views

An array arr[i] is interpreted as *(arr+i). Here, arr denotes the address of the first array element or the 0 index element. So *(arr+i) means the element at i distance from the first element of the array. So array index starts from 0 as initially i is 0 which means the first element of the array.A program that demonstrates this in C++ is as follows.Example Live Demo#include using namespace std; int main() {    int arr[] = {5,8,9,3,5};    int i;    for(i = 0; i

How to print size of array parameter in a function in C++?

George John
Updated on 26-Jun-2020 14:19:16

217 Views

The size of a data type can be obtained using sizeof(). A program that demonstrates the printing of the array parameter in a function in C++ is given as follows.Example Live Demo#include using namespace std; int func(int a[]) {    cout

When are static objects destroyed in C++?

Chandu yadav
Updated on 26-Jun-2020 14:20:43

2K+ Views

Static objects are declared with the keyword static. They are initialized only once and stored in the static storage area. The static objects are only destroyed when the program terminates i.e. they live until program termination.A program that demonstrates static objects in C++ is given as follows.Example Live Demo#include using namespace std; class Base {    public :    int func() {       int a = 20;       cout

How static variables in member functions work in C++?

Arjun Thakur
Updated on 26-Jun-2020 14:21:40

864 Views

The static variables in member functions are declared using the keyword static. The space for the static variables is allocated only one time and this is used for the entirety of the program. Also, there is only one copy of these static variables in the whole program.A program that demonstrates static variables in member functions in C++ is given as follows.Example Live Demo#include using namespace std; class Base {    public :    int func() {       static int a;       static int b = 12;       cout

Why do we assume strncpy insecure in C/C++?

Ankith Reddy
Updated on 26-Jun-2020 14:04:29

468 Views

The function strncpy() is used to copy the specified number of characters to the destination from the source.The following is the syntax of strncpy()char *strncpy( char *destination, char *source, size_t n);Here, destination is the pointer to the destination array where the source string is to be copied, source is the string is to be copied and n is the maximum number of characters to be copied from the source string.The strncpy() function is insecure because if the NULL character is not available in the first n characters in the source string then the destination string will not be NULL terminated.A ... Read More

Advertisements