Found 7197 Articles for C++

Relational Operators on STL Array in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:39:05

343 Views

There are six relational operators for comparison of operands of the same type. These are >,

Read/Write Class Objects from/to File in C++

Sunidhi Bansal
Updated on 05-Oct-2023 01:10:37

39K+ Views

The iostream standard library has two methods cin, to accept input from standard input stream and cout to print output to the standard output stream. In this article we will learn how to read data from files into class objects and how to write data in class objects to files.Reading and writing data to and from files requires another standard library of C++ . The three main data types of fstream are −ifstream − represents input file stream and reads information from files.ofstream − represents output file stream and writes information to files.fstream − represents general file stream and has ... Read More

Count columns to be deleted to make each row sorted in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:33:43

118 Views

The abnormal behavior of C++ programs often leads to program crash. You may have encountered problems like Segmentation fault, Aborted, Floating point exception etc. Following are sample programs that may help you to understand the reasons for a C++ program crash.ExceptionsExceptions in C++ are responses of a program when it encounters an abnormal condition. The program crashes due to such exceptions if they are not handled properly using try-catch blocks. Following program crashes due to divide by zero exception −Example#include int main(){    int num1=10;    int num2=0;    int quotient=num1/num2;    printf(" Quotient is: %d", quotient);    return ... Read More

Range-based for loop in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:30:46

4K+ Views

The range based for loop is added in C++ 11 standard and is a more compact form of its traditional equivalent. The range based for loop is used to iterate over elements of a container from beginning to end. The syntax for range-based for loop is as follows −Syntaxfor( range-declaration : range-expression ) loop statementrange-declaration − it is declaration of a variable of type same as the type of elements of range-expression. Often the auto keyword is used to automatically identify the type of elements in range-expression.range-expression − any expression used to represent a sequence of elements. Also Sequence of ... Read More

Maximums from array when the maximum decrements after every access in C++

Ayush Gupta
Updated on 09-Oct-2020 09:33:33

93 Views

In this problem, we are given an array arr[]and an integer M. Our task is to create a program to find Maximums from array when the maximum decrements after every access in C++.Problem DescriptionTo find the maximum, we will find the maximum element from the array and after every retrieval and decrease it by -1, M times.Let’s take an example to understand the problem, Input: arr[] = {3, 6, 8, 9} M = 2Ouput:17Explanation1st iteration, maximum = 9, sum = 9, updated arr = {3, 6, 8, 8}2nd iteration, maximum = 8, sum = 9+8 = 17, updated arr = ... Read More

Maximum value with the choice of either dividing or considering as it is in C++ program

Ayush Gupta
Updated on 09-Oct-2020 09:34:55

224 Views

In this problem, we are given a number N. Our task is to create a program to find to Maximum value with the choice of either dividing or considering as it is in C++.Problem DescriptionTo find the maximum, we can consider any two values, either by take the value as it is or we could get the maximum value by dividing.The value could be extracted as F(N/2) + F(N/3) + F(N/4) + F(N/5).Let’s take an example to understand the problem, Input:N = 8Output:9ExplanationF(8) =F(8/2) + F(8/3) + F(8/4) + F(8/5) = F(4) + F(2) + F(2) + F(1) = 4 ... Read More

Queries for characters in a repeated string in C++

Ayush Gupta
Updated on 09-Oct-2020 09:43:56

191 Views

In this problem, we are given a string str and Q queries consisting of two values a and b. Our task is to create a program to solve Queries for characters in a repeated string in C++.Problem DescriptionTo solve each query, we need to check whether the characters at index a and b are the same and return the value accordingly.Let’s take an example to understand the problem, Input: str = “tutorialspoint”Q = 2Query = {{0, 2}, {4, 7}}Output:RepeatedNot RepeatedExplanationFor Query 1, the character at index 0 is t, and the character at index 2 is t. Both are the ... Read More

Maximum value with the choice of either dividing or considering as it is in C++

Ayush Gupta
Updated on 21-Aug-2020 05:33:51

111 Views

In this tutorial, we will be discussing a program to find maximum value with the choice of either dividing or considering as it is.For this we will be provided with an integer value. Our task is to find the maximum value with either by dividing the number into four parts recursively or choosing it as it is using the given function F(n) = max( (F(n/2) + F(n/3) + F(n/4) + F(n/5)), n).Example Live Demo#include using namespace std; //calculating the maximum result int findMaximum(int size) {    int term[size + 1];    term[0] = 0;    term[1] = 1;    int i=2;    while(i

Maximum Weight Difference in C++

Ayush Gupta
Updated on 09-Oct-2020 09:46:05

236 Views

In this problem, we are given an array arr[] and a number M. Our task is to create a program to calculate the Maximum Weight Difference in C++.Problem StatementWe will find M elements from the array such that the absolute difference between the sum and the sum of rest elements is maximum.Let’s take an example to understand the problem, Input: arr[] = {3, 1, 6, 9, 4} M = 3Ouput:15ExplanationWe will consider 4, 6, 9. The sum is 19. The absolute difference with the sum of rest numbers is|19 - 4| = 15Solution ApproachThe solution to the problem is based ... Read More

Maximum value of an integer for which factorial can be calculated on a machine in C++

Ayush Gupta
Updated on 09-Oct-2020 09:47:58

218 Views

In this problem, we need to create a program to find Maximum value of an integer for which factorial can be calculated on a machine in C++.Factorial of a number is a huge value, as it is the product of all values preceding it. And C++ can handle large values only upto a certain value by using its inbuilt function. We need to find this restriction.Solution ApproachWe will simply use the property of data types which is when the numbers exceed the maximum value a negative number is returned.We will use long long int which is the largest basic data ... Read More

Advertisements