Implementation of conditional operator

G

gupta.keshav

Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.
Thanks in advance.
K2G
 
E

Eric Sosman

Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.

The best answer is `x?y:z'. Why would you want
any other?
 
K

Keshav

I would like to know is there any such thing available in C by whcih
without using ? or if , we can take decision. I know using ? of if is
the best way but still just to know whether there is any other way of
taking decision based on some operator :)
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.

Since the C language includes the trinary operator, if() statements, and loops,
and you can't have C without them, there's no obvious reason that you would need
to answer this sort of question. Just use the trinary operator, or an if()
statement.

OTOH, if this is /homework/, intended to exercise your knowledge of C, then the
artificial constraints on this problem are acceptable. What would /not/ be
acceptable, however, is for us to do your homework for you.

I'll give you a hint: For a limited subset of types for a, x, y, z
the expression
a = x ? y : z;
/can/ be rewritten within your stated constraints. Think logically, and limit
your answer to a suitable subset of types, and you'll be able to figure it out
on your own.



- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDnaPbagVFX4UWr64RAtrZAKCYjPdgJ2QunwVGC/vkYxIJVbZ93ACfSwsy
uXtEJRFSvGPUh4S2oXOGMKQ=
=zAuG
-----END PGP SIGNATURE-----
 
M

Markus Moll

Hi
I would like to know is there any such thing available in C by whcih
without using ? or if , we can take decision. I know using ? of if is
the best way but still just to know whether there is any other way of
taking decision based on some operator :)

Yes, and it's quite easy. Anyway, I hope you're only asking because this is
an assignment or a fun question...

Hint: What operators do you know that do not necessarily evaluate all
parameters?

(I just realize: Are you "allowed" to use , ?)

Markus
 
D

David Resnick

Keshav said:
I would like to know is there any such thing available in C by whcih
without using ? or if , we can take decision. I know using ? of if is
the best way but still just to know whether there is any other way of
taking decision based on some operator :)

Sounds like homework. Perhaps you should try doing it? Or you
could post the name of your professor, we could then cut out
the middleman and directly send our answers to get credit.

If you are writing programs in C, you have access to ? and if, and
there is no reason not to use them.

-David
 
E

Eric Sosman

Keshav wrote On 12/12/05 11:18,:
I would like to know is there any such thing available in C by whcih
without using ? or if , we can take decision. I know using ? of if is
the best way but still just to know whether there is any other way of
taking decision based on some operator :)

(Please quote enough context in a reply to allow a
reader to understand what you're talking about without
reading all the preceding messages. For the benefit of
latecomers, Keshav has desires to produce the same effect
as `x?y:z' without using the conditional operator, an `if'
statement, or any loops. He wishes to restrict himself
to the symbols ~, !, ^, &, +, |, <<, and >>.)

So: Is there a way to fulfil Keshav's silly desire?
I believe the answer is "No, not for general y and z."
 
S

Skarmander

Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.
Thanks in advance.

I have almost everything I need, but could you also give me the address of
your class instructor or prospective employer? I can send the answer
directly that way.

The question is flawed, by the way. It's impossible to implement the ternary
operator with the symbols given if you don't restrict the question some more
(e.g., only 'int' expressions).

When you've solved it, it's instructive to solve it again with the
constraint that none of the expressions should be evaluated more than once,
if this isn't already so.

S.
 
J

Jordan Abel

are || and && permitted? [these are distinct symbols from & and |]

if so,

(x&&(y||1))||z will have the same effect [if x, then y, else z] - it
won't return the same as "x?y:z" though.
 
U

usr.root

Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.
Thanks in advance.
K2G

y & ~x + x + !x | z & ~x + x + !!x;
 
N

Niklas Norrthon

y & ~x + x + !x | z & ~x + x + !!x;

Not working:

#include <stdio.h>

int main(void)
{
const char* x = NULL;
const char* y = "hello";
const char* z = "good bye";

printf("%s\n", x?y:z);
printf("%s\n", y & ~x + x + !x | z & ~x + x + !!x);
return 0;
}

/Niklas Norrthon
 
R

rayw

Niklas Norrthon said:
Not working:

#include <stdio.h>

int main(void)
{
const char* x = NULL;
const char* y = "hello";
const char* z = "good bye";

printf("%s\n", x?y:z);
printf("%s\n", y & ~x + x + !x | z & ~x + x + !!x);
return 0;
}

Yet it works ok on ints ...

BTW

I hand parsed the original expression for precedence and associativity - had
to, in order to [try and ] understand it!

This is what I came up with ...

#define F(x,y,z) ((y) & ((~x) + ((x) + (!x)))) | ((z) & ((~x) + ((x) +
(!!x))))

It seems to *still* work, so I assume I got it right. However, I was
wondering whether there are any simple parsers that would take a complex
expression like (the original) y & ~x + x + !x | z & ~x + x + !!x; and
paren it accordingly? Better still, something that can break it down into
simpler sub expressions, or a tree representation, e.g. ...

a = ((y) & ((~x) + ((x) + (!x))));

b = ((z) & ((~x) + ((x) + (!!x))));

c = a | b;
 
N

Niklas Norrthon

rayw said:
Yet it works ok on ints ...

x?y:z works with ints *and* pointers.

This is similar to the old swap hack:

void swap(int* x, int* y)
{
*x ^= *y;
*y ^= *x;
*x ^= *y;
}

Seems to work, but try:

int a = 7;
swap(&a, &a);

/Niklas Norrthon
 
R

Randy Howard

(e-mail address removed) wrote
(in article
Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.
Thanks in advance.

Why do professors continue to ask these silly homework problems?
Are they really that strapped for good ideas that might
actually teach something useful?

I'm genuinely intested if anyone sees any actual teaching value
to exercises of this nature for C students.
 
K

Keith Thompson

Randy Howard said:
(e-mail address removed) wrote
(in article


Why do professors continue to ask these silly homework problems?
Are they really that strapped for good ideas that might
actually teach something useful?

I'm genuinely intested if anyone sees any actual teaching value
to exercises of this nature for C students.

There is some danger in these "How can you drive a nail without using
a hammer?" questions, in that they can encourage students to think
that excessively clever solutions are a good idea.

On the other hand, they can probably be a decent way for students to
demonstrate an ability to construct complex solutions to problems, a
skill that's important in the real world.

The canonical first C program just prints "Hello, world". Nobody
really needs a program that just prints "Hello, world" (on most
systems the "echo" command is simpler and more flexible). The point
is to demonstrate the ability to write a program. Similarly, nobody
*needs* to compute the size of an object without using "sizeof", or
implement a conditional operator using an artificially restricted set
of operations, but if you *can* do so, it probably implies some
understanding of the language.

The instructor's problem is to create an exercise that can be stated
briefly and unambiguously, but that requires some significant amount
of knowledge and work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top