SRL Archive - General

Explain please.. Teacher says I'm wrong..


Brandon

2012-03-06 12:44:46

Am I wrong that the answer is 2?? Teacher refuses and says that it's 3 because the compiler spits out 3 for ans.. but if I do it manually with pen an paper or with the cout<<v2%v1++, it spits out 2 which is what I get on paper..


#include <iostream>

using namespace std;

int main()
{
int ans = 0, v2 = 8, v1 = 5;
ans = v2 % v1++;
cout<<(ans); //This prints out 3..
cout<<(v2 % v1++); //This prints out 2..
cin.get();
return 0;
}

Gu3st...

2012-03-06 12:55:28

try "cout<<(v2 % v1++);" instead of "ans = v2 % v1++;" and "cout<<(ans);". What does it return?

Brandon

2012-03-06 13:02:58


#include <iostream>
using namespace std;

int main()
{
int ans = 0, v2 = 8, v1 = 5;
cout<<(v2 % v1++);
cout<<" ";
ans = v2 % v1++; //If I do (V2 % V1++) it still prints the same thing.. IF I remove the ++, it still prints the same thing!
cout<<(ans);
cin.get();
return 0;
}
Prints a 3 and a 2 respectively.

Edit someone explained to me that it's using the old value.. so I have to reset it..
So I did..

but look now:


#include <iostream>
using namespace std;

int main()
{
int ans = 0, v2 = 8, v1 = 5;
ans = v2 % v1++;

cout<<(ans); //Prints 3..

v1 = 5; //Reset 5;

cout<<(v2 % v1); //Prints 3..

cout<<(v2 % v1++); //Prints 3.. wow...
cin.get();
return 0;
}

mormonman

2012-03-06 13:34:34

The v1++ is not applied until after the %. Go read Order of Operations again :)

Brandon

2012-03-06 13:37:27

From: mormonman
The v1++ is not applied until after the %. Go read Order of Operations again :)



http://en.cppreference.com/w/cpp/language/operator_precedence


++ comes BEFORE %..

Even says it on Wiki as well.. The scope operator has the highest order of precedence where the ++ comes right after as second.. As a suffix and postfix or whatever.. it still always comes before mod.

mormonman

2012-03-06 13:48:53

Well, apparently whatever you read is wrong ;) Because that is exactly what is happening. My java prof went over this same thing in class the other day.

Brandon

2012-03-06 13:50:32

But how can this be wrong then:

V2 % V1++ Prints 3..
//Reset all variables to the original values..

V2 % V1 Prints 3 still.. if ++ comes before % according to all the C++ tutorial sites.. then I cannot be wrong..

Killerdou

2012-03-06 13:55:26

maybe ++v1 will be before and v1++ will be after

x[Warrior]x3500

2012-03-06 14:04:33

v1 % v2++ -> 8%5; 5++;
v1 % ++v2 -> 8%6;

EDIT: i think thats how it works XD. been a lil while.

superuser

2012-03-06 14:09:12

++x is pre-increment and x++ is post-increment that is in the first x is incremented before being used and in the second x is incremented after being used.

mormonman

2012-03-06 14:09:15

It's a postfix. That's how it works.

Brandon

2012-03-06 15:45:52

All wrong above.. The Value is incremented no matter what.. There is a trick to solving this which I just did..

template <class T>
class R
{
private:
T val;
public:
R() {}
~R() {}

int& operator ++ (int &SomeNumber)
{
if (this != &SomeNumber)
val = SomeNumber;

SomeNumber += 1;
return *this;
}
};
In other words:


int increment(int& i)
{
int result = i;
i += 1;
return result;
};
Pretty ridiculous exam question though because she couldn't even explain why we were wrong.. -___- Turns out the ++ does happen before modulo 100% of the time..

Basically it stores the actual value in memory first.. increments it but returns the one in memory.. when the value is used, it then returns the one in memory aka the incremented value.. pretty dumb ={

Markus

2012-03-06 15:58:00

pre-increment/post-increment. Very useful difference, as the post-increments get executed after evaluation. If you wanted 2 you had to do like

#include <iostream>
using namespace std;

int main()
{
int ans = 0, v2 = 8, v1 = 5;
cout<<(v2 % v1++);
cout<<" ";
ans = v2 % ++v1;
cout<<(ans);
cin.get();
return 0;
}

x[Warrior]x3500

2012-03-06 16:07:04

From: Markus
pre-increment/post-increment. Very useful difference, as the post-increments get executed after evaluation. If you wanted 2 you had to do like

#include <iostream>
using namespace std;

int main()
{
int ans = 0, v2 = 8, v1 = 5;
cout<<(v2 % v1++);
cout<<" ";
ans = v2 % ++v1;
cout<<(ans);
cin.get();
return 0;
}



int main()
{
int ans = 0, v2 = 8, v1 = 5;
cout<<(v2 % v1++); // v1 is incremented to 6
cout<<" ";
ans = v2 % ++v1; // 8%(6+1);
cout<<(ans); //1 . you need to reset the values after cout<<" "; XD
cin.get();
return 0;
}

Brandon

2012-03-06 16:10:04

^^ That's the thing.. V1 is NOT equal to 6.. even though u DID increment it.. What I posted was correct..

V1++ is equavalent to V1 = V1.. it does nothing.. then if u call V1 after.. it'll equal 6..
So


int main()
{
int V1 = 5;
cout<< V1++; //This will print 5; Remember.. ++ returns the original value first..
cout<<V1; //This will print 6; Call our variable for a second time and the incrementation would have applied.
}
By the way markus how did you get C code tags -__- some special trick or something?

It's a dirty exam question that a teacher cannot explain.. ridiculous. It was multiple choice so we couldn't use a computer.. just pencil paper. Entire class got it wrong because we all thought it happens immediately even though it happens only on the second call to that variable.

x[Warrior]x3500

2012-03-06 16:13:03


#include <iostream>
using namespace std;

int main()
{
int ans = 0, v2 = 8, v1 = 5;
cout<<(v2 % v1++);
cout<<" ";
cout<<"v1 = " << v1;
cout<<" ";
ans = v2 % ++v1;
cout<<(ans);
cin.get();
return 0;
}

its equals 6. like i said earlier:

v1 % v2++ -> 8%5; 5++;
v1 % ++v2 -> 8%6;