Found 7347 Articles for C++

The basic_string c_str function in C++ STL?

sudhir sharma
Updated on 04-Oct-2019 06:39:11

68 Views

The basic_string c_str function that returns a pointer to an array of characters that is terminated using null character. It is an inbuilt method that has the value of a string that has null character termination.Syntax to define a c_str function in C++ −const Char ptr* c_str() constAbout the functionIt is an inbuilt method for the c++ STL library. No parameters can be passed to the method. It returns a char pointer. This pointer points to NULL terminated character array.Example Live Demo#include #include using namespace std; int main() {    string s = "I Love Tutorials Point";    int ... Read More

Absolute distinct count in a sorted array in C++?

sudhir sharma
Updated on 06-Jul-2020 08:24:37

210 Views

An array is a collection of elements of the same data type. A sorted array is an array that has elements stored in a sequence of ascending or descending order.The distinct count is the number of elements that are not the same.Absolute distinct count is distinct count of absolute value of the elements i.e. elements without sign(unsigned values).In this program we will find the absolute distinct count in a sorted array. i.e. we will count the number of distinct values if absolute value of each element of the array is considered.For example, Input : [-3 , 0 , 3 , ... Read More

Anti Clockwise spiral traversal of a binary tree in C++?

sudhir sharma
Updated on 03-Oct-2019 13:02:36

102 Views

Anti-Clockwise spiral traversal of a binary tree is traversing the elements of a tree in such a way that if traversed they make a spiral but in reverse order. The following figure shows how a anti-clockwise spiral traversal of binary tree.The Algorithm defined for spiral traversal of a binary tree works in the following way −Two variables i and j are initialized and values are equated as i = 0 and j = height of variable. A flag is used to check while section is to be printed. Flag is initially is set of false. A loop working till i ... Read More

Arc length from given Angle in C++?

sudhir sharma
Updated on 03-Oct-2019 12:58:30

341 Views

An Angle is formed when two rays meet at a point. The point on the plane at which these rays meet is vertex.Arc of a circle is a portion of the circumference that is described by an angle.In this problem, we are given an angle of the circle. And we need to find the length of arc using the given diameter of the circle. For example, Input : Angle = 45° Diameter = 28 Output : Arc = 11ExplanationLength of arc = (circumference) X (angle/360°)= (π * d)*(angle/360°)To make a program that calculates the length of Arc from the given ... Read More

Absolute Difference of even and odd indexed elements in an Array in C++?

sudhir sharma
Updated on 03-Oct-2019 12:54:24

769 Views

An array is a container of multiple elements of the same data type. The index of elements start from 0 i.e. first element has index 0.In this problem, we need to find the absolute difference between two even indexed number and two odd indexed numbers.Even indexed number = 0, 2, 4, 6, 8….Odd indexed number = 1, 3, 5, 7, 9…Absolute difference is the modulus of difference between two elements.For example, Absolute difference of 15 and 7 = (|15 - 7|) = 8Input: arr = {1 , 2, 4, 5, 8} Output : Absolute difference of even numbers = 4 ... Read More

Adding one to number represented as array of digits in C++?

sudhir sharma
Updated on 03-Oct-2019 12:48:57

202 Views

A number represented as array stored each digit of the number in a single element of the array. The length of the array is equal to the number of digits in the array i.e. length = 3 for four digit number. Each element of the array is a single digit number. The number is stored in such a way that the last element store the least significant digit of the number. And the first element stores the most significant digit of the number. For example, Number − 351932 is stored as {3, 5, 1, 9, 3, 2}To add one to ... Read More

C++ program to find whether there is a path between two cells in matrix

Ayush Gupta
Updated on 03-Oct-2019 12:28:49

127 Views

In this article, we will be discussing a program to find whether there exists a path between two cells in a given matrix.Let us suppose we have been given a square matrix with possible values 0, 1, 2 and 3. Here, 0 means Blank Wall1 means Source2 means Destination3 means Blank CellThere can only be one Source and Destination in the matrix. The program is to see if there’s a possible path from Source to Destination in the given matrix moving in all four possible directions but not diagonally.Example Live Demo#include using namespace std; //creating a possible graph from given array ... Read More

C++ program to find whether only two parallel lines contain all coordinates points or not

Ayush Gupta
Updated on 03-Oct-2019 12:21:59

291 Views

In this article, we will be discussing a program to find whether only two parallel lines can hold all the given coordinates points.For this we will be given an array, such that the coordinates will be (i, arr[i]). Let us suppose we are given an array, arr = {2, 6, 8, 12, 14}Then we can have these points on two parallel lines, the first line containing (1, 2), (3, 8) and (5, 14). The second line having the rest coordinates i.e (2, 6) and (4, 12).This problem can be solved by comparing the slopes of the lines made by the ... Read More

C++ program to find the vertex, focus and directrix of a parabola

Ayush Gupta
Updated on 03-Oct-2019 12:18:03

85 Views

In this article, we will be discussing a program to find the vertex, focus and directrix of a parabola when the coefficients of its equation is given.Parabola is a curve whose all points on the curve are equidistant from a single point called focus.As we know the general equation for a parabola isy = ax2 + bx + cFor this equation, the following are defined as :Vertex -(-b/2a, 4ac - b2/4a) Focus - (-b/2a, 4ac - b2+1/4a) Directrix - y = c - (b2 +1)4aExample Live Demo#include using namespace std; void calc_para(float a, float b, float c) {    cout ... Read More

C++ program to find the probability of a state at a given time in a Markov chain

Ayush Gupta
Updated on 03-Oct-2019 12:15:35

332 Views

In this article, we will be discussing a program to find the probability of reaching from the initial state to the final state in a given time period in Markov chain.Markov chain is a random process that consists of various states and the associated probabilities of going from one state to another. It takes unit time to move from one state to another.Markov chain can be represented by a directed graph. To solve the problem, we can make a matrix out of the given Markov chain. In that matrix, element at position (a, b) will represent the probability of going ... Read More

Advertisements