0

find mistake

perform in c++ int i=2; cout<<i++<<i<<i++<<i; cout<<i; answer is 33224

3rd Dec 2017, 12:49 PM
ADITYA PATHAK
ADITYA PATHAK - avatar
10 Respuestas
+ 3
1)Firstly You should know about pre increment and post increment, Lets say we have p = count++; and int count =5 p = 5(p gets the value of count only, not the ++ part) and count = count+1; so count becomes 6. To know more about this watch this video: https://www.youtube.com/watch?v=FkxFPJG5paw 2)Coming to your question firstly i dont think that is the correct answer. I tried running your program on Dev c++ and the output was absurd. BTW if you want to know how to debug, watch this video https://www.youtube.com/watch?v=LqvLGx8I9QA 3)Without debugging i got 34244 and with debugging I got 23344 which must be the correct one. Break your code in this manner and use the pre and post increment concept #include<iostream> using namespace std; int main() { int i=2; cout<<i++; // lets say x = i++, now x = i(2); so 2 will be displayed cout<<i; // i will be i++ or i+1; so new value of i is 3 cout<<i++; // as new value of i is 3, again let y = i++, so y = i(3) cout<<i; // i will be incremented so i =i+1 ;new value of i is 4 cout<<i; //whether we cout << i or cout<<i++ the output will be the same i.e4 return 0; }
3rd Dec 2017, 3:59 PM
RR2001
RR2001 - avatar
+ 1
you want to get answer as 33224? or you want to get answer as per that code?
3rd Dec 2017, 1:09 PM
RR2001
RR2001 - avatar
+ 1
The actual answer is 34244. you want to know how we get that answer?
3rd Dec 2017, 2:50 PM
RR2001
RR2001 - avatar
+ 1
Please give me a couple of minutes. I shall explain it in a simpler manner.
3rd Dec 2017, 2:53 PM
RR2001
RR2001 - avatar
+ 1
np :)
7th Dec 2017, 12:42 PM
RR2001
RR2001 - avatar
0
no according to that code my answer comes this but it should not come this...
3rd Dec 2017, 2:49 PM
ADITYA PATHAK
ADITYA PATHAK - avatar
0
yep
3rd Dec 2017, 2:51 PM
ADITYA PATHAK
ADITYA PATHAK - avatar
0
actually situation is that... in my question paper above code was given and this answer was stated as correct..also turbo CPP gives the same answer..how could it be
3rd Dec 2017, 2:52 PM
ADITYA PATHAK
ADITYA PATHAK - avatar
0
OK bro
3rd Dec 2017, 2:53 PM
ADITYA PATHAK
ADITYA PATHAK - avatar
0
thx
7th Dec 2017, 6:50 AM
ADITYA PATHAK
ADITYA PATHAK - avatar