Found 33676 Articles for Programming

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

224 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

224 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

178 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

317 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

887 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

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

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

341 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?

Farhan Muhamed
Updated on 26-May-2025 18:16:59

202 Views

In C++, it is faster to process a sorted array than an unsorted array due to some reasons related to algorithm efficiency and data access patterns. In this article, we see why this is the case and how sorting can improve performance of array processing. First of all, let see an example of processing time of a sorted and unsorted array. Processing Time: Sorted vs Unsorted Array In the example code below, we have used library of C++ STL to measure the time taken to process an unsorted array and a sorted array. The code counts how many ... Read More

C++ Program to Implement Bit Array

Farhan Muhamed
Updated on 05-May-2025 18:29:04

2K+ Views

Bit array is used to store true/false or yes/no type of values in very less memory. In this article, we will see how to create and use a bit array in C++. What is Bit Array? Bit array is a special array where each element can hold only 0 or 1. Instead of using a full byte (8 bits) for each boolean value, a bit array stores multiple values using just one bit each. Meaning, in a normal array of bool values, each value takes at least one byte. But in bit array, we store 8 values in one ... Read More

Advertisements