Found 1401 Articles for C

Variable Length Arrays in C and C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:02:29

560 Views

Here we will discuss about the variable length arrays in C++. Using this we can allocate an auto array of variable size. In C, it supports variable sized arrays from C99 standard. The following format supports this concept −void make_arr(int n){    int array[n]; } int main(){    make_arr(10); }But, in C++ standard (till C++11) there was no concept of variable length array. According to the C++11 standard, array size is mentioned as a constant-expression. So, the above block of code may not be a valid C++11 or below. In C++14 mentions array size as a simple expression (not constant-expression).ExampleLet ... Read More

Uninitialized primitive data types in C/C++ Program

Arnab Chakraborty
Updated on 27-Aug-2020 13:56:57

195 Views

In this section we will see when we declare one variable that is un-initialized, which value they hold in C or C++ language. Sometimes we assume that the compiler assigns some value like 0 for int, 0.0 for float etc. But what will be for character datatype? Let us see using implementation and compile using different compilers.Example (C++)Let us see the following implementation to get better understanding − Live Demo#include using namespace std; int main() {    char char_var;    float float_var;    int int_var;    double double_var;    long long_var;    cout

Maximum distinct lines passing through a single point in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:55:56

155 Views

We are given the number N and coordinates of two points (x1, y1) and (x2, y2) for each line. The goal is to find the maximum number of lines from given lines that can pass through a single point such that no two lines cover each other, and no rotation is performed.We will represent lines as pair of (m, c) where y=mx+c and m is slope m=y2-y1/x2-x1Lines with same m are parallel given c1!=c2. We will count distinct slopes(m). For vertical lines if x1=x2, slope = INT_MAX else m.Let us understand with an example.Input Line 1 (x1, y1)=(4, 10) (x2, y2)=(2, ... Read More

Maximum given sized rectangles that can be cut out of a sheet of paper in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:53:52

298 Views

We are given the dimensions of the sheet of paper, it’s Length L, and Breadth B. Also, we are given the dimensions of a small rectangle, it’s length l, and breadth b. The goal is to find the maximum number of smaller rectangles that can be cut out of a sheet of paper.We will do following steps −Firstly, we will take horizontal alignment, lengths L and l of sheet and rectangle respectively. Start aligning the L by l and B by b and count the rectangles.Then also do the same in vertical alignments. Count again. Return the maximum value of ... Read More

Maximum number of candies that can be bought in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:50:11

655 Views

We are given an array of candies[] of length stored in ‘size’. Each element candies[i] has a number for candies of type i.The goal is to buy as many candies as possible for any amount of money. The conditions are as given −If you purchase X[i] of type i (0

Maximum number of 2×2 squares that can be fit inside a right isosceles triangle in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:48:50

581 Views

We are given a right isosceles triangle. Isosceles triangle is the one which has two sides of equal length. Right triangle is the one which has height(ag in fig.) and base (dg in fig.) perpendicular to each other. The goal is to find the maximum number of squares that can fit into this right isosceles triangle of side 2 sq units. The sides base or height (both equal) are taken as input. No. of squares is output.Refer to the below figure to understand the problemThe given triangle of height ag and base gd has 3 squares of side 2 each. ... Read More

Count number of 1s in the array after N moves in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:37:01

349 Views

We are given an array of size N. The array has all 0’s initially. The task is to count the no. of 1’s in the array after N moves. Each Nth move has a rule associated. The rules are −1st Move − Change element at positions 1, 2, 3, 4…………..2nd Move − Change element at positions 2, 4, 6, 8…………..3rd Move − Change element at positions 3, 6, 9, 12…………..Count the number of 1’s in the last array.Let’s understand with examples.Input Arr[]={ 0, 0, 0, 0 } N=4Output Number of 1s in the array after N moves − 2Explanation − Array after ... Read More

Maximum binomial coefficient term value in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:33:47

125 Views

We are given with a positive integer ‘N’. We have to find the maximum coefficient term in all binomial coefficients.The binomial coefficient series is nC0, nC1, nC2, …., nCr, …., nCn-2, nCn-1, nCnfind the maximum value of nCr.nCr = n! / r! * (n - r)!Input − N=4Output − Maximum Coefficient − 6Explanation − 4C0= 1, 4C1 = 4, 4C2 = 6, 4C3 = 4, 4C4 = 1Therefore, the maximum coefficient is 6 in this case.Input − N=5Output − Maximum Coefficient − 10Explanation − 5C0= 1, 5C1 = 5, 5C2 =10, 5C3 = 10, 5C4 = 5, 5C5 = 1Therefore, ... Read More

Maximize the difference between two subsets of a set with negatives in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:32:06

140 Views

We are given with an array of positive and negative integers. The task is to find the maximum difference between positive and negative subsets of elements present in the array. As we have subsets of positive and negative numbers. Then the difference (sum of positives) - (sum of negatives) will always be maximum. This is because subtracting negatives will add them. Converting all negatives into positive and adding all the elements of the array will produce the desired result. Let us see examples for understanding −Input − Arr[] = { -2, 0, -3, 8, 10, 12, -4 }Output − Maximized ... Read More

Maximum and minimum of an array using minimum number of comparisons in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:30:24

12K+ Views

We are given with an array of integers. The task is to find the minimum and maximum element of the array in the minimum number of comparisons.Input Arr[] = { 1, 2, 4, 5, -3, 91 }Output Maximum element : 91 Minimum Element : -3Explanation − Here to minimize the number of comparisons, we will initialize the maximum and minimum element with Arr[0]. And starting from the 2nd element compare each value with min and max and update accordingly.Input Arr[] = { 10, 20, 21, 31, 18, 11 }Output Maximum element : 31 Minimum Element : 10Explanation − Here also, to minimize the number ... Read More

Advertisements