Found 33676 Articles for Programming

C++ Program to Implement Deque in STL

Farhan Muhamed
Updated on 06-May-2025 18:20:01

443 Views

Deque or Double Ended Queue is a special type of queue where we can insert and delete elements from both front and back. In this article, we will learn how to use deque from C++ STL (Standard Template Library). What is Deque? Deque is a linear data structure in which the insertion and deletion operations can be performed at both the ends (front and rear). Meaning, the data can be inserted at both front and rear positions and can be deleted from both front and rear positions. This gives more flexibility compared to a normal queue which supports only ... Read More

Binary Search functions in C++ STL

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

1K+ Views

Binary search is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the sorted array. The time complexity of binary search is O(1). This is a C++ program in which we implement various. Binary Search functions in C++ STLAlgorithmBegin    Initialize the vector of integer values.    The functions are used here:    binary_search(start_pointer, end_pointer, value) = Returns true if the value present in array otherwise    false.    lower_bound(start_pointer, end_pointer, value) = Returns pointer to “position of value” if container ... Read More

Sum of array using pointer arithmetic in C

Nitya Raut
Updated on 30-Jul-2019 22:30:25

1K+ Views

In this program, we need to find sum of array elements using pointer arithmetic.Here we use * which denotes the value stored at the memory address and this address will remain stored in the variable. Thus “int *ptr” means, ptr is a variable which contains an address and content of the address is an integer quantity.*p means it is a pointer variable. Using this and sum() we will find out sum of the elements of array.Example Code#include  void s(int* a, int len) { int i, s_of_arr = 0; for (i = 0; i 

4 Dimensional Array in C/C++

Farhan Muhamed
Updated on 29-May-2025 19:12:08

1K+ Views

4 Dimensional ArrayThe 4 dimensional array is an array of 3 dimensional arrays. Each 3 dimensional array is an array of 2 dimensional arrays and each 2 dimensional array is an array of 1 dimensional arrays. In this article, we will learn all about 4 dimensional arrays, how to create them and how to access their elements with examples in both C and C++. Syntax to Create a 4D Array Following is the syntax for declaring a 4 dimensional array in C/C++: datatype array_name[dimension1][dimension2][dimension3][dimension4]; // Example int arr[3][2][3][4]; Example of a 4 Dimensional Array The ... Read More

Why C/C++ array index starts from zero?

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

759 Views

As Array index starts with 0, so a[i] can be implemented as *(a + i).If Array index starts with 1 then a[i] will be implemented as *(a+i-1) which will be time consuming during compilation and the performance of the program will also be effected.So, it is better to start index of the array from 0.A simple program of array is given -Example Codeint main() {    int array[5] = {7, 7, 7, 6, 6};    for (int i = 0; i < 5; i++)       cout

Pointer vs Array in C

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

459 Views

Pointers and array most of the time are treated as same in c. Some differences are:&operator:&pointer = returns the address of pointer.&array = returns the address of first element.sizeof operator:sizeof(array) = returns the total memory consumed by the all the elements of the array.sizeof(pointer) = returns the only memory consumed by the pointer variable itself.Array variable can’t be re-assigned a value whereas pointer variable can.Declaration:int a[]; //array Int *p; //pointerLet us consider that there is an integer pointer variableint *i;Now let us consider the outcome of the following assignments –a = &i; //illegal assignment. a variable can not be updated ... Read More

An Uncommon representation of array elements in C/C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

128 Views

This is a simple C++ program of an uncommon representation of array elements.#include using namespace std; int main() { int array[5] = {7,7,7, 6, 6}; for (int i = 0; i < 5; i++) cout

Why C treats array parameters as pointers?

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

289 Views

C treats array parameter as pointers because it is less time consuming and more efficient. Though if we can pass the address of each element of the array to a function as argument but it will be more time consuming. So it’s better to pass the base address of first element to the function like:void fun(int a[]) { … } void fun(int *a) { //more efficient. ….. }Here is a sample code in C:#include void display1(int a[]) //printing the array content {    int i;    printf("Current content of the array is: ");    for(i = 0; i < ... Read More

Flexible Array Members in a structure in C

Nitya Raut
Updated on 30-Jul-2019 22:30:25

1K+ Views

Flexible Array members in a structure in C means we can declare array without its dimension within a structure and its size will be flexible in nature. Flexible array member must be the last member of class.Here is an example:Example#include #include #include //structure of type employee and must contain at least one more named member in addition to the flexible array member. struct employee {    int emp_id;    int name_len;    int emp_size; //‘emp_size’ variable is used to store the size of flexible    character array emp_name[].    char emp_name[]; //Flexible array member emp_name[] should be the last member ... Read More

How to create a dynamic array of integers in C++ using the new keyword

Farhan Muhamed
Updated on 02-Jun-2025 19:34:43

11K+ Views

Dynamic arrays are a type of array that can change their size when new elements are added or removed. They are created using pointers and memory management operators like new and delete. In this article, we will learn how to create and use a dynamic array in C++. What is Dynamic Array? A dynamic array is an array that can change its size during runtime. This is different from a static arrays, which have a fixed size determined by the programmer at compile time. Dynamic arrays are used when the size of the array is not known at compile ... Read More

Advertisements