A structure in C++ is a user-defined data type, which is used to group and store variables of different data types under a single name. In this article we will see how to sort an array of structures using conditions on member variables. For this we will be using the sort() function which is defined under header file. Syntax Here is the following syntax of sort() function. sort(start_index, end_index, comparison_function); start_index is an iterator (or pointer) to the first element of the array of structures.end_index is an iterator (or pointer) to one past the last element (means last_index + ... Read More
JavaScript Program for the Number of local extrema in an array is a common problem in the field of computer science and data analysis. Local extrema are the peaks and valleys in a sequence of numbers. In this article we are having two arrays, our task is to write a Javascript program for number of local extrema in an array. Example Input: arr1 = [1, 2, 3, 2, 1, 4, 5, 6, 5, 4, 3, 2, 1]; First local extrema: 3, {2 < 3 > 2} Second local extrema: 1, {2 > 1 < 4} Third local ... Read More
A sorted and rotated array is an array that is sorted in ascending or descending order and then rotated either left or right by a specific number of elements. There should exist exactly one pivot point around which the array is rotated. In this article, our task is to find the maximum element in the given sorted and rotated array. We will use the following two approaches mentioned below: Using Linear Search Using Binary Search Example Here is an example of ... Read More
A sorted and rotated array is an array that is sorted in ascending or descending order and then rotated either left or right by a specific number of elements. There should exist exactly one pivot point around which the array is rotated. In this article, our task is to find the minimum element in the given sorted and rotated array. We will use the following two approaches mentioned below: Using Linear Search Using Binary Search Example Here is an example of searching the ... Read More
The subsets of a set refers to all the possible combinations of choosing elements from the set. For example, if we have a set {1, 2}, the subsets are {}, {1}, {2}, and {1, 2}. In this article, we will learn how to generate subsets of a set using the Binary Counting Method in C++. // Set of elements int arr[] = {1, 2, 3}; // Subsets of the above set {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} Binary Counting Method The binary counting method is a technique used to generate ... Read More
In this section, we will see how to remove some characters from a string in C++. In C++ we can do this task very easily using erase() and remove() function. The remove function takes the starting and ending address of the string, and a character that will be removed. // Input String = "Hello TutorialsPoint" Char = 'o' // Output String = "Hell TutrialsPint" Algorithm to Remove Certain Characters from a String Here is the algorithm to remove certain characters from a string in C++: Step 1: Take a string and a character to be removed ... Read More
In this article, we will learn an uncommon representation of array elements in both C and C++. Example of Uncommon Representation of Array Elements Consider following code in C/C++, that is trying to print an element from an array. C C++ #include int main() { int arr[2] = {0,1}; printf("First Element = %d",0[arr]); } Output The output of above program will be: First Element = 0 #include using namespace std; int main() { int arr[2] = {0,1}; cout
In C/C++, both ternary operator and if-else statements are conditional statements: A condition statement expresses the relationship between two ideas or events. Where one is dependent on the other. If the condition is true, the if statement will return; otherwise, the else statement will return. Ternary Operator We know that the ternary operator is the conditional operator. Using this operator, we can check some conditions and do some tasks according to those conditions. Without using the ternary operator, we can also use the if-else conditions to do the same. Following is The Syntax of the Ternary Operator: condition ? expression_if_true ... Read More
In this article, we Implement the C program to find the sum of two numbers without using any Operator. Find Sum of Two Numbers Without Using Any Operator in C This problem is tricky. To solve this problem we are using the minimum width features of the printf() statement. We can use '*' which indicates the minimum width of output. For example, in this statement "printf("%*d", width, num);", the specified 'width' is substituted as *, and 'num' is printed within the minimum width specified. If the number of digits in 'num' is less than the provided 'width', the output will ... Read More
In the C programming language, the printf() function is used to print ("character, string, float, integer, octal, and hexadecimal values") to the output screen. To demonstrate the value of an integer variable, we use the printf() function with the %d format specifier. Let's see a C statement and guess the result: printf("%d %d %d", i, ++i, i++); Here, in the above C statement, both 'i' and 'i++' are in the argument list; this statement causes undefined behaviour. The sequence in which the arguments are evaluated is not specified; orders may alter depending on the compiler. At different times, a ... Read More