C Articles - Page 95 of 134

A Puzzle on C/C++ R-Value Expressions?

Arnab Chakraborty
Updated on 31-Jul-2019 12:08:17

185 Views

Here we will see one puzzle. Suppose there is a program which is given as below, we have to tell what will be the output and why?Example#include using namespace std; int main() {    int x = 0xab;    ~x;    cout

A Pancake Sorting Problem?

Arnab Chakraborty
Updated on 31-Jul-2019 12:06:03

230 Views

Here we will see another sorting problem named Pancake sort. This problem is simple. We have one array. We have to sort this. But we can use only one operation called rev(arr, i). This will reverse the elements of arr from 0 to ith position.This idea is like the selection sort. We repeatedly place the max element at end reduce the size of the array. Let us see the algorithm to get the idea.AlgorithmpancakeSort(arr, n)Begin    size := n    while size > 1, do       index := index of max element in arr from [0 to size ... Read More

A nested loop puzzle?

Arnab Chakraborty
Updated on 31-Jul-2019 12:03:12

452 Views

In this section we will see one interesting problem. We will see two code segments. Both are with two nested loops. We have to identify which one will run faster. (We will assume that the compiler is not optimizing the code).Segment 1for(int i = 0; i < 10; i++){    for(int j = 0; j

A matrix probability question ?

Arnab Chakraborty
Updated on 31-Jul-2019 12:01:37

212 Views

Here we will see one matrix probability problem. We have one rectangular matrix. We can move four directions from the current cell with equal probability. These four directions are left, right, up and down. We have to calculate the probability after N moves from position M[i, j].Here we will do something related to DFS. We will traverse recursively traverse in each of the four possible rooms from the current room. Then we will calculate the probability with one less move. As each of the four directions has equal probability, then each direction will contribute 0.25 of total probability. If we ... Read More

Print the given pattern recursively

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

4K+ Views

Here, as per the given problem pattern needs to be displayed using recursive approach.Recursive function is the one that calls itself n number of times. There can be ‘n’ number of recursive function in a program. The problem working with recursive function is their complexity.AlgorithmSTART Step 1 -> function int printpattern(int n)    If n>0       Printpattern(n-1)       Print *    End IF End Step 2 -> function int pattern(int n)    If n>0       pattern(n-1)    End IF    Printpattern(n)    Print End STOPExample#include int printpattern(int n) {    if(n>0) { ... Read More

Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

126 Views

Given with equation program must find the value of ‘a’ where a+b Declare start variables b=10, x=9, n=40 and flag=0, divisible Step 2 -> Loop For divisible = (b / x + 1 ) * x and divisible = 1       Print divisible-1       Set flag=1    End END STOPExample#include int main(int argc, char const *argv[]) {    int b=10, x=9, n=40, flag = 0;    int divisible;    for (divisible = (b / x + 1 ) * x ; divisible = 1) {          printf("%d ", divisible - b );          flag = 1;       }    }    return 0; }Outputif we run above program then it will generate following output8 17 26

Print n smallest elements from given array in their original order

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

259 Views

Given with array of let’s say k elements the program must find the n smallest elements amongst them in their appearing order.Input : arr[] = {1, 2, 4, 3, 6, 7, 8}, k=3 Ouput : 1, 2, 3 Input k is 3 it means 3 shortest elements among the set needs to be displayed in original order like 1 than 2 and than 3AlgorithmSTART Step 1 -> start variables as int i, max, pos, j, k=4 and size for array size Step 2 -> Loop For i=k and i=0 and j--       If arr[j]>max         ... Read More

Print n terms of Newman-Conway Sequence

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

414 Views

Newman-Conway Sequence is used for generating following integer sequence.1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12Formula used for generating Newman-Conway sequence for n numbers is −P(n) = P(P(n - 1)) + P(n - P(n - 1)) Where, p(1) =p(2) =1AlgorithmSTART Step 1 -> Input variable n(e.g. 20) Step 2 -> start variables as i, p[n+1], p[1]=1, p[2]=1 Step 3 -> Loop For i=3 and i End Loop For STOPExample#include int main() {    int n = 20,i;    int p[n + 1];    p[1] = 1;    p[2] = 1;    printf("Newman-Conway Sequence is :");    printf("%d %d ",p[1],p[2]);    for (i = 3; i

Print the kth common factor of two numbers

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

347 Views

Given with two numbers x and y the output should contain their kth common factor.Input: x=9 y=18 k=1 Output : k common factor = 2 Factors of 9 : 1, 3, 9 Factors of 18 : 1, 2, 3, 6, 9, 18 Greatest Common Factor : 9AlgorithmSTART Step 1 -: take input as x and y lets say 3 and 21 and k as 1 Step 2 -: declare start variables as int i,num,count=1 Step 3 -: IF x

Print N lines of numbers such that every pair among numbers has a GCD K

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

221 Views

GCDGCD stands for Greatest Common Divisor of two or more integers excluding 0Like, to find the greatest common divisor of 48 and 18048 = 2 × 2 × 2 × 2 × 3180 = 2 × 2 × 3 × 3 × 5Greatest common divisor = 2 × 2 × 3 = 12.In the given problem, N lines should be printed with elements have GCD as specifiedInput : N=2 GCD=2 Ouput : 2-4-6-10 14-16-18-22AlgorithmSTART Step 1 -> take input n(e.g. 2) and k(e.g. 2) as int values and i Step 2-> Loop For i to 0 and i end loop ... Read More

Advertisements