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

Quick c++ question

I was studying for an exam and came across two questions that me and my friends didn't know the answer to. We are given the answer but we just can't figure out what the answer is what it is.

First, why does this code give an error?

int z=5, q=2;
cout << 10/(q/z) << endl;


Second, what does this code equal? I believe it equals 19 but our teacher says it is 20.

//Given x=10 and y=5

y = 2*y++ + --x;
cout << y << endl;


Wouldn't y = 2*y++ + --x just be the same as 2*5 + 9 because y++ is post-processing which means that after this line is run y is now equal to 6 and not 5? And --x is equal to 9 because it is pre-processing so 10 would be decremented to 9 before the math?

pattmorter
Light Poster
26 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

I figured out why cout << 10/(q/z) << endl; gives an error. integer division and division by 0. but i still dont understand the second question.

pattmorter
Light Poster
26 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

Did you put the second example in a small program and run it?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 
Did you put the second example in a small program and run it?

yeah the output is 19

pattmorter
Light Poster
26 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

>>//Given x=10 and y=5

y = 2*y++ + --x;

y = (2*y++) + (--x)

//substitute, not valid but easier to see
y = (2 * 5++) + (--10);
y = (2 * 5) + (--10) //5++ returns 5 first then increments, so 5 will be multiplied to 2
y = 10 + (--10)
y = 10 + 9 //--10 decrements first then returns so --10 decrements to 9 first
y = 19

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

I wonder if some compilers would actually increment the y during multiplication.
...that seems to violate a math rule, though.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

yeah that is why i figured id ask here. i personally think she just made a typo. but i wanted to make sure.

pattmorter
Light Poster
26 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 
Second, what does this code equal? I believe it equals 19 but our teacher says it is 20.

Run away from that teacher as fast as you can.

y = 2*y++ + --x;


modifies y twice (by means of postincrement, and by means of assignment) with no sequence point in between. Undefined behaviour, end of story. No answer is correct.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

Run away from that teacher as fast as you can.

y = 2*y++ + --x;


Correct me if I'm wrong. But how is this an undefined behavior? I agree that the below line will give undefined behavior.

x = y++ * --y;

Because we cannot predict which one (y++ or --y) will be evaluated first.
But In this case

y = 2*y++ + --x;

2*y++ and --x are independent. In whatever order they are executed, we get the same result. And only after the result is got, it will be assigned to y. I may be wrong here, but I would like to know what is the undefined behavior here.

subith86
Junior Poster
124 posts since Mar 2011
Reputation Points: 30
Solved Threads: 14
 

Because y is modified twice. See this .

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

ok. Thanks. So

i = i++;


is undefined because of two assignments happening
1. modification done using = operator.
2. modification done by ++ operator.

So if my understanding is correct this assignment should be okay and doesn't have undefined behavior.

i = ++i;

Am I right?

subith86
Junior Poster
124 posts since Mar 2011
Reputation Points: 30
Solved Threads: 14
 
Am I right?

No. The y++ differs from ++y in the value calculated by the operator. A side effect still happens at an unspecified moment somewhere prior to a sequence point. The behaviour is still undefined.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 
A side effect still happens at an unspecified moment somewhere prior to a sequence point.

I'm sorry to say I didn't understand this line. I also googled about undefined behavior and sequence point. I could find nowhere anybody mentioning about pre increment operator, though there are many posts which gives undefined behavior while using post increment. In the example that I gave,

i = 10;
i = ++i;
//step 1 : increment happens, i becomes 11.
//step 2 : i = 11; assignment is going to happen with 11.
//step 3 : value of i becomes 11 after assignment.

My question is, ++ operator already did its job in step 1. So what is the problem?

subith86
Junior Poster
124 posts since Mar 2011
Reputation Points: 30
Solved Threads: 14
 

Given i = ++i;

Does ++ changei?
Does = change i?
If the answer is yes to both, undefined.

Why would it be different from i = i++; ?

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

ok. Thanks. So

i = i++;

is undefined because of two assignments happening 1. modification done using = operator. 2. modification done by ++ operator.

So if my understanding is correct this assignment should be okay and doesn't have undefined behavior.

i = ++i;

Am I right?


How is it undefined? final value of i should always increase by 1

... please ignore i take it back :D

Ali_2101
Newbie Poster
16 posts since Feb 2012
Reputation Points: 4
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You