C++ Articles

Page 314 of 597

How to append text to a text file in C++?

Revathi Satya Kondra
Revathi Satya Kondra
Updated on 12-Jun-2025 8K+ Views

Appending text means adding new content to an existing file without removing its current content. In C++, this can be done by opening the file in append mode and writing the specified content to it. Steps to Append Text to a Text File You need to follow the below steps to open a file in append mode and append the content to it: First of all, you need to include the header file. Them, create an ofstream object to write to the file. Open the file in append mode using the ios::app flag. Use the

Read More

What is the difference between a definition and a declaration in C++?

Revathi Satya Kondra
Revathi Satya Kondra
Updated on 12-Jun-2025 2K+ Views

In C++, declaration and definition are often confused. A declaration tells the compiler about the name and type, while a definition allocates memory or provides implementation. In this article, we will understand their differences with examples. What is a Declaration in C++? A declaration means (in C or C++) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user-defined type or function in your program. No space is reserved in memory for any variable in case of the declaration. Following is the syntax to ...

Read More

Count distinct elements in an array in C++

Ravi Ranjan
Ravi Ranjan
Updated on 11-Jun-2025 8K+ Views

In this article, we have an unsorted array of integers having repetitive elements. Our task is to count the distinct elements in the given array in C++. Example Here is an example of counting the unique number in the given array: Input: array = {4, 2, 32, 26, 57, 52, 40, 2, 57, 40, 33} Output: Distinct elements in the array: 8 Here is a list of approaches for counting distinct elements in the given array: Using Nested for Loop Using Sorting ...

Read More

C++ Program to Demonstrate the Implementation of 4-Color Problem

Farhan Muhamed
Farhan Muhamed
Updated on 11-Jun-2025 899 Views

In this article, we will explain the 4 color problem to color a graph and implement the backtracking algorithm to solve it in C++. The 4 Color Problem The 4-color problem states that the maximum number of colors needed to color any planar graph (or a 2D map) is four, such that no two adjacent nodes have the same color. For example, suppose that you want to color a world map, such that no two countries sharing a border have the same color. According to this theorem, the maximum number of colors needed to do this is four. Now, ...

Read More

A Sum Array Puzzle in C++?

Ravi Ranjan
Ravi Ranjan
Updated on 11-Jun-2025 349 Views

In this article, we will solve a sum array puzzle, where we have an array with n elements. We have to create another array of n elements such that the 'ith' position of the second array will hold the sum of all elements of the first array except the 'ith' element. We have one constraint, we cannot use the subtraction operator in this problem. Example Here is an example of calculating the sum of array elements except for the ith element: Input: arr[] = {5, 6, 7, 8} Output: res[] = ...

Read More

How to get the IP Address of local computer using C/C++?

Akansha Kumari
Akansha Kumari
Updated on 11-Jun-2025 10K+ Views

In this article, we will see how to get the Host name and IP address of the local system in an easier way using C and C++ program.  Getting IP Address of Local Computer For this, some standard networking functions are available in socket programming with header files like on Windows and , , on Linux. In this article we will mainly discuss three commonly used functions: Sr.No ...

Read More

Printing the correct number of decimal points with cout in C++

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 11-Jun-2025 17K+ Views

When working with floating-point numbers in C++, it's important to know how to control the number of decimal places displayed in the output. By default, cout may not always print the expected number of digits after the decimal point.How to Print Correct Number of Decimal Points with cout?To display numbers with a specific number of decimal places using cout, you can use the setprecision() function, which accepts an integer value representing the number of digits after the decimal to be printed and returns a stream manipulator that formats the number with the specified precision. The setprecision() function belongs to the ...

Read More

How to initialize const member variable in a C++ class?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 11-Jun-2025 18K+ Views

A const member variable in a C++ class is a data member whose value must be initialized during object construction and cannot be modified. Initializing Const Member Variable in C++ Class The const member variable must be initialized. To initialize a const member variable, you can use a member initializer list using a constructor, . The member initializer list is the special way to initialize the values to class variables while creating an object before the constructor block runs. Note: If the member is static and of an integer type, you can directly initialized with the class definition. ...

Read More

How to use POSIX semaphores in C language

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 11-Jun-2025 3K+ Views

The POSIX stands for Portable Operating System which was developed by IEEE (Institute of Electrical and Electronics Engineers). This is UNIX based operating system that is used for both system calls and library functions. The semaphores are used in the process of multithreading and synchronization. For eg. file sharing and memory management. It can be named or unnamed. Multithreading is the process of executing multiple tasks in the same instance of time while synchronization is used to control the thread to work in a sequential manner. It is important for data security. Using Semaphores in C language To use ...

Read More

How to use the PI constant in C++?

Aman Kumar
Aman Kumar
Updated on 10-Jun-2025 22K+ Views

The Pi is a special mathematical value, approximately 3.14159, that is often used in calculations related to circles and geometry. In C++, we need to understand how to access and use this constant in our programs. In this article, we will show how to use the PI constant in a C++ program. The PI constant is available in the cmath header file. We will explain how to use PI to calculate values like the area or circumference of a circle. How to Use Pi in C++ There are multiple ways to use the Pi constant in C++. ...

Read More
Showing 3131–3140 of 5,962 articles
« Prev 1 312 313 314 315 316 597 Next »
Advertisements