Home Misc
Category:

Misc

View Miscellaneous problems

  • Linked ListMisc

    Reverse a Linked List in pairs.

    by nikoo28
    1 minutes read

    Question: Write a program to reverse a Linked List in pairs. That means we should reverse the 1st and 2nd numbers, 3rd and 4th numbers and so on. Input: 4, 8, 15, 16, 23, 42 Output: 8, 4, 16, 15, 42, 23 The basic idea behind solving this problem is that we need to traverse the entire list and we need to keep on swapping the values of 2 adjacent nodes. Since we need to reverse the values in pairs, it is also clear that we need to move 2 …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: In C you can do something like:- char s[] = “Hello World”; and char *s = “Hello World”; What is the difference between the two of them? What happens to the memory during compile time and run-time? This declaration: Creates ONE object – a char array of size 6, called s, initialized with the values ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’. Where this array is allocated in memory, and how long it lives for, depends on where the declaration appears. If the declaration is within a function, it will …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    How to use boolean type in C?

    by nikoo28
    1 minutes read

    Question: C does not have the boolean data type by default. Is there a way by which we can implement it? In C language, we do not have the boolean datatype by default. That means we cannot do something like We can do this in C++ and JAVA. However, we have an alternative method to achieve the same functionality. Before moving on to the implementation it is important to know that in C, if a value is ‘0 (zero)’ it is treated as false, and any other value other than …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: What is the difference between doing:         ptr = (char **) malloc (MAXELEMS * sizeof(char *)); or:         ptr = (char **) calloc (MAXELEMS, sizeof(char*)); When is it a good idea to use calloc and when to use malloc ? There are two differences:- Firstly it is in the number of arguments, malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. calloc() allocates a memory area, the length …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is an unsigned char?

    by nikoo28
    1 minutes read

    Question: In C, what is an unsigned char used for? How is this different from a regular char? In C, there are three distinct character types: char signed char unsigned char If you are using character types for text, use the unqualified char: it is the type of character literals like ‘a’ or ‘0’. it is the type that makes up C strings like “abcde” It also works out as a number value, but it is unspecified whether that value is treated as signed or unsigned. Beware character comparisons through …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop ? i++ is known as Post Increment whereas ++i is called Pre Increment. ++i will increment the value of i, and then return the incremented value. i++ will increment the value of i, but return the original value that i held before being incremented. For a for loop, either works. ++i seems more common, perhaps because that is what is used in Dennis Ritchie. In …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: How can you divide a number by 3 without using *, /, +, – % operator? Input: 48 Output: 16 The method below implements the method using bit-wise operators. For example if your number is 10 then convert to binary 10 =>00001010 if 10 > 3 then shift the binary number 2 bits Now num will be 00000010 ie, 2 Now sum will be 2 num = (shift the number by 2 bits)+(number BITWISE AND 3) num = 2+2 Now the number will be 4 then, 4>3 => true …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Given a number ‘n’, how to check if n is a Fibonacci number. A simple way is to generate Fibonacci numbers until the generated number is greater than or equal to ‘n’. Following is an interesting property about Fibonacci numbers that can also be used to check if a given number is Fibonacci or not. A number is Fibonacci if and only if one or both of 5n2+4 or 5×2-4 is a perfect square (Source: Wikipedia). Following is a simple program based on this concept. Output:- 1 is a Fibonacci …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More