write a program to compare 2 numbers without using logical
operators?
Answer Posted / rishabh
#include<stdio.h>
#include<limits.h>
int sign(int number)
{
return (unsigned) number / (unsigned) INT_MIN;
}
int main(int argc, char *argv[])
{
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int dif = a - b;
int sb1 = sign(dif);
int sb2 = sign(dif - 1) - sb1;
int ptr = 2 * sb2 + sb1;
char *messages[3] =
{
"%d is greater than %d",
"%d is less than %d",
"%d is equal to %d" };
printf(messages[ptr], a, b);
}
}
| Is This Answer Correct ? | 3 Yes | 4 No |
Post New Answer View All Answers
I have a varargs function which accepts a float parameter?
What is the meaning of ?
a formula,a series of steps,or well defined set of rules for solving a problem a) algorithem b) program c) erdiagram d) compiler
Why does not c have an exponentiation operator?
What is malloc() function?
What is spaghetti programming?
Is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name?
How we can insert comments in a c program?
What are header files and what are its uses in C programming?
Explain how do you list files in a directory?
What is wrong with this program statement? void = 10;
What is the purpose of void in c?
Why cant I open a file by its explicit path?
What are 'near' and 'far' pointers?
You have given 2 array. You need to find whether they will
create the same BST or not.
For example:
Array1:10 5 20 15 30
Array2:10 20 15 30 5
Result: True
Array1:10 5 20 15 30
Array2:10 15 20 30 5
Result: False
One Approach is Pretty Clear by creating BST O(nlogn) then
checking two tree for identical O(N) overall O(nlogn) ..we
need there exist O(N) Time & O(1) Space also without extra
space .Algorithm ??
DevoCoder
guest
Posted 3 months ago #
#define true 1
#define false 0
int check(int a1[],int a2[],int n1,int n2)
{
int i;
//n1 size of array a1[] and n2 size of a2[]
if(n1!=n2) return false;
//n1 and n2 must be same
for(i=0;i