Programming Articles - Page 2572 of 3366

C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges

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

451 Views

This is a C++ program in which we generate a undirected random graph for the given edges ‘e’. This algorithm basically implements on a big network and time complexity of this algorithm is O(log(n)).AlgorithmBegin    Function GenerateRandomGraphs(), has ‘e’ as the number edges in the argument list.    Initialize i = 0    while(i < e)       edge[i][0] = rand()%N+1       edge[i][1] = rand()%N+1       Increment I;    For i = 0 to N-1       Initialize count = 0       For j = 0 to e-1         ... Read More

C++ Program to Perform Searching Based on Locality of Reference

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

160 Views

Searching based on locality of reference, depends upon the memory access pattern data elements are reallocated.Here linear searching method is used to search an element.AlgorithmBegin    int find(int *intarray, int n, int item)    intialize comparisons = 0    for i = 0 to n-1       Increase comparisons    if(item == intarray[i])       Print element with its index    break    if(i == n-1)       Print element not found    return -1    Print Total comparisons    For j = i till i>0       intarray[j] = intarray[j-1]       intarray[0] = ... Read More

Bind function and placeholders in C++

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

2K+ Views

Here we will see the Bind function and the placeholders in C++. Sometimes we need to manipulate the operation of some functions as we need. We can use some default parameters to get some essence of manipulating.In C++11, one new feature is introduced, called the bind function. This helps us to do such manipulating in some easier fashion. To use these features, we have to use header file.Bind functions with the help of placeholders helps to determine the positions, and number of arguments to modify the function according to desired outputs.Placeholders are namespaces which detect the position of a ... Read More

Shuffle vs random_shuffle in C++

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

556 Views

Here we will see the Shuffle and random_shuffle in C++. Let us see the random_shuffle first. It is used to randomly rearrange the elements in range [left, right). This function randomly swaps the positions of each element with the position of some randomly chosen positions.We can provide some random generator function to tell which element will be taken in every case. If we do not provide some, it will use its own random generator function.Example Live Demo#include using namespace std; int myRandomGenerator(int j) {    return rand() % j; } main() {    srand(unsigned(time(0)));    vector arr;    for (int ... Read More

When should we write our own assignment operator in C++?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

471 Views

Here we will see when we need to create own assignment operator in C++. If a class do not have any pointers, then we do not need to create assignment operator and copy constructor. C++ compiler creates copy constructor and assignment operator for each class. If the operators are not sufficient, then we have to create our own assignment operator.Example Live Demo#include using namespace std; class MyClass { //no user defined assignment operator or copy constructor is present    int *ptr;    public:       MyClass (int x = 0) {          ptr = new int(x);       }    void setValue (int x) {       *ptr = x;    }    void print() {       cout

ctime() Function in C/C++

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

460 Views

The C library function char *ctime(const time_t *timer) returns a string representing the localtime based on the argument timer.The returned string has the following format − Www Mmm dd hh:mm:ss yyyy, where Www is the weekday, Mmm the month in letters, dd the day of the month, hh:mm:ss the time, and yyyy the year.The syntax is like below −char *ctime(const time_t *timer)This function takes the pointer to a time_t, which is containing the calendar time. It returns a string containing date, time info in human readable format.Example Live Demo#include #include int main () {    time_t curtime;    time(&curtime); ... Read More

isnormal() in C++

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

130 Views

In this section we will see the isnormal() function in C++. This function is present in the cmath library. This function is used to check whether a number is normal or not. The numbers that are considered as non-normal are zeros, infinity or NAN.This function takes float, double or long double values as argument. Returns 1 if the number is normal, otherwise returns 0.Example Live Demo#include #include using namespace std; int main() {    cout

Foreach in C++ and Java

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

167 Views

In C++ and Java, there are another type of loop called foreach loop. This is not present in C. This loop has introduced in C++11 and Java JDK 1.5.0. The advantage of this loop is that, it can access the elements very quickly without performing initialization, testing and increment/decrement. This loop is used to access every element in one array or some containers. This loop is known as foreach but to denote this loop we have to use ‘for’ keyword. The syntax is different than normal for and foreach.for(datatype item : Array) { }Let us see some examples of foreach ... Read More

Comparison of Exception Handling in C++ and Java

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

305 Views

The exception handling feature is present almost any object oriented languages nowadays. In C++ and Java also we can get this kind of feature. There are some similarities between exception handling in C++ and exception handling in Java, like in both languages we have to use the try-catch block. Though there are some difficulties also. These are like below −In C++, we can throw any type of data as exception. Any type of data means primitive datatypes and pointers also. In Java we can only throw the throwable objects. Subclasses of any throwable class will also be throwable.Example Live Demo#include ... Read More

Write a program that produces different results in C and C++

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

135 Views

Here we will see some program that will return different results if they are compiled in C or C++ compilers. We can find many such programs, but here we are discussing about some of them.In C and C++, the character literals are treated as different manner. In C, they are treated as int but in C++, they are treated as characters. So if we check the size using sizeof() operator, it will return 4 in C, and 1 in C++.Live Demo For C.Example Live Demo#include int main() {    printf("The character: %c, size(%d)", 'a', sizeof('a')); }Output(C)The character: a, size(4)Live Demo For ... Read More

Advertisements