Found 7350 Articles for C++

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

156 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

Sum of array using pointer arithmetic in C++

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

2K+ Views

This is a C++ program to find out sum of array elements using pointer.AlgorithmBegin    Initialize the array elements with values from user input.    Initialize s = 0    Loop for i = 0 to       s = s + *(ptr + i)    Print the sum value in variable s. EndExample Code Live Demo#include using namespace std; int main() {    int a[7], i, s = 0;    int *ptr;    cout > a[i];    }    ptr = a;    for (i = 0; i < 7; i++) {       s = s + *(ptr + i);    } cout

Initialization of a multidimensional arrays in C/C++

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

129 Views

In multidimensional array, the array should have dimension more that 1. The following diagram shows the memory allocation strategy for a multidimensional array with dimension 3 x 3 x 3.This is a C++ program to initialize a multidimensional array.AlgorithmBegin    Initialize the elements of a multidimensional array.    Print the size of the array.    Display the content of the array. EndExample#include using namespace std; int main() {    int r, c;    int a[][2] = {{3,1},{7,6}};    cout

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

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

136 Views

This is a C++ program return a local array from a function.AlgorithmBegin We can use dynamically allocated array to return a local array from function Array(). Print the elements of the array. EndExample 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

Array of Strings in C++

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

808 Views

Array of strings can be created in C++ using string keyword. Here we are discussing a C++ program by using this approach.AlgorithmBegin Initialize the elements of array by string keyword. And take string as input. Print the array. End.Example Code Live Demo#include #include using namespace std; int main() {    string Fruit[3] = {"Grape", "Mango", "Orange"};    cout

Why doesn't C++ support functions returning arrays

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

87 Views

Let us consider this following program, Live Demo#include using namespace std; int* Array() {    int a[100];    a[0] = 7;    a[1] = 6;    a[2] = 4;    a[3] = 3;    return a; } int main() { int* p = Array(); cout

Does C++ support Variable Length Arrays

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

188 Views

C++ does not support variable length arrays. The C++11 standard mentions array size as a constant-expression.So if we write a program in C++ like:void displayArray(int n) { int arr[n]; // ...... } int main() { displayArray(7); } It will not work.

How does delete[] “know” the size of the operand array in C++

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

570 Views

New operator is used for dynamic memory allocation which puts variables on heap memory. Delete[] operator is used to deallocate that memory from heap. New operator stores the no of elements which it created in main block so that delete [] can deallocate that memory by using that number.Example Code Live Demo#include using namespace std; int main() { int B = 4; int A = 5; int** a = new int*[B]; for(int i = 0; i < B; ++i) a[i] = new ... Read More

Passing a 2D array to a C++ function

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

2K+ Views

Arrays can be passed to a function as an argument. In this program, we will perform to display the elements of the 2 dimensional array by passing it to a function.AlgorithmBegin The 2D array n[][] passed to the function show(). Call function show() function, the array n (n) is traversed using a nested for loop. EndExample Code Live Demo#include using namespace std; void show(int n[4][3]); int main() {    int n[4][3] = {       {3, 4 ,2},       {9, 5 ,1},       {7, 6, 2},       {4, 8, 1}};    show(n);    return 0; } void show(int n[][3]) { cout

Advertisements