How to compare two integer number without using if-else and Equality and Relational Operators - compare

When I was have interview, A Interviewer ask me a question which how to compare two numbers without using if-else statement and Equality and Relational Operators.
I hope get a solution for me. thanks in advance.

Related

Comparison Vs Relational expressions in c++

I was recently asked a question it was
"The expression x<y is called a(n)?"
Comparison expression.
Logical expression.
Arithmetic expression.
Relational expression.
None of the above.
I thought it was a comparison expression due to < however it was a relational expression. I've tried looking up difference and such for comparison and relational but I honestly think they are the same thing.
Can someone explain to me the differences between the expressions?
The question is about Relational expressions, see http://eel.is/c++draft/expr#rel

Using C ampersand with or without brackets

Let's have two lines of code:
&car->speed
&(car->speed)
Are these two lines equivalent? Will I get in both cases address to the speed?
If they are equivalents, what is better to choose as coding convention?
Are these two lines equivalent? Will I get in both cases address to the speed?
Yes. -> has higher precedence than that of unary &, therefore &car->speed and &(car->speed) are equivalent.
If they are equivalents, what is better to choose as coding convention?
Go with second as it shows the intended behaviour that you are interested in the address of speed.
This question already asked here several times. Postfix expression operators have higher priority than unary operators. So these two expressions
&car->speed
&(car->speed)
are equivalent.
Or another similar example with other unary operator !
!car->speed
!(car->speed)
As for coding convention I would prefer
&car->speed
and if you want to ampersandify car alone, use this:
( &car )->speed

For C++ strings is using compare method or relational operators better

I am working with a lot of strings in this project and currently in my code I have a lot of if (StringA.compare(StringB) == 0). I am wondering if replacing the comparisons in the if statements with (StringA == StringB) would make the code easier to read? Also, is using the relational operators instead of the compare method preferred by c++ coders? Additionally does using the relational operators take more time to run? And if so, how much extra time?
Use relational operators with parentheses for stacking operators. Use relational operators for all situations where you do not need the result of compare, which can produce <0,0,0< results depending on the difference. Speed is equivalent.

Parse multi-stacks to find how to properly set up an equation

It has been awhile since I have used C++ I was wondering if anyone can give me a good way on how to create two stacks and then grab a user input such as: 1+6/3 and parse though this input to put the numbers in a 'number stack' and the operators in an 'operator stack', then make a priority list to make sure the proper operators are used in the equation first so the answer comes to 3. Any tips would be very much appreciated, I never use stacks.
http://en.wikipedia.org/wiki/Recursive_descent_parser
Evaluating arithmetic expressions from string in C++
What is the best way to evaluate mathematical expressions in C++?

Equivalence of boolean expressions

I have a problem that consist in comparing boolean expressions ( OR is +, AND is * ). To be more precise here is an example:
I have the following expression: "A+B+C" and I want to compare it with "B+A+C". Comparing it like string is not a solution - it will tell me that the expressions don't match which is of course false. Any ideas on how to compare those expressions?
Any ideas about how can I tackle this problem? I accept any kind of suggestions but (as a note) the final code in my application will be written in C++ (C accepted of course).
An normal expression could contain also parenthesis:
(A * B * C) + D or A+B*(C+D)+X*Y
Thanks in advance,
Iulian
I think the competing approach to exhaustive (and possibly exhausting) creation of truth tables would be to reduce all your expressions to a canonical form and compare those. For example, rewrite everything into conjunctive normal form with some rule about the ordering of symbols (eg alphabetical order within terms) and terms (eg alphabetical by first symbol in term). This of course, requires that symbol A in one expression is the same as symbol A in another.
How easy it is to write (or grab from the net) a C or C++ function for rewriting your expressions into CNF I don't know. However, there's been a lot of AI work done in C and C++ so you'll probably find something when you Google.
I'm also a little unsure about the comparative computational complexity of this approach and the truth-table approach. I strongly suspect that it's the same.
Whether you use truth tables or a canonical representation you can of course keep down the work to be done by splitting your input forms into groups based on the number of different symbols that they contain.
EDIT: On reading the other answers, in particular the suggestion to generate all truth tables and compare them, I think that #Iulian has severely underestimated the number of possible truth tables.
Suppose that we settle on RPN to write the expressions, this will avoid having to deal with brackets, and that there are 10 symbols, which means 9 (binary) operators. There will be 10! different orderings of the symbols, and 2^9 different orderings of the operators. There will therefore be 10! x 2^9 == 1,857,945,600 rows in the truth table for this expression. This does include some duplicates, any expression containing only 'and' and 'or' for instance will be the same regardless of the order of symbols. But I'm not sure I can figure this any further ...
Or am I making a big mistake ?
You can calculate the truth table for each expression over all possible inputs then compare the truth tables.