Found 34494 Articles for Programming

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

863 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

How to calculate combination and permutation in C++?

George John
Updated on 26-Jun-2020 14:06:26

10K+ Views

Combination and permutation are a part of Combinatorics. Permutation is the different arrangements that a set of elements can make if the elements are taken one at a time, some at a time or all at a time. Combination is is the different ways of selecting elements if the elements are taken one at a time, some at a time or all at a time.Number of permutations when there are total n elements and r elements need to be arranged.Number of combinations when there are total n elements and r elements need to be selected.A program that calculates combination and ... Read More

How do I convert a double into a string in C++?

Chandu yadav
Updated on 14-Sep-2023 02:20:31

28K+ Views

A double can be converted into a string in C++ using std::to_string. The parameter required is a double value and a string object is returned that contains the double value as a sequence of characters.A program that demonstrates this in C++ is given as follows.Example Live Demo#include #include using namespace std; int main() {    double d = 238649.21316934;    string s = to_string(d);    cout

C++ static member variables and their initialization

Arjun Thakur
Updated on 26-Jun-2020 14:08:02

5K+ Views

Static C++ member variables are defined using the static keyword. The static member variables 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.The static class member variables are initialized to zero when the first object of the class is created if they are not initialized in any other way.A program that demonstrates static member variables and their initialization in C++ is given as follows.Example Live Demo#include using namespace std; class Demo {    public :    static int num; ... Read More

Data Types we cannot use to create array in C

Chandu yadav
Updated on 26-Jun-2020 14:11:13

294 Views

An array can be created using all data types such as int, char, float, double etc. But creating an array by using the void data type is not possible. An error will be displayed if that is done.A program that demonstrates this is given as follows.Example Live Demo#include #include int main() {    void arr1[4];    printf("A void array");    return 0; }OutputThe above program returns the following error.error: declaration of ‘arr1’ as array of voids void arr1[4];Now let us understand the above program.An array arr1 of void data type is created in the above program. Since this is ... Read More

Initializing array with variable vs a real number in C++

Arjun Thakur
Updated on 26-Jun-2020 14:12:22

228 Views

An array is a collection of same type of elements at contiguous memory location. The lowest address in the array corresponds to the first element while highest address corresponds to the last element. The array index starts with zero(0) and ends with the size of array minus one(array size - 1).An array can be initialized with variables as well as real numbers. A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; int main() {    int a = 5;    int b = 3;    int arr[4];    arr[0] = a;    arr[1] = 8;    arr[2] = b;    arr[3] = 2;    cout

How to return local array from a C++ function?

Ankith Reddy
Updated on 26-Jun-2020 14:13:36

649 Views

A local array cannot be directly returned from a C++ function as it may not exist in memory after the function call. A way to resolve this is to use a static array in the function. As the lifetime of the static array is the whole program, it can easily be returned from a C++ function without the above problem.A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; int *retArray() {    static int arr[10];    for(int i = 0; i

How to pass a 2D array as a parameter in C?

George John
Updated on 26-Jun-2020 14:14:28

945 Views

A 2-D array can be easily passed as a parameter to a function in C. A program that demonstrates this when both the array dimensions are specified globally is given as follows.Example Live Demo#include const int R = 4; const int C = 3; void func(int a[R][C]) {    int i, j;    for (i = 0; i < R; i++)    for (j = 0; j < C; j++)    a[i][j] += 5; ; } int main() {    int a[R][C];    int i, j;    for (i = 0; i < R; i++)    for (j = 0; ... Read More

Advertisements