Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Sorting Searching Code Examples

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

/* C++ Program to Create the Prufer Code for a Tree - This algorithm generates a prufer code for the given tree. - For a given tree of v vertexes, a prufer code is a unique sequence of v-2 vertex indexes. - The time complexity to generate this code is O(v*e). - This algorithm takes the input of the number of vertexes for the tree. - Then it takes the input if vertex pairs which have an edge between them. - To generate prufer code it removes the leaf vertex which has lowest index value. - It simultaneously adds the other vertex to the prufer code from which this leaf vertex was connected. - This process is done till two vertex remains in the tree. - Exit. */ #include<iostream> using namespace std; int main() { int i, j, v, e, min, x; // Take the input of the number of vertexes of the tree. cout<<"Enter the number of vertexes of the tree: "; cin>>v; // Calculate the number of edges in the tree as 'v-1'. e = v-1; int edge[e][2], deg[v+1] = {0}; cout<<"\nFor "<<v<<" vertexes this connected tree must have exactly "<<e<<" edges.\n"; // Enter the vertex pairs which have an edge between them. cout<<"\nEnter "<<e<<" pair of vertexes for the tree.\n"; for(i = 0; i < e; i++) { cout<<"Enter the vertex pair for edge "<<i+1<<":"; cout<<"\nV(1): "; cin>>edge[i][0]; cout<<"V(2): "; cin>>edge[i][1]; deg[edge[i][0]]++; deg[edge[i][1]]++; } // Print the prufer code of the given tree. cout<<"\nThe Prufer code for the given tree is: { "; for(i = 0; i < v-2; i++) { min = 10000; // Select the vertex which have lowest index and degree as 1. for(j = 0; j < e; j++) { if(deg[edge[j][0]] == 1) { if(min > edge[j][0]) { min = edge[j][0]; x = j; } } if(deg[edge[j][1]] == 1) { if(min > edge[j][1]) { min = edge[j][1]; x = j; } } } // Remove the selected vertex by decreasing its degree to 0. deg[edge[x][0]]--; // Decrement the degree of other vertex, since we have removed the edge. deg[edge[x][1]]--; // Print the vertex from which leaf vertex is removed. if(deg[edge[x][0]] == 0) cout<<edge[x][1]<<" "; else cout<<edge[x][0]<<" "; } cout<<"}"; return 0; }

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C++ language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant. (2D) array in C++ programming is also known as matrix. A matrix can be represented as a table of rows and columns. In C/C++, we can define multi dimensional arrays in simple words as array of arrays. Data in multi dimensional arrays are stored in tabular form (in row major order).

A relational operator is used to check the relationship between two operands. C++ Relational Operators are used to relate or compare given operands. Relational operations are like checking if two operands are equal or not equal, greater or lesser, etc. Relational Operators are also called Comparison Operators.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Write a simple C++ program which have four different functions for "basic operations" like addition, subtraction, division, multiplication. It should be menu based asking user to enter

Program display the minimum heap method of arranging elements. "Minimum Heap" is a method of "Arranging Elements" in a Binary search tree where value of the parent node is