Found 7346 Articles for C++

Passing Arrays to Function in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

234 Views

C++ does not allow to pass an entire array as an argument to a function. However, you can pass a pointer to an array by specifying the array's name without an index.If you want to pass a single-dimension array as an argument in a function, you would have to declare function formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.Method-1Formal parameters as a pointer as follows −void myFunction(int *param) {    .    .    . }Method-2Formal parameters as ... Read More

How to initialize a dynamic array in C++?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

2K+ Views

Here is a C++ program to initialize a dynamic array. Here in this program we can use dynamically allocated array to return a local array from the function Array().Example Code Live Demo#include using namespace std; int* Array() {    int* a = new int[100];    a[0] = 7;    a[1] = 6;    a[2] = 4;    a[3] = 5;    return a; } int main() {    int* p = Array();    cout

How to use std::sort to sort an array in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

8K+ Views

In programming language, sorting is a basic function which is applied to data to arrange these data is ascending or descending data. In C++ program, there is a function std::sort() for sorting the array.sort(start address, end address)Here,Start address => The first address of the element. Last address => The address of the next contiguous location of the last element of the array.Example Code Live Demo#include #include using namespace std; void display(int a[]) {    for(int i = 0; i < 5; ++i)    cout

How to pass an array by reference in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

6K+ Views

If we pass the address of an array while calling a function, then this is called function call by reference. The function declaration should have a pointer as a parameter to receive the passed address, when we pass an address as an argument.Example Code Live Demo#include using namespace std; void show( int *num) {    cout

Find size of array in C/C++ without using sizeof

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

5K+ Views

In this program, we find out Find size of array in C/C++ without using sizeof.AlgorithmBegin Initialize the elements of the array. &a => This is the pointer to array which points at the same memory address as a. &a + 1 => It points at the address after the end of the array. *(a+1) => Dereferencing to *(&a + 1) gives the address after the end of the last element. *(a+1)-a => Subtract the pointer to the first element to get the length of the ... Read More

How to create a dynamic 2D array inside a class in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

715 Views

Here it is a simple C++ program to create a dynamic 2D array inside a class to print the elements of the array.AlgorithmBegin Create a class Arr and declare size of array. Inside the class, initialize all the elements by using for loop. Print the all elements. End.Example Code Live Demo#include #include using namespace std; const int M=7; class Arr {    private:    int A[M];    int size;    public:    Arr(int s, int v) {       if(s>M) {          cout

Convert string to char array in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

3K+ Views

This is a C++ program to Convert string to char array in C++. This can be done in many ways:Type 1:AlgorithmBegin Assign value to string m. For i = 0 to sizeof(m) Print the char array. EndExample Code Live Demo#include #include using namespace std; int main() {    char m[] = "Tutorialspoint";    string str;    int i; for(i=0;i

Overloading array index operator [] in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

339 Views

Operator overloading has an important role in object oriented programming language features. Operator overloading is a type of compile time or static polymorphism.AlgorithmBegin Create a class Arr and declare size of array. Inside the class, initialize all the elements by using for loop. Print the all elements. End.Example Code Live Demo#include #include using namespace std; const int M = 7; class Arr {    private:    int A[M];    int size;    public:    Arr(int s, int v) {       if(s>M) {          cout

How to print dimensions of multidimensional array in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

146 Views

Here is a C++ program to print dimensions of given array.AlgorithmHere template() function is used to find out the current size of array. Then recursively call it till the last dimension of array.Example Code Live Demo#include using namespace std; template void printDimensionsOfArray(const t (&a)[n]) {    cout

Array product in C++ using STL

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

159 Views

This is an example of C++ program to find out Array Product.AlgorithmBegin Initialize the values of array. Call used defined function accumulate to return the product of array. Print the solution. End.Example Code Live Demo#include #include using namespace std; int ProductOfArray(int p[], int n) {    return accumulate(p, p + n, 1, multiplies()); } int main() { int m[] = {6,7 }; int n = sizeof(m) / sizeof(m[0]); cout

Advertisements