Home > Archive > C > December 2005 > Implementation of conditional operator
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Implementation of conditional operator
|
|
| gupta.keshav@gmail.com 2005-12-12, 6:56 pm |
| 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
| |
| Eric Sosman 2005-12-12, 6:56 pm |
|
gupta.keshav@gmail.com wrote On 12/12/05 11:04,:
> 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?
--
Eric.Sosman@sun.com
| |
| Keshav 2005-12-12, 6:56 pm |
| 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 :)
| |
| Markus Moll 2005-12-12, 6:56 pm |
| Hi
Keshav wrote:
> 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
| |
| David Resnick 2005-12-12, 6:56 pm |
|
Keshav wrote:
> 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
| |
| Lew Pitcher 2005-12-12, 6:56 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
gupta.keshav@gmail.com wrote:
> 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)
iD8DBQFDnaPbagVFX4UWr64RAtrZAKCYjPdgJ2Qu
nwVGC/vkYxIJVbZ93ACfSwsy
uXtEJRFSvGPUh4S2oXOGMKQ=
=zAuG
-----END PGP SIGNATURE-----
| |
| Skarmander 2005-12-12, 6:56 pm |
| gupta.keshav@gmail.com wrote:
> 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.
| |
| Eric Sosman 2005-12-12, 6:56 pm |
|
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."
--
Eric.Sosman@sun.com
| |
| usr.root@gmail.com 2005-12-13, 3:55 am |
|
gupta.keshav@gmail.com wrote:
> 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;
| |
| Jordan Abel 2005-12-13, 6:56 pm |
| On 2005-12-12, Eric Sosman <eric.sosman@sun.com> wrote:
>
>
> gupta.keshav@gmail.com wrote On 12/12/05 11:04,:
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.
[color=darkred]
>
> The best answer is `x?y:z'. Why would you want
> any other?
>
| |
| Niklas Norrthon 2005-12-13, 6:56 pm |
| "usr.root@gmail.com" <usr.root@gmail.com> writes:
> gupta.keshav@gmail.com wrote:
>
> 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
| |
|
|
"Niklas Norrthon" <do-not-use@invalid.net> wrote in message
news:0pr78htej0.fsf@niklas.ua.dynas.se...
> "usr.root@gmail.com" <usr.root@gmail.com> writes:
>
>
> 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;
| |
| Niklas Norrthon 2005-12-14, 6:57 pm |
| "rayw" <ray.webster@gmail.com> writes:
> "Niklas Norrthon" <do-not-use@invalid.net> wrote in message
> news:0pr78htej0.fsf@niklas.ua.dynas.se...
>
> 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
| |
| Randy Howard 2005-12-14, 6:57 pm |
| gupta.keshav@gmail.com wrote
(in article
<1134403479.758337.280790@g43g2000cwa.googlegroups.com> ):
> 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.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw
|
|
|
|
|