Found 7347 Articles for C++

Are array members deeply copied in C++?

Arnab Chakraborty
Updated on 29-Jan-2020 08:12:50

816 Views

In case of C/C++, we can be able to assign a struct (or class in C++ only) variable to another variable of same type. At the time when we assign a struct variable to another, all members of the variable are copied to the other struct variable. In this case the question is arisen what happens when the structure consists of an array?Now, we have to discuss about arrays. Main point to note is that the array members is not copied as shallow copy; compiler automatically accomplishes Deep Copy in case of array members. In the below program, struct test ... Read More

Angle between a chord and a tangent when angle in the alternate segment is given in C++?

Arnab Chakraborty
Updated on 29-Jan-2020 08:06:13

86 Views

In case of a given circle, chord and tangent is met at a particular point. The angle in the alternate segment is provided. The main job here is to find the angle between the chord and the tangent.ExamplesInput: z = 40 Output: 40 degrees Input: z = 60 Output: 60 degreesApproachLet, angle QPR is the given angle in the alternate segment.Let, the angle between the chord and circle = angle RQY = aBecause line drawn from center on the tangent is perpendicular, So, angle CQR = 90-aAs, CQ = CR = radius of the circleSo, angle CRQ = 90-aNow, in ... Read More

An in-place algorithm for String Transformation in C++

Arnab Chakraborty
Updated on 29-Jan-2020 08:09:55

99 Views

For a given string, transfer all even positioned elements to end of string. While transferring elements, keep the relative order of all even positioned and odd positioned elements same.For example, if the given string is "a1b2c3d4e5f6g7h8i9j1k2l3m4”, transform it to “abcdefghijklm1234567891234” in-place and in O(n) time complexity.Following are the stepsCut out the highest prefix sub-string of size of the form 3^k + 1. In this step, we locate the highest non-negative integer k such that 3^k+1 is less than or equal to n (length of string)Implement cycle leader iteration algorithm ( it has been explained below ), starting with index 1, ... Read More

An application on Bertrandís ballot theorem in C/C++

Arnab Chakraborty
Updated on 29-Jan-2020 07:51:43

123 Views

In Bertrand's original paper, he explains a proof depended on a general formula for the number of favourable sequences implementing a recursion relation.ExampleLet there are 5 voters, of whom 3 vote for candidate A and 2 vote for candidate B (so p = 3 and q = 2). Ten possibilities are exist for the order of the votes cast −AAABBAABABABAABBAAABAABBAABABABAABAABBAABABAABBAAAFor the order AABAB, the tally of the votes as the election progresses is given below −CandidateAABABA12233B00112For each column the tally for A is always greater than the tally for B so the A is always strictly ahead of B. For ... Read More

Alternate vowel and consonant string in C++

Arnab Chakraborty
Updated on 29-Jan-2020 07:47:42

150 Views

In case of a given string, rearrange characters of the given string so that the vowels and consonants occupy alternate position. If string cannot be rearranged in proper way, display “no such string”. Order of vowels with respect to each other and the order of consonants with respect to each other should be preserved.If more than one needed strings can be constructed, display the lexicographically smaller.ExamplesInput : Tutorial Output : Tutorila Input : onse Output : noseThere exist two possible outcomes "nose" and "ones". Since "nose" is lexicographically smaller, we display it.Number of vowels and consonants in given string is ... Read More

Converting Decimal Number lying between 1 to 3999 to Roman Numerals in C++

Ayush Gupta
Updated on 29-Jan-2020 07:45:23

297 Views

In this tutorial, we will be discussing a program to convert decimal number lying between 1 to 3999 to roman numerals.For this we will be provided with a random integer. Our task is to convert the given number into its roman numeral equivalent.Example Live Demo#include using namespace std; //converting decimal to roman numeral int printRoman(int number){    int num[] = {1,4,5,9,10,40,50,90,100,400,500,900,1000};    string sym[] =    {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};    int i=12;    while(number>0){       int div = number/num[i];       number = number%num[i];       while(div--){          cout

Convert to number with digits as 3 and 8 only in C++

Ayush Gupta
Updated on 29-Jan-2020 07:44:12

48 Views

In this tutorial, we will be discussing a program to convert a number to have digits as 3 and 8 only.For this we will be provided with a random number. Our task is to convert its digits to be only 3 and 8 by either adding/subtracting 1 from the number or converting digits of the number to any desired digit.Example Live Demo#include using namespace std; //calculating minimum operations required int cal_min(long long int num){    //calculating remainder and operations    int rem;    int count = 0;    while (num) {       rem = num % 10;   ... Read More

Convert to Strictly increasing integer array with minimum changes in C++

Ayush Gupta
Updated on 29-Jan-2020 07:43:14

127 Views

In this tutorial, we will be discussing a program to convert to strictly increasing integer array with minimum changes.For this we will be provided with an array. Our task is to change the elements of the array to be in strictly increasing order by minimum number of changes in the elements.Example Live Demo#include using namespace std; //calculating number of changes required int remove_min(int arr[], int n){    int LIS[n], len = 0;    for (int i = 0; i < n; i++)    LIS[i] = 1;    for (int i = 1; i < n; i++) {       ... Read More

Converting one string to other using append and delete last operations in C++

Ayush Gupta
Updated on 29-Jan-2020 07:49:47

87 Views

In this tutorial, we will be discussing a program to convert one string to other using append and delete last operations.For this we will be provided with two strings. Our task is to calculate whether the first string can be converted into the second one by performing k operations of append and delete last element.Example#include using namespace std; //checking if conversion between strings is possible bool if_convert(string str1, string str2, int k){    if ((str1.length() + str2.length()) < k)    return true;    //finding common length of both string    int commonLength = 0;    for (int i = ... Read More

Converting Roman Numerals to Decimal lying between 1 to 3999 in C++

Ayush Gupta
Updated on 29-Jan-2020 07:40:25

275 Views

In this tutorial, we will be discussing a program to converting roman numerals to decimal lying between 1 to 3999.For this we will be provided with a random roman numeral. Our task is to convert the given roman numeral into its decimal equivalent.Example Live Demo#include using namespace std; //calculating the decimal value int value(char r){    if (r == 'I')    return 1;    if (r == 'V')    return 5;    if (r == 'X')    return 10;    if (r == 'L')    return 50;    if (r == 'C')    return 100;    if (r == 'D')   ... Read More

Advertisements