954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Parenthesize following expressions

Hello ladies and gents,

Ive got an exercise in which I have to fully parenthesize expressions using the higher-lower precedence, for instance:

a+b*c would become (a+(b*c)) because multiplication has a higher precedence then addition.

Here are my solutions for the following expressions:

1) a = b + c * d << 2 & 8
(a = ((b + (c * d)) << 2 & 8))

2) a & 077 != 3
Don't think there is a solution for this since & is "adress of", but how can you have an adress of 077?

3) a == b || a == c && c < 5
(((a == b) || ((a == c) && (c < 5))))

4) c = x != 0
(c = (x != 0))

5) 0 <= i < 7
((0 <= i) < 7)

6) f(1, 2) + 3
((f(1, 2)) + 3)

7) a = - 1 + + b -- - 5
Don't think there is a solution either since the expressions are not correct?

8) a = b == c ++
(a = (b == (c ++)))

9) a = b = c = 0
(a = (b = (c = 0)))

10) a[4][2] *= * b ? c : * d * 2
((a[4][2]) *= (* b ? c : ((* d) * 2)))

11) a-b, c=d
((a-b), (c=d))

I was hoping that you guys could check these out and tell me which ones are wrong?

Thank you.

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

10 is not a valid statement. you can write a short program that tests each of your problems.

int main(int argc, char* argv[])
{
	int a[5][3] = {0};
	int b = 1;
	int c = 2;
	int d = 3;

	a[4][2] *= * b ? c : * d * 2
	return 0;
}

:\dvlp\test3\test3.cpp(15) : error C2100: illegal indirection
d:\dvlp\test3\test3.cpp(15) : error C2100: illegal indirection

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

it could be correct if b and d were both of type int* - but since the OP's assignment doesn't say otherwise, a safe assumption is that these are just plain old numbers (or perhaps literals)

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 
Hello ladies and gents,


Hi! :)1) a = b + c * d << 2 & 8
(a = ((b + (c * d)) << 2 & 8))
You missed some parentheses. << and & have different precedence.2) a & 077 != 3
Don't think there is a solution for this since & is "adress of", but how can you have an adress of 077?
& is the bitwise AND operator, so the expression is valid and can be parenthesised.7) a = - 1 + + b -- - 5
Don't think there is a solution either since the expressions are not correct?
That's a cruel example. It's a valid expression, but whomever wrote it was trying to be mean and trick you with whitespace. The first - makes 1 negative, the second + is a unary operator that makes b nonnegative and the -- applies to b. If you use more sensible spacing, it looks like a = -1 + +b-- - 5 .10) a[4][2] *= * b ? c : * d * 2
((a[4][2]) *= (* b ? c : ((* d) * 2)))
That's a little off. ;)

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 
10 is not a valid statement. you can write a short program that tests each of your problems.

Ok, I'll do that AD :!:

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 
Hi! :)

Hi Ravalon ;)You missed some parentheses. << and & have different precedence.
& is the bitwise AND operator, so the expression is valid and can be parenthesised.
Darn :cheesy:That's a cruel example. It's a valid expression, but whomever wrote it was trying to be mean and trick you with whitespace. The first - makes 1 negative, the second + is a unary operator that makes b nonnegative and the -- applies to b. If you use more sensible spacing, it looks like a = -1 + +b-- - 5 .
Well, you know Bjarne, cheeky fella :cheesy:That's a little off. ;)
Darn Darn :cheesy:

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 
it could be correct if b and d were both of type int* - but since the OP's assignment doesn't say otherwise, a safe assumption is that these are just plain old numbers (or perhaps literals)

Yep, tried that and it worked, but, as you say, there is no explanation as to what the variables represent, pointers or numbers.

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

I don't know Mr. Jobe if you already know this but there is a book which presents the solutions to the problems posed in C++ Programming Language, you can get it here .

And if you think the book is too expensive or not worth it you can always download the source code for free from here .

Hope it helped, bye.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
I don't know Mr. Jobe if you already know this but there is a book which presents the solutions to the problems posed in C++ Programming Language

Nope, didn't know that s.o.s, thanks for that.

I'd appreciate it if you didn't call me Mr. Jobe, makes me feel old and superior somehow, either JoBe or Johan(my real name) will do ;)And if you think the book is too expensive or not worth it...

I certainly think it's worth it, but, due to lack of trust in online buying with a credit card, I tend to use a store called proxis.be and they don't sell it anymore.

Anyway, thanks for that, I'll surely see if I can't buy it anywhere else :!:

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 
I'd appreciate it if you didn't call me Mr. Jobe, makes me feel old and superior somehow, either JoBe or Johan(my real name) will do ;)


Good luck getting Mr.~s.o.s~ to do that :mrgreen: He is a very respectful person -- to a fault...

I certainly think it's worth it, but, due to lack of trust in online buying with a credit card, I tend to use a store called proxis.be and they don't sell it anymore.

Interesting.... And after all these years, too. Out of curiosity, when you go to a restaurant, when the pink & orange spike haired girl with tattoos and piercings asks for payment, do you hand your credit card to her and she disappears with it? ;)

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
Interesting.... And after all these years, too. Out of curiosity, when you go to a restaurant, when the pink & orange spike haired girl with tattoos and piercings asks for payment, do you hand your credit card to her and she disappears with it? ;)

Pink & orange spiked hair with tattoos...? What sort of restaurants do you visit? ;)

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 
I'd appreciate it if you didn't call me Mr. Jobe, makes me feel old and superior somehow, either JoBe or Johan(my real name) will do ;)


Don't know why all foreigners dislike being addressed as Mister. This is what we are taught in our country to give utmost respect to everyone.;)

Saying Mister is not an indication of you going old or being superior to someone. For me its always being "what goes around comes around".

But still if you don't like it, I will try to refrain from saying it the next time, though there are no guarantees...;)

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Don't know why all foreigners dislike being addressed as Mister. This is what we are taught in our country to give utmost respect to everyone.;)

Saying Mister is not an indication of you going old or being superior to someone. For me its always being "what goes around comes around".

But still if you don't like it, I will try to refrain from saying it the next time, though there are no guarantees...;)

Well, I guess kids these days don't get taught any respect! ;)

Actually, its used the same way over here - although usually only on really formal occasions (Such as in a courtroom, an interview, or in a formal letter). most of the time we're all just too lazy for all that!

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 
Interesting.... And after all these years, too. Out of curiosity, when you go to a restaurant, when the pink & orange spike haired girl with tattoos and piercings asks for payment, do you hand your credit card to her and she disappears with it? ;)

Well, I actually don't have a credit card :cheesy: And yeah, like Bench said, in what kind of restaurants do you go and eat :lol:Don't know why all foreigners dislike being addressed as Mister. This is what we are taught in our country to give utmost respect to everyone.

It's not that I dislike it, on the contrary, it shows that your parents brought you up in a very descent manner and we to are thought to be curtious and polite, just, .... not on a forum like this :cheesy:But still if you don't like it, I will try to refrain from saying it the next time, though there are no guarantees...

Thanks s.o.s, appreciated ;)Well, I guess kids these days don't get taught any respect!

LOL :lol: Never thought that at the age of 37, I would be called a KID LOL :lol: Thanks ....Mr. Bench ;)

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 
Don't know why all foreigners dislike being addressed as Mister. This is what we are taught in our country to give utmost respect to everyone.;)


Being called Mr. Raye is awkward on many levels, but I can ignore all of them except where it feels like you're just dealing with a stranger rather than trying to make a friend. ;) Frankly, there's a difference between respect and professional coldness.

I don't mind being addressed as Mister, but eventually I'll see you as enough of a mate to be bothered by it. At that point I'll ask you to use a more familiar means of address. :)

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 

Hello.
.... you're just dealing with a stranger rather than trying to make a friend. ;) Frankly, there's a difference between respect and professional coldness.
I guess different cultures have different ways of interpreting different things. I was never before given the impression that Mister amounts toprofessional coldness or a complete stranger -- and it has been a while since I was born.

So in the end I guess I really am a cold hearted, unfriendly person.....

Thank you.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
I don't mind being addressed as Mister, but eventually I'll see you as enough of a mate to be bothered by it. At that point I'll ask you to use a more familiar means of address. :)


Don't worry, he will eventually. Just look at me, now instead of calling me "Mr. Joe" he now calls me Joey!

And no I don't really think that a Mr. prefix is bad at all, especially with new members. It really shows respect.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Don't worry, he will eventually. Just look at me, now instead of calling me "Mr. Joe" he now calls me Joey!

And no I don't really think that a Mr. prefix is bad at all, especially with new members. It really shows respect.


Oh, don't misunderstand me, it's definitely not bad. But one man's respect is another's weirdness. I was raised to be polite, but I think I'm still too young to be comfortable with formal titles in everyday conversation. ;)

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 
Interesting.... And after all these years, too. Out of curiosity, when you go to a restaurant, when the pink & orange spike haired girl with tattoos and piercings asks for payment, do you hand your credit card to her and she disappears with it? ;)
Pink & orange spiked hair with tattoos...? What sort of restaurants do you visit? ;)

All kinds. Around the U you get more of these with the strange dress code...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Hello ladies and gents,

Ive got a few more expressions I needed to fully parenthesize:

1) *p++ becomes *(p++)

2) *--p becomes nothing
if *--p would have been --*p:
--*p OR--(*p) would have the same result.
Reason is, both would have the same precedence, so, it
goes from left to right in both cases.

3) ++a-- becomes ++(a--)

4) (int*)p->m becomes (int*)(p->m)

5) *p.m becomes *(p.m)

6) *a[i] becomes *(a[i])


One more question, am I correct that the following expression:
(int*)p->m equals a pointer member which get's a type cast conversion?

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You