Travelling Salesman Problem: NP-Hard Proof

Tapas Kumar Ghosh
Updated on 11-Apr-2025 18:22:50

5K+ Views

The Traveling Salesman Problem (TSP) involves a salesman who must start at a given location, visit every other city exactly once, and then return to the starting point. The goal of TSP is to find the shortest path that minimizes the total distance traveled. Travelling Salesman Problem : NP−Hard This problem is classified as NP-hard, which indicates that it cannot be solved in polynomial time. NP-hard refers to a class of problems for which no efficient solution exists. A simple example of an NP-hard problem is the subset sum problem. To better understand how a salesman travels to multiple ... Read More

Size of Void Pointer in C/C++

Tapas Kumar Ghosh
Updated on 11-Apr-2025 18:20:24

8K+ Views

The size of void pointer varies system to system. If the system is 16-bit, size of void pointer is 2 bytes. If the system is 32-bit, size of void pointer is 4 bytes. If the system is 64-bit, size of void pointer is 8 bytes. Finding Size of a Void Pointer  To find the size of a void pointer, you can use the sizeof() operator that accepts a data type, variable name, or any value and returns its size. In this case, you need to pass the name of the void pointer. Syntax Following is the basic syntax of void ... Read More

Octal to Binary Converter in C++

Tapas Kumar Ghosh
Updated on 11-Apr-2025 18:14:50

1K+ Views

In a computer system, the binary number is expressed in the binary numeral system while the octal number is in the octal numeral system. The binary number is in base 2 while the octal number is in base 8. Here, we provide the tabular data to understand binary numbers and their corresponding octal numbers as follows: Octal Number ... Read More

Understanding cin.clear() and cin.ignore() in C++

Revathi Satya Kondra
Updated on 11-Apr-2025 17:33:40

1K+ Views

When we attempt to work with user input in C++, unwanted behavior may be caused by errors or leftover characters in the input buffer. So, in that case cin.clear() and cin.ignore() are functions that can help in dealing with this kind of problem. cin.clear() cin.ignore() cin.clear and cin.ignore() cin.clear() The cin.clear() resets the error flags on the cin stream and is used when an input operation fails (e.g., entering a non-numeric value for an integer variable). Without clearing the error flags, further input operations will not work. ... Read More

Curly Braces in C++ Best Practices

Revathi Satya Kondra
Updated on 11-Apr-2025 17:32:54

1K+ Views

In C/C++, omitting the curly braces assumes that only the first statement is the block and this leads to quite a few issues during debugging, as code is pretty tough to read and comprehend. Curly braces help us prevent errors and confusion, which also helps with the flow of the program. Proper Use of Curly Braces 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. Syntax The following is the syntax: − ... Read More

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

Revathi Satya Kondra
Updated on 11-Apr-2025 17:32:01

245 Views

In C++, to print the odd-numbered levels of a binary tree, the levels are numbered from the root as Level 1. The odd-numbered levels are Level 1, Level 3, and so on. This program prints the nodes present at these odd levels. It uses level-order traversal to process the tree level by level and prints the nodes found at these levels. Algorithm to Print only Odd Numbered Levels of a Tree Following is the Algorithm to print odd numbered levels of a tree − Create a structure for tree nodes with data and pointers to left ... Read More

Size of an Empty Class in C++

Samual Sam
Updated on 11-Apr-2025 17:17:28

380 Views

The size of an object of an empty class in C++ is 1 byte as it allocates one unique address to the object in the memory. The size can not be 0, as the two objects can not have same memory allocation. In this article, we will see an example of checking the size of an object of an empty class in C++. Demonstrating Size of an Empty Class In this example, we have two C++ classes. One class is an empty class while other is not an empty class. We have printed the size of objects of both the ... Read More

C++ Program to Convert Fahrenheit to Celsius

Ravi Ranjan
Updated on 11-Apr-2025 17:17:03

4K+ Views

To convert fahrenheit to celsius, use a simple mathematical formula. In this article, we are going to discuss the conversion formula, a simple example, and an easy example code. We have been given a temperature in fahrenheit and our task is to convert the given temperature into celsius using C++. Understanding Fahrenheit to Celsius Formula The formula for converting the given temperature from fahrenheit to celsius is as follows: celsius = (fahrenheit - 32.0) * 5.0 / 9.0; Let's see an example to convert the temperature given in fahrenheit to celsius using the above formula: Input: Temperature in ... Read More

Find GCD and LCM of N Numbers in C++

Ravi Ranjan
Updated on 11-Apr-2025 17:15:30

4K+ Views

The gcd refers to 'Greatest Common Divisor', i.e. greatest common number which can divide all the given numbers. The lcm refers to the 'Least Common Multiple' i.e. the lowest common multiple of all the numbers. To find the gcd and lcm of n numbers in C++, we can use various approaches like iterative approach, or built-in C++ functions. In this article, we are having 'n' number of elements, our task is to find the gcd and lcm of n number of elements using C++. Example Here is an example of GCD and LCM of 4 numbers: Input: Numbers = ... Read More

C++ Program to Generate Random Number

Ravi Ranjan
Updated on 11-Apr-2025 17:14:54

4K+ Views

Random numbers are the numbers that are unpredictable and we do not use any pattern or algorithm for choosing them. They are used to bring randomness such as while shuffling data or used in games. To generate a random number in C++, the built-in functions from different libraries such as or can be used. In this article, we will understand four different approaches for generating a random number. The approaches are listed below. Using rand() with srand() Functions Using rand() with Range Limit Using random_device ... Read More

Advertisements