Programming Articles - Page 2748 of 3366

Overloading array index operator [] in C++

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

480 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++

Farhan Muhamed
Updated on 28-May-2025 17:37:16

228 Views

A multidimensional array is an array with more than one dimension. It means that the array can grow in multiple directions, such as length, width, and height. In this article, we will learn how to print the dimensions of given multidimensional array in C++. First of all, let's understand the problem statement. You are given a multidimensional array as input, and you need to print its dimensions such as the number of rows, number of columns, and so on. For example: // Input multidimensional array int arr[3][4] = { {1, 2, 3, 4}, ... Read More

Array product in C++ using STL

Farhan Muhamed
Updated on 21-Jul-2025 18:54:59

351 Views

The product of an array refers to the multiplication result of all the elements in the array. The STL library of C++ provides several built-in functions to quickly calculate product of an array. In this article, we will explain all those STL functions with examples. Consider the following input/output scenario to understand the concept of array product: // Input array int arr[] = {1, 2, 3, 4, 5}; // Output 120 Explanation: The product of the elements in the array is 1 * 2 * 3 * 4 * 5 = 120. Following ... Read More

Sum of array using pointer arithmetic in C++

Farhan Muhamed
Updated on 27-May-2025 18:08:05

3K+ Views

The pointers in C++ are used to store memory addresses of variables. We can perform pointer arithmetic such as incrementing or decrementing pointers to traverse through an arrays. In this article, we will learn how to using pointer arithmetic in C++ to traverse through an array and find sum of it. First of all, let's understand the problem statement. You are given an array of integers and you need to find the sum of all the elements in that array using pointer arithmetic. For example: // Input array int arr[] = {1, 2, 3, 4, 5}; ... Read More

Initialization of a multidimensional arrays in C/C++

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

233 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

235 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

Why doesn\\'t C++ support functions returning arrays

Farhan Muhamed
Updated on 27-May-2025 18:23:19

203 Views

C++ does not support functions that return arrays directly because arrays do not have a built-in size or type information that can be returned along with the array. This design choice was made by the C++ creators to avoid memory management issues. But, we have alternative methods to achieve similar result. In this article, we will understand why C++ does not support functions returning arrays and how to overcome this limitation using other techniques. Functions Returning an Array First of all, let's understand what will happen if we try to return an array from a function in C++. Array ... Read More

Does C++ support Variable Length Arrays

Farhan Muhamed
Updated on 27-May-2025 18:10:41

343 Views

No, C++ does not support Variable Length Arrays (VLAs). A Variable length array is an array whose size is determined at runtime, not at compile time. These types of arrays are only supported in C99 version of C language. In this article, we will discuss the reasons why C++ does not support VLAs and what alternatives are available for variable array allocation. What is Variable Length Array (VLA)? A Variable Length Array (VLA) is an array whose size can be determined at runtime. It is not supported in standard C++. But, in C99 you can declare a VLA like ... Read More

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

Farhan Muhamed
Updated on 27-May-2025 18:09:10

914 Views

The delete[] operator is used to deallocate that memory from heap that was allocated using the new[] operator. In this article, we will explore, what is delete[] operator in C++, how it works, and how it knows the size of the operand array to deallocate the correct amount of memory. The delete[] Operator The delete[] operator is a C++ operator that is used to free memory that was previously allocated for an array using the new[] operator. It is always important to deallocate memory that has been allocated with new[] to avoid memory leaks in your program. If you ... 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