Found 7346 Articles for C++

Why is it considered a bad practice to omit curly braces in C/C++?

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

726 Views

In C++ we can omit the curly braces after if-else statements, or after any loop. If we do not use curly braces then only one statement after the if-else or loop will be considered under that block. For example −if(condition) {    Line 1    Line 2 } if(condition) Line 1 Line 2In the first case, the Line1 and Line2 both are in the if block. But in the second condition, the Line1 is in if block but Line2 is not in if block. So we can omit curly braces only there is a single statement under if-else or ... Read More

Why would we call cin.clear() and cin.ignore() after reading input in C++?

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

1K+ Views

In C++ the cin is used to take input from user. Sometimes for some reasons some error flags are set. In that time the cin does not take any input. Sometimes it takes some other characters. So if we clear the cin, then the error flags are reset. Then we can use getline(), get() etc. functions.The ignore() function is another stream input function. If we write the function like thiscin.ignore(1000, ‘’)Then it will ignore next 1000 characters, otherwise ignore lines until ‘’ is found.

C++ Program to Print only Odd Numbered Levels of a Tree

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

121 Views

This is a C++ program to print only Odd Numbered Levels of a Tree.AlgorithmStructure and functions with pseudocodes:Begin    Declare nod as a structure.       Declare d of integer datatype.       Declare a pointer l against struct nod.       Declare a pointer l against struct nod.    Call function struct nod* newNod(int d).    Declare struct nod* newNod(int d) function.       Declare a pointer node against struct node.       Initialize node = (struct nod*) malloc(sizeof(struct nod)).       node->d = d       node->l = NULL     ... Read More

Differences between pass by value and pass by reference in C++

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

17K+ Views

In C++ we can pass arguments into a function in different ways. These different ways are −Call by ValueCall by ReferenceCall by AddressSometimes the call by address is referred to as call by reference, but they are different in C++. In call by address, we use pointer variables to send the exact memory address, but in call by reference we pass the reference variable (alias of that variable). This feature is not present in C, there we have to pass the pointer to get that effect. In this section we will see what are the advantages of call by reference ... Read More

What do single quotes do in C++ when used on multiple characters?

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

779 Views

In C++ thIn C++ the double quotes are used as string literals, and single quote with one character is used as character literals. Now here we will see what will be the output if we try to print a multi-character string using the single quote.Example Code Live Demo#include using namespace std; main() {    cout

Why does std::getline() skip input after a formatted extraction?

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

385 Views

The std::getline() skips input after some formatted extraction. We can simply check this error from the following code −Example Code#include #include using namespace std; int main(){    string name;    string city;    if (cin >> name && getline(cin, city)){       cout

C++ Program to Perform Right Rotation on a Binary Search Tree

George John
Updated on 30-Jul-2019 22:30:25

632 Views

A Binary Search Tree is a sorted binary tree in which all the nodes have following two properties −The right sub-tree of a node has a key greater than to its parent node's key.The left sub-tree of a node has a key less than or equal to its parent node's key.Each node should not have more than two children.Tree rotation is an operation that changes the structure without interfering with the order of the elements on a binary tree. It moves one node up in the tree and one node down. It is used to change the shape of the ... Read More

C++ Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree

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

440 Views

If a binary tree is traversed post-order, the left subtree is visited first, then the right sub-tree and later the root. This is a C++ Program for Post Order Tree traversal without recursion. We do the program here by using stack.AlgorithmFor postorder traversal:Begin    Declare postorder_traversal(struct node*t, struct tree**top)       if(t==NULL) then          print “Empty Tree”.          Return.       Print “Postorder Data Using Stack :”.       Call push(t, top) function to insert values.       Declare a pointer store against tree structure.          Initialize ... Read More

C++ Program to Perform Left Rotation on a Binary Search Tree

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

2K+ Views

A Binary Search Tree is a sorted binary tree in which all the nodes have following two properties − The right sub-tree of a node has all keys greater than to its parent node's key. The left sub-tree of a node has all keys less than to its parent node's key. Each node should not have more than two children. Tree rotation is an operation that changes the structure without interfering with the order of the elements on a binary tree. It moves one node up in the tree and one node down. It is used to change the shape of the tree, ... Read More

C++ Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

556 Views

If a binary tree is traversed in-order, the left subtree is visited first, then the root and later the right sub-tree. The output the key in ascending order in in_order traversal. This is a C++ Program for Inorder Tree Traversal without Recursion.AlgorithmBegin    Declare a structure n.       Declare d of the integer datatype.       Declare a pointer l against structure n.       Declare a pointer r against structure n.       Declare a constructor of structure n.          Pass an integer variable d to parameter.         ... Read More

Advertisements