Found 34471 Articles for Programming

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

724 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

343 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

147 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

162 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

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

134 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

143 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

824 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

Advertisements