Home Author
Author

nikoo28

  • 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
  • Question: Suppose you are given a Linked List as 45-> 123-> 87-> 11-> 53-> 24-> 412-> 22. And we have to find the nth node from the end. Input:- n = 3 Output:- 24 BRUTE FORCE APPROACH:- In this method, start with the first node and count how many nodes are there after that node. If the number of nodes are < n-1, then return saying, “fewer number of nodes in the list.” If the number of nodes are > n-1 then go to the next node. Continue this until …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Linked List

    Important operations on a Linked List

    by nikoo28
    6 minutes read

    We covered some of the basic operations on a Linked List in this post. Basic operations on a Linked List However, in order to create a functional Linked List, we need even more operations before we can proceed further. Some of these operations are:- Inserting a node in the beginning of the Linked List. Inserting a node in the middle of a List. Inserting at a certain position Inserting after a specific node. Deleting a node from the Linked List. Deleting the entire Linked List. Inserting a node in the …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Linked List

    Basic Operations on a Linked List

    by nikoo28
    5 minutes read

    Here are some of the basic operations on a singly Linked List. Creating a new List Adding new elements Traversing a List Printing a List Basic functions to perform the above techniques have been defined in the code below. Creating a new list A new Linked List creation means that we do not have any elements in the list and we want to start fresh. This means allocating space in the memory for a node and then inserting the data into it. Since only a single node is created, the …

    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