×

# include<iostream>

using namespace std; //Pre And Post Increment Behaivour int main(){ int a=2,b; b=a++ + a++; cout<<"The value is "<<b<<endl; //Output=5

a=2;
b=a++ + ++a;
cout<<"The value is "<<b<<endl;
//Output=6

a=2;
b=++a + a++;
cout<<"The value is "<<b<<endl;
//Output=7 HOW?
a=2;
b=++a + ++a;
cout<<"The value is "<<b<<endl;
//Output=8 HOW??

return 0;
}


I couldn't understand the last Two outputs.. Can anyone help?

This answer is marked "community wiki".

1
accept rate: 0%

1

If you change the value of a variable more than once in c, like (++a + a++ + --a etc.) then its undefined behaviour. Its all on luck what gets printed. You got an output of 6, which is a nice co-incidence.

In short, unlike java, these operations lead to undefined behaviour in C (and perhaps c++?)

(21 May, 14:04) 4★

Answer to the above asked question.. output is 6 as ,first increment the whole thing in one go,now put the incremented value at pre-increment and start calculating from the left by putting the actual value in post-increment. As,b=a++ + ++a ;// b = 2 + 4; b=++a + a++;// b=4 + 3; b=++a + ++a;//b = 4+4;

(15 Jun, 14:41)
 1 I am a learner in programming .Where can I search for algorithm .I mean ,tell me a good source website to learn algorithms. answered 21 May, 19:26 11●2 accept rate: 0% geeksforgeeks.org is best website for this. Also, there are tutorials on codechef, and many answers which deal with how to start coding/learn algorithms. Give forum a search (cant provide link atm, any other user who can is requested to provide link) (21 May, 20:17) vijju1234★ I can't reply to ram_mahidhar directly, but here's a good book for beginners: https://cses.fi/book.html (21 May, 21:47) vasja5★ @ram_mahidhar http://www.geeksforgeeks.org/top-algorithms-and-data-structures-for-competitive-programming/ Here is the link where you can find the algorithms brother. (26 May, 10:27) gary_5710★
 1 How many karma points will a person get if his answer is upvoted? And how many karma points he will lose by asking a question and how many karma points will he lose if his answer gets downvoted? UPD: Also what are badges? Will someone get karmas for earning badges? answered 11 Jun, 15:03 2★xerefic 61●2 accept rate: 0% 1 answer is 10,0,2 (11 Jun, 15:07) Google "Codechef's new karma system" and you will find every detail needed on codechef blog. (11 Jun, 15:24) vijju1234★ Badges can be taken as "trophies" you get for some achievement. No, they dont affect karma in any way. (15 Jun, 17:21) vijju1234★
 1 To stabcode, for his Q on two num- Your code is looping through i=1 to i=number of turns. That is causing TLE since N can be very large. You need to derive a formula of ans in form on A,N,B. You will find that no matter how large the N is, if N is even, its effect is nullified upon divison and this reduces the answer to max(A,B)/min(A,B). If N is odd, alice gets 1 turn more so we replace A by 2 x A and do the same. Wrote answer thrice but due to some bug at forum its giving error, hence answer posted here. Comment if things arent clear. answered 17 Jul, 15:54 4★vijju123 6.5k●2●11 accept rate: 17%
 0 What is the best way to check my code execution time? answered 22 May, 23:42 1★irikeish 1 accept rate: 0% @irikeish Run it on "Code, Compile and Run" and it will show you the exact time. If you mean execution time for larger test cases, meaning "if this algo will pass or not", just remember that online judge performs 10^8-10^9 instructions per second. So if your program is like- for(i=0;i< n;i++) { for(j=0;j< n;j++) { .... } } Then for every i, second loop repeats N times. Overall, things are performed N+N+N...(upto N times) = N x N = N^2 times So if N is around 10^5, it requires 10^10 instructions which exceeds time limit. You will get a fair idea like this and when you practice and look for new algos. Do indicate via comment (on your answer)if this resolves your query or not. (Couldnt post as comment as it exceeded limit :/ ) (23 May, 00:00) vijju1234★ 1 @irikeish you can use the clock() function defined in the time.h header file. your code could be like this. #include using namespace std; int main() { clock_t start = clock(); //your code part of which you want to check the execution time for. clock_t end = clock(); double t=(double)(end-start)/CLOCKS_PER_SEC; //now the varialbe t contains the execution time in secs. Just print it to check the time. printf("%lf\n",t); return 0; }  (23 May, 00:16)
 0 can anyone tell me why im getting TLE in SNAKEEAT problem in snackdown_17 qualifier ! here is the link to my code https://www.codechef.com/viewsolution/13663672 ...and also suggest d best approach ! answered 24 May, 18:07 3★msd_007 289●7 accept rate: 7% 4★vijju123 6.5k●2●11 for(int y=m-1;y>=0;y--) { a[y]=a[y]+1; if(a[y]>=b[r]) { c++; a[y-1]=a[y-2]; a[y-2]=0; } } This loop inside the query loop makes your complexity O(NQ) in worst case and it would require ~10 seconds to solve. Read the editorial first, if doubt still persists, comment here and i will convert it into a question. (24 May, 18:13) vijju1234★ There is another thread asking the same question, keep an eye there too. The methods followed are basically same. (24 May, 18:17) vijju1234★
 0 Why can't I submit my code for May Long Challenge? Up-solving the problems that you were not able to solve in a contest is a must. But the submission link is removed. Also, I want to ask about the editorials of the long contests. The May Long questions are still not provided with editorials. The last one : Is there a way by which I can view the test cases after the contest ends? answered 25 May, 18:42 1 accept rate: 0% The problems are moved to practice section. To easily get to the practice version of problem- 1)Click on weblink 2)remove the part "/MAY17" 3) HIT enter. Regarding editorials, forum users cannot comment anything. Try mailing codechef about it, or try to ask the question as doubt here, we will surely help! No, there is no way of viewing the test cases. (25 May, 18:52) vijju1234★ That helped. So, there won't be any editorials? (25 May, 22:14) Depends...only @admin can provide any information on it, and its advised to mail them for this. However, i case you need an editorial urgently, one of the discuss members would love to write one. Just ask once :) (25 May, 22:29) vijju1234★ ok thanks. (26 May, 15:11)
 0 Can anyone tell me why this code is not functioning properly?Can we accept and display a string like this?  main() { string a;char ch; for(int i=0;i<5;i++) { cin>>ch; a[i]=ch; } cout<
 0 Is using insertion sort in SNAKEEAT problem causing TLE? https://www.codechef.com/status/SNAKEEAT,amanawasthi96 answered 26 May, 13:22 15●1 accept rate: 0% Insertion sort is O(N^2) in worst case and can cause TLE depending on input. (26 May, 14:17) vijju1234★
 0 Hi everyone, can anyone tell me why am I getting WA with this code the problem CHEFSQ ? I've tried different test cases and they all seem to give me the correct answer... Thanks ! answered 26 May, 15:59 2★sysm 11●1 accept rate: 0% Answer to sysm: The sub sequence doesn't have to be continuous, but the order of elements should be maintained. Input 1 5 1 2 3 4 5 3 1 3 5  Your Output: No  Correct Output: Yes  my C code (26 May, 16:28) Haaaaaa. Thanks for your answer ! (26 May, 16:31) sysm2★
 0 Sir,my code for permut2 https://www.codechef.com/problems/PERMUT2 is not working properly,I am unable to debug it and desired output is not coming for a few problems. PlEASE HELP https://www.codechef.com/viewsolution/13791948 answered 26 May, 21:26 1 accept rate: 0% Your question has been shifted here- https://discuss.codechef.com/questions/99127/kindly-help-me-for-spoj-ambiguous-permutation It has been upgraded into an individual question. Make sure to keep an eye there to clarify your doubts! (26 May, 21:32) vijju1234★ Thank you sir. (27 May, 11:44)
 0 I got wrong answer in the samesnak problem asked in snack down qualifiers. Can anyone please tell me what's wrong with the code. Here's the link: https://www.codechef.com/viewsolution/13762975 answered 26 May, 23:10 0★avi0_8 1 accept rate: 0% Can you please check once the test cases provided by fellow members to other people who got WA? Have a look at similar questions and the errors, and then comment here if help is still needed. I will then convert it to a question (This question is being repeatedly asked, hence my request to have a check :) ) (26 May, 23:50) vijju1234★
 0 Can anybody tell me what is meant by number of stars beside a users' name? I have seen many people having different number of stars beside their name. Some have 3 or 7 or so. answered 27 May, 21:42 1 accept rate: 0% https://blog.codechef.com/2017/03/09/a-star-studded-rating-system/ (27 May, 21:45) Stars are awarded on basis of your ratings. More ratings=more stars= meaning the user did well in contests (27 May, 21:45) vijju1234★
 0 IS THERE ANY WAY OF EARNING KARMA POINTS OTHER THAN UPVOTING WHICH IS EASY PLZ REPLY answered 27 May, 22:36 3★soheb17 1 accept rate: 0% According to the Buddhist belief Karma is that whatever you do comes back to you, e.g. if you do something good, something good will happen to you, and vice versa. (28 May, 18:10)
 0 Can I get May Lunch Time Questions in Practice block ? answered 28 May, 12:08 11●1 accept rate: 0% A thread would be created if this doesnt happen by 11pm today. Dont worry :) (28 May, 12:09) vijju1234★ The thread has been created. Please keep an eye out there for updates :) (29 May, 02:07) vijju1234★
 0 Doesn't lunchtime have editorials? answered 28 May, 17:08 5★saahilk 1 accept rate: 0% They usually have. (29 May, 02:07) vijju1234★
 0 getting "successfully executed" on online IDE...bt when i submit it im getting runtime error !! is der any way to check my error ?? answered 01 Jun, 01:46 3★msd_007 289●7 accept rate: 7% This usually happens when accessing out of index in an array/string. Please give link to question and solution for us, so that we can help you further. :) (02 Jun, 00:00) vijju1234★
 0 In the latest SNCKPB17 contest, in SNELECT, can you provide all the trick cases because according to me there is no error in my code even after spending 2 hours on it. It will really help a lot. Thank You! answered 01 Jun, 03:10 1 accept rate: 0% This is against the rules. (01 Jun, 11:05) vijju1234★
 0 What is the "accept rate" below each user's name in the forum? After each contest, how long until rating and solving activity are updated? I did SnackDown Pre-A which was several days ago, but neither rating nor "solved problems" on my profile have been updated since then. Or are these 24-hour contests unrated? Thanks Vijju. answered 01 Jun, 15:23 2★royappa 21●1 accept rate: 0% Accept rate is the % of answers (of that user) which got accepted by other users. Accepted answer means the answer satisfied the query of the one of who asked the query and rewards the answering user with karma. Ratings can take time sometimes, while at times they are done instantaneously . Snackdown is elimination rounds are not rated afaik. Lunchtime, Coofoff and Long contests are rated, and any external contest which they declared as rated. All other contests are unrated. (01 Jun, 16:11) vijju1234★ Got it, thanks! (02 Jun, 00:46) royappa2★
 0 i am trying to solve beautiful arrays question. in IDE the input i gave (in accord to format) gives right output , but my answer is shown wrong. can i see (at least some of them) test cases (my code gives right output for test cases provided on site)? Here's the link: https://www.codechef.com/viewsolution/13978678 answered 02 Jun, 12:26 1 accept rate: 0% 1 your code fails for this testcase 1 5 -1 1 1 -1 1 your ouput:no Expected output:yes (02 Jun, 12:33)
 0 Will the problems solved in the recent snackdown pre-A, pre-B rounds be added to my "recent activity"? It shows practice problems solved, so even though those contests are unrated I was hoping to see them there (just to keep track). Thanks! answered 02 Jun, 13:12 2★royappa 21●1 accept rate: 0% If solved in practice, they should be added. If during contest, then i am not sure (although answer inclines to a no). Anyways, its more of a question you should ask via mail to admins. (02 Jun, 16:22) vijju1234★ As the problems were submitted on the behalf of your team, and not as you. It won't be added to your "recent activity". (02 Jun, 22:25)
 0 Can I ask questions regarding modifications required in the code? answered 02 Jun, 13:23 11●1 accept rate: 0% This question needs more details to be complete. modification of which code? The code given in editorial, or "modifying and debugging my code to help me fetch AC" ?? OR any other interpretation? (02 Jun, 16:20) vijju1234★
 0 SNAKGRAPH SNPB17 https://www.codechef.com/problems/SNGRAPH Can any one tell for which test case my code fails?https://www.codechef.com/viewsolution/13974266 please tell also about self loop (GIVEN IN CONSTRAINT). answered 02 Jun, 21:31 1 accept rate: 0% Self loop means a node having edge to itself i.e edge from 'u' to 'u'. Multi edges meaning There exists more than two edges from Node 'u' to node 'v'. Input: 1 5 5 1 2 2 3 2 4 4 5 1 5 Correct Output: 0 1 4 4 4 Your Output: -1 0 4 4 4 (02 Jun, 22:19) Thanks divyansh for helping him out :) (02 Jun, 23:15) vijju1234★ 1 Always happy to help :) (02 Jun, 23:44)
 0 How to optimize this kind of loop for(i=0;i
 0 can anyone explain dis problem statement with example cases clearly...https://www.codechef.com/JUNE17/problems/NEO01/ answered 04 Jun, 21:56 3★msd_007 289●7 accept rate: 7% Ask this at comment section of problem. If its not violating rules, problem setter oughts to reply. Admin says that problem shouldnt be discussed on discuss while contest is live... (04 Jun, 22:38) vijju1234★
 0 can i submit my logic to june contest and ask where it wrong? answered 05 Jun, 21:17 1★ani_1107 1 accept rate: 0% @ani_1107 No it is not allowed as it is an ongoing contest. (05 Jun, 21:19) Discussing of problems of a live contests are against the rules. So, Nope :( (05 Jun, 21:19)
 0 Hi! I tried to submit a code but codechef isnt accepting it. It doesnt move forward to the window where it shows 'running', it always hangs on a specific code. When i tried to submit another code which is not related to the question, it accepted and showed a WA. But when i submit this specific code, it doesnt accept. What do i do? I think its a bug or something answered 06 Jun, 16:15 3★cersei 1 accept rate: 0% Please provide the exact link to the question. (06 Jun, 16:21) shraeyas3★
 0 Why do I am not able to view others solution of the contest Snackdown elimination round? Also the editorial is taking a whole lot of time to upload. answered 06 Jun, 20:00 56●2 accept rate: 10% Its best if you move this to the question related to snackdown editorial. (06 Jun, 20:09) vijju1234★ 1 Probably because when they made the mirror, everyone was able to see the solutions and some people used it for cheating, so they denied access and forgot to revoke that. And true :( editorials are taking long. There is a post regarding that issue already here: https://discuss.codechef.com/questions/100670/pleases-upload-snackdown-elimination-round-editorials (06 Jun, 20:09) If you have a specific problem you want solution of, you can request the fellow members of discuss to help you. (08 Jun, 19:57) vijju1234★
 0 Why do I get SIGSEGV runtime error for this code?here is my code https://www.codechef.com/viewsolution/14165238 check it once answered 09 Jun, 00:58 0★dimpu123 1 accept rate: 0% Firstly, dont spam discuss by asking same thing multiple times. Then, N varies from 1-10^9. For 10^9, you will exhaust the maximum recursion depth and hence get a SIGSEV. Your function has a limit upto which it can undergo recursion. (09 Jun, 01:20) vijju1234★ sorry for asking multiple times.what changes i have to do in my code?can u please tell me? (09 Jun, 13:10) dimpu1230★ The changes are simple. First thing is, that the function repeats after every 6th number. Meaning- F(1)=F(7), F(2)=F(8) and so on... All you have to do is to compute the first 6 values of function (do some maths on paper!). Then, do N%6 and arr[N%6-1] is the answer. (To understand this, i repeat, DO it on paper, find out values of F(1), F(2)...F(8) in terms of A and B, and then it will make perfect sense!) (09 Jun, 13:31) vijju1234★ Thanks @vijju.:) (09 Jun, 13:44) dimpu1230★ In case you get stuck on implementation, you can view my code here- https://www.codechef.com/viewsolution/13783648 (09 Jun, 13:53) vijju1234★
 0 Hi Team, I tried to submit a solution to one of the problems on this site. However it shows as NZEC error. I have tested my code successfully with few test cases in my local IDE as well as on ideone.com Since, it is failing at codechef, I understand my code is failing with few test cases. Now, how to understand - 1) What is the actual runtime error the code is throwing? 2) For which test case (set of inputs) my code is failing? Regards, Lalitha answered 09 Jun, 14:00 1●1 accept rate: 0% Please give a link to the question, as well as your solution. Because without seeing your code, its not possible to pin-point the exact reason behind the error. (09 Jun, 14:43) vijju1234★ Hi, Thanks for the reply. I would want to rectify the error myself at least till I give up.. :) I am more keen in understanding, where in this website can I find the output the code is generating? I am just seeing a NZEC in the MySubmissions page; but not clear of what exception the code is throwing. Regards, Lalitha (09 Jun, 14:55) NZEC error most commonly happens when you access an array out of bounds, especially in JAVA. I will suggest you go through any nested loop and make sure its of type- `for(i=0;i
 0 What kind of error does data overflow gives? Context: I changed data-type from int to long and I am still getting WA answered 09 Jun, 18:21 0 accept rate: 0% 4★vijju123 6.5k●2●11 Which language are you using? If its C++, then long and int have same memory. Use long long int instead of long or int. Data overflow usually gives WA. (09 Jun, 18:45) vijju1234★ I am using Java (09 Jun, 18:54) Then it must be a logical error on your part. Make sure to read the Q carefully and check comments (if any). (09 Jun, 18:58) vijju1234★ 1 Thanks vijju, it was a logical error. Got AC. (09 Jun, 19:33)
 0 I am a member from one of the SnackDown Online Elimination top 300 teams (fzszacm) I haven't written the address in my own account. Could I edit my address these days? And the address need to be in English(or counld I use Chinese？) Sorry for my poor English :) answered 09 Jun, 19:24 0★l0nl1f3 1 accept rate: 0% Actually it's completed now.is it right now ? (09 Jun, 19:40) l0nl1f30★ Goto your profile and use the pencil button to edit things out. Its better if you use english. Regarding use of native language in address, please mail them at help@codechef.com because its they who can best tell regarding this. (09 Jun, 19:41) vijju1234★
 0 Hey everyone.. There are a few problems which executes faster in first lang than in second. Also the solution in second lang might exceed the time limit. Can anyone please suggest me how to prefer a language to avoid exceeding time limit? Also what to do in such situations? answered 11 Jun, 16:51 1 accept rate: 0% Slower languages are given appropriate time compensation so that they dont get TLE if algo is fine. You should, however, prefer c++ ideally. (11 Jun, 18:02) vijju1234★
 0 Why can't I see the comments in the JUNE challenge problems. I have faced this issue before too. When I want ot see the comments it shows no comments. Are the comments not available for everyone? answered 12 Jun, 12:39 4★nvs232 21●2 accept rate: 0% Comments are usually hidden until admins decide otherwise. (12 Jun, 17:01) vijju1234★
 0 please see why my code (https://www.codechef.com/viewsolution/14246552) for SNELECT gives this and i got this error many times but couldn't get why? please answer..... this error..... Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at Codechef.main(Main.java:14) answered 13 Jun, 18:03 2★manoj09 1 accept rate: 0% why do i need to check through in.nextInt() or some other?? and even after checking now it gives runtime error(nzec) why? someone pls help? (13 Jun, 18:18) manoj092★ @manoj09 your link is broken. (13 Jun, 18:22) sysm2★ @sysm updated link: https://www.codechef.com/viewsolution/14246552 (13 Jun, 18:25) @manoj09 try this testcase 1 m (13 Jun, 18:28) hey @olofmeister , can you help him get some details on that error. I think its due to negligence of white spaces or something like that. Your opinion? :) (13 Jun, 18:54) vijju1234★ 1 @vijju123 @manoj09 , i guess accessing direct elements such as ch[1], without knowing they have been initiated before may lead to a runtime error(i am not 100% sure, as this is a condition of SIGSEG too), but as u can see, u have directly used ch[1] and ch[ch.length-2] which wont be declared for string size of 1, and as expected it gives a runtime error on strings of length 1. I think that could be the case. (13 Jun, 20:20) 1 thanks everybody ...i got it (13 Jun, 20:52) manoj092★ showing 5 of 7 show all
 0 Hello, i'm a newcomer to CodeChef. During contest I couldn't find any information about memory/time limit for the task. So if it exists, please show where to look up for it. answered 13 Jun, 19:00 3★bobra 31●2 accept rate: 0% time limit and memory limit for a particular question in codechef(any problem may b practice or contest) can be found if u scroll down till bottom..... @bobra der u can see details about author,tester,time limit,source code limit !! happy coding:) (13 Jun, 19:06) msd_0073★ Just a small correction, memory limit is not the source limit. Usually memory limit is ~256 (or 512) MB. You can declare an array size upto 10^8 easily (~10^4 for 2-D array). (Array sizes of 10^9 give error as they take too much memory) (13 Jun, 19:32) vijju1234★ ohh im sorry nd tqq @vijju123 (13 Jun, 19:33) msd_0073★ Thanks for helping him dear ^^ (13 Jun, 19:34) vijju1234★ Thank you. This will help a lot:D (13 Jun, 21:49) bobra3★
 0 why i m getting this error.... Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at CodeChef.main(Main.java:10) ...... in my code https://www.codechef.com/viewsolution/14253104 answered 14 Jun, 17:31 2★manoj09 1 accept rate: 0% question from chef and feast(NEO01) (14 Jun, 17:32) manoj092★ Where are you getting this bug? If this would had been the case, you wouldnt get WA after 0.81s, but instantly at 0.00sec. Are you inserting input in correct format? In JAVA, inout is sensitivive to extra spaces and extra lines. (16 Jun, 10:46) vijju1234★
 0 Guyz I am pretty much new to coding..I mostly am only able to apply brute force on questions! Can anyone suggest me a good book for Data Structures and Algorithms pls? P.S: I need a book not a soft copy or websites. Thanks for the help :) answered 14 Jun, 19:54 243●7 accept rate: 9% I will say rather start from geeksforgeeks.org , most of the books will deal only with basics and you will need more than it. Also, give a search on forums, this question regarding books and notes are answered alot before. (14 Jun, 19:59) vijju1234★ Thanks man :) (16 Jun, 11:05)
 0 When will the editorials of JUNE17 arrive. Why does codechef no longer upload the editorials of contests on time ? Its been 3 days the long challenge has ended and no editorial has arrived yet!! December long challenge had no editorials and I still struggle with this and this problems from that contest. Then February challenge again had no editorials and the last 2 questions were really nice and are still left for me to upsolve. Then May had no editorials of last 4 questions and now June has no editorials yet. Why not create editorials before the contest ? When will the editorials of these contests arrive ? answered 15 Jun, 22:05 4★vivace 46●1 accept rate: 10% You could have created a Q yourself...You want me to convert it into a question?? TBH, seeing the pattern, i dont think the editorials will come out...But i see that community has been really helpful this time. Alot of questions are discussed actively and excellent approaches were given. You can refer those till then. :) (15 Jun, 22:21) vijju1234★
 0 can anyone please tell me as to how could I do IPC train of July'17 Long using lazy propagation.? Is this even possible? answered 17 Jul, 15:07 3★kunal12 34●5 accept rate: 0% Check the editorial once, if things are not clear i will explain. No, lazy propagation was not needed there. (17 Jul, 15:51) vijju1234★
 0 Can you please explain the logic of calculator for passing the 2nd test case as it is bothering me? answered 17 Jul, 15:15 1●1 accept rate: 0% Is the editorial unclear to you? If yes then i will elaborate, else its fine ^^ (17 Jul, 15:28) vijju1234★ sorry.. editorial wasn't uploaded when i asked the question and thanks for the reply, i then checked it and understood the editorial. (17 Jul, 15:48)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×155
×82