Found 33676 Articles for Programming

Passing Arrays to Function in C++

Farhan Muhamed
Updated on 02-Jun-2025 19:35:02

381 Views

In this article, we will learn different ways to pass arrays to functions in C++ and how to work with them effectively. Passing Arrays to Functions Here are list of approaches to pass arrays to functions in C++ that will be discussed in this article along with examples: As a Sized Array As an Unsized Array As a Pointer (Pass by Pointer) As a Reference (Pass by Reference) As a Sized Array This method passes an array to a ... Read More

How to initialize a dynamic array in C++?

Farhan Muhamed
Updated on 16-Jul-2025 16:01:28

3K+ Views

Dynamic arrays are a type of array that can grow or shrink their size as elements are added or removed. They are created using pointers and memory management functions such as new and delete. In this article, we will learn different ways to initialize a dynamic arrays in C++. Here is the list of approaches to initialize a dynamic array in C++: Using new and () Using new and {} Using Loop for Custom Initialization Initializing Dynamic Arrays Using new and () This ... Read More

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

Farhan Muhamed
Updated on 29-May-2025 19:13:01

8K+ Views

Sorting of an array refer to the process of arranging the elements of the array in ascending or descending order. In this article, we will learn how to use the std::sort function from the C++ STL library to sort an array. In this problem, you are given an array of integers, and you need to arrange the elements in ascending order using std::sort. For example: // Input array int arr[] = {5, 2, 9, 1, 5, 6}; // Output {1, 2, 5, 5, 6, 9} // Explanation: The array is sorted in ascending order. C++ ... Read More

How to pass an array by reference in C++

Farhan Muhamed
Updated on 29-May-2025 19:13:47

7K+ Views

Passing an array by reference is a method of passing arrays to functions in C++ that allows the function to access and modify the original array without creating a copy. In this article, we will learn how to pass arrays by reference, the benefits of this approach, and provide examples. Passing an Array by Reference In the pass by reference approach, the address location of the first element of the array is passed to user defined functions. So, technically, we are passing the same array to that function without creating a copy. The changes made to the array ... Read More

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

Jennifer Nicholas
Updated on 03-Mar-2025 13:16:03

6K+ Views

In C/C++, we usually use the sizeof operator to find the size of an array. However, once an array is passed to a function, its size is lost. So, how can we find the size of an array without using sizeof? For example, consider this array: int arr[] = {1, 2, 3, 4, 5}; Without using sizeof, we can use a loop to count the number of elements in the array. In this article, we'll show you different methods to find the size of an array in C or C++ without using sizeof, with clear and simple examples. ... Read More

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

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

854 Views

A dynamic 2D array is an 2D array in which the size of array is determined during runtime and stored using pointers. In this article, we will learn how to create a dynamic 2D array inside a class in C++. First of all, let's understand the problem statement. In this problem, you need to create a class that contains a dynamic 2D array as its data member. The memory allocation and deallocation must be handled using constructor and destructor. For example: // Create a class with dynamic 2D array ClassMatrix m(3, 4); // Output Matrix of size ... Read More

Convert string to char array in C++

Nishu Kumari
Updated on 28-May-2025 18:05:05

4K+ Views

A string in C++ is a group of characters written inside double quotes like "Hello" and characters are the single letters or symbols in a string. Converting a string to a char array means putting each character into a simple list (array) of characters. In this article, we will show you how to convert a string to a character array in C++. For example, if we have the string "Hello@TP", converting it to a char array means storing each character 'H', 'e', 'l', 'l', 'o', '@', 'T', and 'P' separately in an array.How to Convert a String into a Character ... Read More

Overloading array index operator [] in C++

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

470 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

217 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

329 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

Advertisements