In C++, we can read the integer values from text files using the ifstream class that allows the user to perform input operations from the files. Here, we have filename (tp.txt) that used in the program and store some integers value. 21 61 24 05 50 11 12 21 66 55 Example to Read Integers from a Text File In this example, we created one text file to save the integers values and then access those value using file handling operation. #include #include using namespace std; int main() { // Define the maximum ... Read More
In C++, the file size determines the total number of bytes. To get the file size, we can take reference of the fstream header which contains various functions such as in_file(), seekg(), and tellg(). Example Input: The file name is tp.txt content- "Hello Tutorialspoint" Output: Size of the file is 22 bytes Example Input: The file name is testfile.txt content- "Hello Tutorialspoint" Output: File size is 27 bytes Now, we will demonstrate the C++ programs of the above two examples using file handling. Getting File Size in C++ The short form of fstream header is file stream which ... Read More
The longest equivalent sublist after K increments is known as the longest contiguous portion of a list where we can make all the elements equal by performing at most K increment operations. Suppose we have a list of numbers called nums and k. Now, consider an operation where we can increment any one element once. If we can perform operations at most k times, we have to find the longest sub-list containing equal elements. Input Output Scenario Let's consider the following scenario, as we can increment 9 once and 6 four times to get the sublist [10, 10, 10], ... Read More
A Binary tree is one of the data structures in Python in which each element is known as a Node. Each node should have a minimum of two child nodes. These children nodes are called as left child and the right child. The top node of the binary tree is known as the Root Node, and the nodes with no children are called Leaf Nodes. Following is the representation of the Binary Tree in Python - 10 / \ 5 ... Read More
In this article, we will see how to generate the assembler output from C or C++ code using GCC. What is GCC The GCC, which stands for GNU Compiler Collection, is a set of compilers and development tools available for various operating systems such as Linux, Windows, and a wide variety of other OSs (operating systems). It supports mostly C and C++, but also Objective-C, Ada, Go, Fortran, and D. The Free Software Foundation (FSF) created GCC and distributed it as totally free (as in libre) software. How to Get Assembler Output The GCC has a great feature that allows ... Read More
What is Cycle Sort? Cycle sort is an in-place, unstable sorting algorithm. It is a comparison sort that is theoretically optimal in terms of the total number of writes to the original array, unlike any other in-place sorting algorithm. A sorting algorithm is in-place sorting in which the sorted items occupy the same place as the original one. Key Points of Cycle Sort Following are the key points: It is optimal in terms of number of memory writes. It minimize the number of memory write to sort (Each value is either written zero times, if ... Read More
In this article, we will implement a C++ program to find whether a number is a power of two. So, you are given a positive integer n; the task is to find if the given number is a power of 2 or not. Let's see some of the numbers that represent the power of two. 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 ... Example Scenario Let's see the following example scenario: Input: 4 Output: yes Explanation: 22 = 4 Input: 16 Output: yes Explanation: 24 = 16 Input: 5 Output: no Explanation: ... Read More
What is Linear Search? Linear search is a sequential searching technique in which we start at one end of the list or array and look at each element until the target element is found. It is the simplest searching algorithm, with O(n) time complexity. Example Scenario let's see the following example scenario: Input: arr = {5, 4, 3, 8, 10}, K = 3; Output: 3 found at index 2; Input: arr = {1, 2, 3, 4, 5}, K = 7; Output: Not Found; How Linear Search Work? Following are the steps to search an element in array or ... Read More
In this article, We implement a C++ program to print sum of series like 13 + 23 + 33 + 43 + .......+ n3 till the nth term. Given an integer n, calculate the sum of the cubes of the first n natural integers. So, we have to cube of 'n' natural integers and sum the results. Example Scenario Let's understand the following example scenario: Input: n = 4 Output: 100 Explanation: 13 + 23 + 23 + 43 Another example scenario: Input: n = 5 Output: 225 Explanation: 13 + 23 + 23 + 43 + 43 ... Read More
In C++, a Binary tree is a generalization of the Binary Search Tree (BST). A B-tree can have more than two children. It is also known as a balanced tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. It is optimized for a system that reads and writes large blocks of data. Properties of B-tree As we discussed in the introduction B-tree is self-balanced tree where a node can have multiple children. It maintains the balance by making sure that all leaf nodes are at the same level. Following is the properties ... Read More