Found 7346 Articles for C++

Sum of array using pointer arithmetic in C++

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

3K+ 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

131 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

139 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

819 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

91 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

194 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

606 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

How do I declare a 2d array in C++ using new

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

246 Views

A dynamic 2D array is basically an array of pointers to arrays. Here is a diagram of a 2D array with dimenation 3 x 4.AlgorithmBegin    Declare dimension of the array.    Dynamic allocate 2D array a[][] using new.    Fill the array with the elements.    Print the array.    Clear the memory by deleting it. EndExample 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 int[A];   ... Read More

Why is it faster to process a sorted array than an unsorted array in C++ program?

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

115 Views

In C++, it is faster to process a sorted array than an unsorted array because of branch prediction. In computer architecture, a branch prediction determines whether a conditional branch (jump) in the instruction flow of a program is likely to be taken or not.Let’s take an example:if(arr[i] > 50) { Do some operation B } else { Do some operation A }If we run this code for 100 elements in unsorted and sorted order below things will be happened:For sorted array:1, 2, 3, 4, 5, ……50, 51………100 A, A, A, A, A A, B ... Read More

Advertisements