The important realization for this problem is that the hour hand is always moving. In other words, at 1:30, the hour hand is halfway between 1 and 2. Once you remember that, this problem is fairly straightforward. Assuming you don’t care whether the function returns the shorter or larger angle.
Efficient code for extracting unique elements from a sorted list of array
int main()
{
int a\[10\]={1, 2, 4, 4, 7, 8, 9, 14, 14, 20};
int i;
for (i = 0;i<9;i++)
{
if (a\[i\] != a\[i+1\])
printf("%d\\n",a\[i\]);
}
return 0;
}
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal
void putlong(unsigned long x)
{
// we know that 32 bits can have 10 digits. 2^32 = 4294967296
for (unsigned long y = 1000000000; y > 0; y /= 10) {
putchar( (x / y) + '0');
x = x % y;
}
}
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]