×

[closed] Slow Trains (BUG4EVER 2017) Weird Issue

 2 Here are two of my submissions, exactly same, one in C and the other in C++. The submission in C++ gives an AC verdict while the submission in C gives a WA verdict. The code was originally written in C. Can anyone please point out the error? I'm unable to find the problem because the two submissions are EXACTLY SAME. (only iostream line was different) Solution 1 Solution 2 Thanks! asked 03 Sep '17, 23:39 600●8 accept rate: 15% That's weird. (04 Sep '17, 09:48) I am also having the same problem...would be grateful if someone could figure it out.. solution link:https://www.codechef.com/viewsolution/15245211 (04 Sep '17, 10:09) Quite weird actually. Are you sure that TCs werent changed mid way? (04 Sep '17, 12:28) nope the TC's were same here's the link to the problem: https://www.codechef.com/BUGE2017/problems/BUG2K17A/ (05 Sep '17, 20:52) yeah, TC's were same @vijju123 as most of the people's solution are 100% similar to mine. It's just if and else lol. Wasted 2 hours figuring out error in my logic lol (06 Sep '17, 22:44) What was your error? What difference was in C and C++ code? (06 Sep '17, 22:56) the competition wasn't fair though (06 Sep '17, 23:54) no error no difference (09 Sep '17, 23:10) showing 5 of 8 show all

The question has been closed for the following reason "The question is answered, right answer was accepted" by swetankmodi 02 Oct '17, 22:19

 1 Well, got the error guys. You're declaring variables inside the loop which sometimes don't work well in C Language. Look at the solution if declared outside your code works fine in C. Solution answered 10 Sep '17, 20:59 61●2 accept rate: 60% If you dont know the cause, how did you conclude :p (10 Sep '17, 21:31) Simply, iterating through all the possibilities where an error should occur and that was the line which was causing the error when declared outside, works like a charm. (11 Sep '17, 00:05) Well, now I got the cause. That's a standard difference. In C99, the rules are the same as in C++. Default GCC uses C89 with some extensions. For C89, you must declare all of your variables at the beginning of a scope block. (11 Sep '17, 00:13) 2 Thanks, never knew that it makes a difference nowadays. I had read that nowadays it doesn't matter :\ Maybe I was wrong lol. I'll declare everything global now :P (15 Sep '17, 01:22) Most of the compiler use C99 but still some Online judges use C89 as gcc. Declaring global will also help you to use more space for allocation. Better to use C++, it also has some inbuilt implementations to make the job easy. (16 Sep '17, 20:01)

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:

×1,070
×100
×12

question asked: 03 Sep '17, 23:39

question was seen: 437 times

last updated: 02 Oct '17, 22:19