Some macros

#define SQR(x) ((x)*(x))

#define MAX(a,b) ( (a) > (b) ? (a) : (b)  )

#define ISLP(y)  (  (y % 400 == 0)  || (y %100 != 0 && y%4 == 0)  )

 #define ISLOWER(a)  (a>=97 && a<=127)

#define TOLOWER(a)  (a - 32)

Using define without assigning value

#include
#define NO
#define YES

int main()
{
    int i = 5,j;
    if (i>5)   j=YES;
    else j=NO;

    printf("%d”,j);
}

Output: Expression syntax in function main
Explanation: Because when assigning j with YES or NO we don’t know what is the value of YES or NO.

To determine whether machine is Little-Endian and Big-Endian?

Problem Write a program to find whether a machine is big endian or little endian. Solution What Little-Endian and Big-Endian? How can I determine whether a machine’s byte order is big-endian or little endian? How can we convert from one to another? First of all, Do you know what Little-Endian and Big-Endian mean? Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. [Read More]

Calculate pow function

Lets look at some solutions. Solution 1 - Using recursion int pow(int x, int y) { if(y == 1) return x ; return x \* pow(x, y-1) ; } Divide and Conquer C program /\* Function to calculate x raised to the power y \*/ int power(int x, unsigned int y) { if( y == 0) return 1; else if (y%2 == 0) return power(x, y/2)\*power(x, y/2); else return x\*power(x, y/2)\*power(x, y/2); } /\* Program to test function power \*/ int main() { int x = 2; unsigned int y = 3; printf("%d", power(x, y)); getchar(); return 0; } Time Complexity: O(n) [Read More]

Swapping 2 variables using macro

Problem

How to swap 2 variables using macro?

 Solution

 #define swap(type,a,b) type temp;temp=a;a=b;b=temp;  

Now, think what happens if you pass in something like this

 swap(int,temp,a) //You have a variable called "temp" (which is quite possible).  

This is how it gets replaced by the macro

int temp;  
temp=temp;  
temp=b;  
b=temp;  

Swap two number in place without temporary variables.

Problem Write a function to swap two number in place without temporary variables. Solution Method1 - The XOR or Exclusive trick In C this should work: a ^= b ^= a ^= b; to simplify : a=a^b; b=a^b; a=a^b; OR a^=b; b^=a; a^=b; Following are operations in XOR 0^0=0 0^1=1 1^0=1 1^1=0 Hence, we have: a=a^b: ‘a’ will save all the bits that a differs from b: if the bit that ‘a’ and ‘b’ differ, it gets 1, otherwise 0. [Read More]

3n+1 problem

The Problem Consider the following algorithm: 1. input n 2. print n 3. if n = 1 then STOP 4. if n is odd then n = 3*n+1 5. else n = n/2 6. GOTO 2 Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. [Read More]

jelly beans

Problem: you have three jars that are all mislabeled. one contains peanut butter jelly beans, another grape jelly jelly beans, and the third has a mix of both (not necessarily a 50/50 mix, could be a 1/99 mix or a 399/22 mix). how many jelly beans would you have to pull out, and out of which jars, to find out how to fix the labels on the jars? | | | | | | |jar 1| |jar 2| |jar 3| [Read More]