Found 7197 Articles for C++

Absolute Difference of all pairwise consecutive elements in an array (C++)?

Revathi Satya Kondra
Updated on 01-Aug-2025 18:06:10

663 Views

An array in C++ is a data structure used to store multiple values of the same type in a contiguous block of memory. To know that how values shift from one index to the next, a common and practical technique is to compute the absolute difference between each pair of consecutive elements. Absolute Difference The absolute difference is the positive distance between two numbers, regardless of which one is larger. It is computed using the abs() function from the library. Absolute difference between a and b is denoted as |a - b|. For example, |7 - 3| = ... Read More

A Sum Array Puzzle in C++?

Ravi Ranjan
Updated on 11-Jun-2025 18:41:02

250 Views

In this article, we will solve a sum array puzzle, where we have an array with n elements. We have to create another array of n elements such that the 'ith' position of the second array will hold the sum of all elements of the first array except the 'ith' element. We have one constraint, we cannot use the subtraction operator in this problem. Example Here is an example of calculating the sum of array elements except for the ith element: Input: arr[] = {5, 6, 7, 8} Output: res[] = ... Read More

A Product Array Puzzle in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

287 Views

Here we will see one interesting problem related to array. There is an array with n elements. We have to create another array of n elements. But the i-th position of second array will hold the product of all elements of the first array except the i-th element. And one constraint is that we cannot use the division operator in this problem.If we can use the division, operation, we can easily solve this problem, by getting the product of all elements, then divide i-th element of first array and store it into i-th place of the second array.Here we are ... Read More

A product array puzzle (O(1) Space) in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

185 Views

Here we will see one interesting problem related to array. There is an array with n elements. We have to create another array of n elements. But the i-th position of second array will hold the product of all elements of the first array except the i-th element. And one constraint is that we cannot use the division operator in this problem. We have to solve this problem in-place without using any additional spaces.If we can use the division, operation, we can easily solve this problem, by getting the product of all elements, then divide i-th element of first array ... Read More

A Boolean Matrix Question in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

258 Views

Here we will see one interesting Boolean matrix problem. One Boolean matrix is given which contains 0’s and 1’s. Our goal is to find where 1 is marked. If the 1 is marked at position mat[i, j], then we will make all entries to 1 of the row i and column j. Let us see an example. If the matrix is like below −1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0Then after modification, it will be −1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1AlgorithmmatrixUpdate(matrix[R, ... Read More

C/C++ program to make a simple calculator?

karthikeya Boyini
Updated on 14-Feb-2025 18:07:39

1K+ Views

A simple calculator allows the user to perform basic math operations like addition, subtraction, multiplication, and division . The user inputs two numbers and selects an operation. For example, if the user enters 4 and 3, then selects the "+" operation, the calculator will perform addition and output the result, 7. Similarly, if the user selects "-", the program will perform subtraction (4 - 3 = 1), and so on. In C++, this can be done using conditional statements and basic math operators. In this article, we will show you how to write a simple calculator program in ... Read More

Power Function in C/C++

Sharon Christine
Updated on 30-Jul-2019 22:30:26

1K+ Views

Power function is used to calculate the power of the given number.The pow function find the value of a raised to the power b i.e. ab.Syntaxdouble pow(double a , double b)It accepts a double integers as input and output a double integer as output. It pow() function is defined in math.h package.If you pass an integer to the power function, the function converts it into double data type. But there's an issue with this, sometimes this conversion might store this as a lower double. For example, if we pass 3 and is converted as 2.99 then the square is 8.99940001 ... Read More

C++ Program to Create the Prufer Code for a Tree

Anvi Jain
Updated on 30-Jul-2019 22:30:26

804 Views

Prufer code uniquely identifies a tree which is given by user as a graph representation with labels from 1 to p. This tree consist p(value is given by user) labels of node. It has sequence of p – 2 values.AlgorithmBegin    Declare i, j, ver, edg, minimum, p to the integer datatype.    Print “Enter the number of vertexes: ”.    Enter the value of ver.    Initialize edg = ver-1.    Declare EDG[edg][2], DG[ver+1] to the integer datatype.       Initialize DG[ver+1] = {0}.    Print “This tree has (value of edg) edges for (value of ver) vertexes”. ... Read More

Print prime numbers in a given range using C++ STL

Smita Kapse
Updated on 30-Jul-2019 22:30:26

603 Views

It is the program to print prime numbers in a given range.AlgorithmsBegin    Declare a user define datatype stl.    Declare a vector number to the stl datatype.    Declare variable a to the stl datatype.    Declare vector Prime_Number to the Boolean datatype.       Prime_Number[0] = false.       Prime_Number[1] = false.    Declare b to the integer datatype.       Initialize b = sqrt(a).    for (stl pr=2; pr

C++ Program to Find Closest Pair of Points in an Array

Anvi Jain
Updated on 30-Jul-2019 22:30:26

831 Views

This is the program to find closest pair of points in an array.AlgorithmsFor Distance between Closest pointBegin    Declare function Closest_dist_Spoint(poi stp[], int s, double dist, poi &pnt1, poi &pnt2) to the double datatype.    Declare Minimum to the double datatype.       Initialize Minimum = dist.    for (int i = 0; i < s; ++i)       for (int j = i+1; j < s && (stp[j].poi2 - stp[i].poi2) < Minimum; ++j)          if (Distance(stp[i], stp[j]) < Minimum) then          Minimum = Distance(stp[i], stp[j]).             ... Read More

Advertisements