Found 7401 Articles for C++

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

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

527 Views

New operator is used for dynamic memory allocation which puts variables on heap memory. Delete[] operator is used to deallocate that memory from heap. New operator stores the no of elements which it created in main block so that delete [] can deallocate that memory by using that number.Example 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 ... 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

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

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

107 Views

In C++, it is faster to process a sorted array than an unsorted array because of branch prediction. In computer architecture, a branch prediction determines whether a conditional branch (jump) in the instruction flow of a program is likely to be taken or not.Let’s take an example:if(arr[i] > 50) { Do some operation B } else { Do some operation A }If we run this code for 100 elements in unsorted and sorted order below things will be happened:For sorted array:1, 2, 3, 4, 5, ……50, 51………100 A, A, A, A, A A, B ... Read More

C++ Program to Implement Bit Array

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

2K+ Views

This is a C++ program to implement Bit Array. A Bit Array is an array data structures that compactly stores data. It is basically used to implement a simple data structure.AlgorithmFunctions and pseudocodes:Begin    Function getBit(int val, int pos)    singleBit->b = 0    if(pos == 0)       singleBit->b = val & 1    else       singleBit->b = ( val & (1 > pos       return singleBit    Function setBit(BitArr *bt, B *bit, int pos)       bt->bVal[pos] = bit       return bt    Function getVal(BitArr *bArray)    initialize val = ... Read More

C++ Program to Implement Graph Structured Stack

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

372 Views

In this C++ program we implement Graph Structured Stack.AlgorithmBegin    Function graphStructuredStack(int **adjMat, int s, int bNode):    Take an array adjMat, source s and bottom node bNode    initialize stackFound = false    for sVertex = 1 to noOfNodes       for dVertex = 1 to noOfNodes          this->adjMat[sVertex][dVertex] = adjMat[sVertex][dVertex]       Done    Done    Push source into mystack.    while (!mystack.empty())       element = mystack.top()       Initialize destination, d=1       while (d adjMat[element][d] == 1)             Push destination into mystack ... Read More

C++ Program to Check Whether Graph is DAG

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

1K+ Views

A directed acyclic graph (DAG) is a graph that is directed and without cycles connecting the other edges. The edges of this graph go one way. This is a C++ program to check whether the graph is DAG.AlgorithmBegin Function checkDAG(int n):    intialize count = 0    intialize size = n - 1    for i = 0 to n-1       if (count == size)          return 1       done       if (arr[i].ptr == NULL)          increment count          for j = 0 to n-1 ... Read More

C++ Program to Perform Edge Coloring to the Line Graph of an Input Graph

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

219 Views

The line graph of an undirected graph G is another graph L(G) that represents the adjacencies between edges of G. In this program, we Perform Edge Coloring to the Line Graph of an Input Graph.AlgorithmBegin    Take the input of the number of vertices ‘n’ and number of edges ‘e’.    Take the input of ‘n’ vertex pairs for the ‘e’ edges in the graph in ed[][].    Function GenLineGraph():       Construct a line graph in LineEd[][].       To construct line graph, for each edge in the given graph, connect it    to its adjacent edge ... Read More

C++ Program to Perform Graph Coloring on Bipartite Graphs

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

333 Views

A bipartite graph is a graph in which if the graph coloring is possible using two colors i.e.; vertices in a set are colored with the same color. In this program we take a bipartite graph as input and outputs colors of each vertex after coloring the vertices.AlgorithmBegin BFS algorithm is used to traverse all the vertices. Take a vertex and colour it yellow. Colour all its neighbour vertices as blue. Colour the next level vertices as yellow and so, until all vertices are coloured. End.Example Code#include using ... Read More

Is there a performance difference between i++ and ++i in C++?

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

709 Views

There is a big distinction between the suffix and prefix versions of ++.In the prefix version (i.e., ++i), the value of i is incremented, and the value of the expression is the new value of i. So basically it first increments then assigns a value to the expression.In the postfix version (i.e., i++), the value of i is incremented, but the value of the expression is the original value of i. So basically it first assigns a value to expression and then increments the variable.Let's look at some code to get a better understanding.Example Code#include using namespace std; int main() ... Read More

Advertisements