Found 7346 Articles for C++

C++ Program to Generate Random Numbers Using Middle Square Method

Nancy Den
Updated on 30-Jul-2019 22:30:25

912 Views

The middle-square method is one of the simplest method of generating random numbers. this method will either begin repeatedly generating the same number or cycle to a previous number in the sequence and loop indefinitely. For a generator of ndigit random numbers, the period can be no longer than n. If the middle n digits are all zeroes, the Generator then outputs zeroes forever, while these runs of zero are easy to detect, they occur too frequently for this method to be of practical use.Input − Enter the digit for random number:4 Output − The random numbers are: 6383, 14846, ... Read More

C Program to sum the digits of a given number in single statement

Chandu yadav
Updated on 30-Jul-2019 22:30:25

395 Views

In this section we will see how to find the sum of digits without writing multiple statements. In other words, we will find the sum of digits in a single statement.As we know that, to find the sum of digits we cut the last digit by taking the remainder after dividing the number by 10, and then divide the number by 10 again and again until the number becomes 0.To do these task in a single statement the for loop can be used. As we know there are three different sections in the for loop. In the initialization phase we ... Read More

C++ Program to Implement the linear congruential generator for Pseudo Random Number Generation

Nancy Den
Updated on 30-Jul-2019 22:30:25

890 Views

The linear congruential generator is a very simple example of a random number generator. It is one of the oldest and best-known pseudorandom number generator algorithms. The function which is used in this method −Xn+1=(aXn + C) mod mwhere X is the sequence of pseudorandom values, andm,0

C Program to print numbers from 1 to N without using semicolon

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

949 Views

Here we will see a tricky solution of the problem. We shall print some numbers from 1 to N without using any semicolon.We can solve this problem in two different methods. The first one is the Iterative method, and second one is recursive method.Method 1The printf() function returns the length of the string so it is a non-zero value. We can perform logical AND with the condition to print the result. Then increase the value of the counter.Example Code#include #define N 20 int main(int num, char *argv[]) { while (num

C++ Program to Check Whether a Directed Graph Contains a Eulerian Cycle

Nancy Den
Updated on 30-Jul-2019 22:30:25

463 Views

The Euler cycle/circuit is a path; by which we can visit every edge exactly once. We can use the same vertices for multiple times. The Euler Circuit is a special type of Euler path. When the starting vertex of the Euler path is also connected with the ending vertex of that path, then it is called the Euler Circuit.To check whether a graph is Eulerian or not, we have to check two conditions −The graph must be connected.The in-degree and out-degree of each vertex must be same.Input − Adjacency matrix of the graph.0100000100000111000000100Output − Euler Circuit FoundAlgorithmtraverse(u, visited)Input − The ... Read More

C++ Program to Check Whether a Graph is Strongly Connected or Not

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

302 Views

In directed graph components are said to be strongly connected, when there is a path between each pair of vertices in one component.To solve this algorithm, firstly, DFS algorithm is used to get the finish time of each vertex, now find the finish time of the transposed graph, then the vertices are sorted in descending order by topological sort.Input: Adjacency matrix of the graph.0011010000010000000100000Output: Following are strongly connected components in given graph −0 1 2 3 4Algorithmtraverse(graph, start, visited)Input: The graph which will be traversed, the starting vertex, and flags of visitednodes.Output: Go through each node in DFS technique and ... Read More

C++ Program to Check the Connectivity of Directed Graph Using DFS

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

490 Views

To check connectivity of a graph, we will try to traverse all nodes using any traversal algorithm. After completing the traversal, if there is any node, which is not visited, then the graph is not connected.For the directed graph, we will start traversing from all nodes to check connectivity. Sometimes one edge can have only outward edge but no inward edge, so that node will be unvisited from any other starting node.In this case the traversal algorithm is recursive DFS traversal.Input: Adjacency matrix of a graph0100000100000111000001000Output: The Graph is connected.Algorithmtraverse(u, visited)Input: The start node u and the visited node to ... Read More

C++ Program to Implement Interpolation Search Algorithm

Nancy Den
Updated on 30-Jul-2019 22:30:25

923 Views

For the binary search technique, the lists are divided into equal parts. For the interpolation searching technique, the procedure will try to locate the exact position using interpolation formula. After finding the estimated location, it can separate the list using that location. As it tries to find exact location every time, so the searching time reduces. This technique can find items easily if the items are uniformly distributed.The complexity of Interpolation Search TechniqueTime Complexity: O(log2(log2 n)) for average case, and O(n) for worst case (when items are distributed exponentially)Space Complexity: O(1)Input − A sorted list of data 10 13 15 ... Read More

C++ Program to Implement Counting Sort

Nancy Den
Updated on 30-Jul-2019 22:30:25

4K+ Views

Counting sort is a stable sorting technique, which is used to sort objects according the keys that are small numbers. It counts the number of keys whose key values are same. This sorting technique is efficient when difference between different keys are not so big, otherwise it can increase the space complexity.The complexity of counting Sort TechniqueTime Complexity: O(n+r)Space Complexity: O(n+r)Input − A list of unsorted data: 2 5 6 2 3 10 3 6 7 8Output − Array after Sorting: 2 2 3 3 5 6 6 7 8 10AlgorithmcountingSort(array, size)Input: An array of data, and the total number ... Read More

C++ Program to Implement Shell Sort

Nancy Den
Updated on 30-Jul-2019 22:30:25

1K+ Views

The shell sorting technique is based on the insertion sort. In the insertion sort sometimes we need to shift large block to insert item in the correct location. Using shell sort, we can avoid large number of shifting. The sorting is done with specific interval. After each pass the interval is reduced to make smaller interval.The complexity of Shell Sort TechniqueTime Complexity: O(n log n) for best case, and for other cases, it depends on the gap sequence.Space Complexity: O(1)Input − The unsorted list: 23 56 97 21 35 689 854 12 47 66 Output − Array after Sorting: 12 ... Read More

Advertisements