int a= 0; X(a++); cout<<"a = "<<a; //console says a = 5 Positive five! a= 0; X(a--); cout<<"a = "<<a; //console says a = -5 Negative five!
waiting for the answers in the comments (comments are moderated) .. i will show them after a couple of right answers..
if your answer is wrong “which is fine”, i will pass the comment..
if it is right .. i will reply on the comment or maybe to your mail telling you to stop trying..
—–
now to the Winners (in order of who replied first):



August 12, 2008 at 2:30 am |
#define X(p) \
p; \
p; \
p; \
p; \
p; \
Actually, I didn’t try that solution… but seems it may do the job
August 12, 2008 at 2:51 am |
1 of 1 submitted solutions is right
August 12, 2008 at 6:13 am |
A few choices…
#define X(stmt) do { stmt; stmt; stmt; stmt; stmt; } while (0)
#define X(stmt) do { stmt; a *= 5; } while (0)
#define X(stmt) crap(&a)
static void crap(int *x)
{
static int value = 5;
*x = value;
value = -value;
return;
}
August 12, 2008 at 9:54 am |
2 of 2 submitted solutions are right ..
?
dr.Kasparov and tombarta
any one else
August 12, 2008 at 3:28 pm |
X takes an argument by reference and times it with 5
August 12, 2008 at 4:17 pm |
@ Tecno ..
no.. not that easy .. try again .. and give me the code .. and try it before you send it unless you are very sure
August 13, 2008 at 11:54 pm |
A simple solution is:
#define X(N) {int i; for(i=0;i0?x:-x; }
August 14, 2008 at 8:42 pm |
Using reference is not useful as it can’t be used on an un-assignable variable (i.e. a++ or a–) ….:S
Am wondering if it can be solved in C#??
August 15, 2008 at 12:37 am |
some people are trying ..
others seem to have got it already
@Hatem
i cant compile your code .. can you please send a line that will compile ?
and no need to be generic just for the +5 -5 case
August 15, 2008 at 1:42 am |
@Fouad:
The problem is that my comment got corrupted as it seems that WordPress has considered the less-than and greater-than signs in the code as the beginning and end of an Html tag
I will rewrite the code differently so that I can hopefully avoid the tag problem.
X will be a macro that repeats the statement passed to it (whether a++ or a–) for 5 times.
#define X(N) while( abs(N) != 4 );
where the function abs( ) is defined as:
inline int abs(int x) { return x>0?x:-x;}
August 16, 2008 at 5:06 pm |
[...] Design Patterns course has ended in FCIS, and following up with the wicked problems of my friend, Fouad, I decided to post a code design problem I heard lately [...]
August 16, 2008 at 6:09 pm |
thanks for all who replied
the model answer “according to myself”
is
#define X(m) m; m; m; m; m
the logic behind is in the next comment..
August 16, 2008 at 6:28 pm |
.. since the ++ suffix operator “a++” is evaluated -usually- the last thing in the whole expression
so
int a = 0;
v = (a++) + 3;
//v = 3 , a = 1; .. so a++ equals 0 not 1
so if you define X as a function that accepts by reference .. or any other way .. it will not work .. it will receive (Zero) as an argument
we need something that accepts an “expression” not a variable
nothing does that “in C” except Macros
a Macro is a kind of function that is executed in pre-compilation time
#define constant 8
means (Replace every word “constant” in the coming code with “8″)
other examples are:
#define min(x,y) (x>=y)?x:y
there are other features in Macros that are simply awesome string literals are not the least ..
then we get “a++” in “m” and we replace the macro with “a++; a++; a++; a++; a++”
#define X(m) m; m; m; m; m
the last expression is not “;” terminated because the macro is written in a terminated line:
X(a++);
August 20, 2008 at 11:55 pm |
a Macro is a kind of function that is executed in pre-compilation time
#define constant 8
Sorry but Idon’t understand whatit’s mean??
Is it akind of standard functions??
August 21, 2008 at 10:24 am |
ok let me explain farther..
1- a macro is NOT a function by the meaning we know .. that is …
#it Replaces Some Code with other code while the function evaluates some input and returns some output
# a function accepts a type .. a macro accepts a string literal .. its like copy and paste
“#define macro(x) x + 1″ replaces macro(5) as a string in the code .. with 5+1 in the code .. then the compiler starts
2- some macros are standard .. like min, max, size_t, wchar .. some are not.. some are Microsoft specific .. some are GCC specific
3- when to choose a macro over a function
“From MSDN”
If you need to choose between a function and a macro implementation of a library routine, consider the following trade-offs:
*Speed versus size The main benefit of using macros is faster execution time. During preprocessing, a macro is expanded (replaced by its definition) inline each time it is used. A function definition occurs only once regardless of how many times it is called. Macros may increase code size but do not have the overhead associated with function calls.
*Function evaluation A function evaluates to an address; a macro does not. Thus you cannot use a macro name in contexts requiring a pointer. For instance, you can declare a pointer to a function, but not a pointer to a macro.
*Type-checking When you declare a function, the compiler can check the argument types. Because you cannot declare a macro, the compiler cannot check macro argument types; although it can check the number of arguments you pass to a macro.
August 21, 2008 at 11:09 pm |
Thanks for these useful information ……