I am trying this sum on spoj and getting a WA…i can’t find what case may be giving a WA…this is the solution…can ne pls find a case where this solution is failing or maybe point out a mistake in the solution. I have used GOTO statements please don’t mind it… i myself don’t like it but had to use it!!!
- Goto is really a bad practice. I have experienced an increase in runtime by 0.02+ sec using goto.
Use “break out” instead, where out is the label on the loop from which you want to break off.Also you may set a flag, and check for flag value in loop.
- Your code is too complicated to have a check upon, hence you can have a look at my code to figure out yourself : http://ideone.com/MEowFN
@bit_cracker007 >> I would like to mention that
goto might be a bad practice, but what you have mentioned next is a misleading information. In no way is
goto slower than any other looping statements say
while. They all are the same in performance!
A discussion or say, comparison on SO.
You can check yourself by compiling all the versions, even with optimizer off, the results will be the same.
More detailed examples with ref. to C++
EDIT: In reply to @bit_cracker007’s comment:
Well Well I did not expect that you are comparing the difference on the basis of how much time the online judge took! Then, it depends on a lot of factors. I can prove that a
goto program is faster than a non
goto program by 0.01 sec (going by same logic as yours, have used your code ) see here. The one that took 2.37 seconds is without
goto and the one with
goto took 2.36 seconds.
The response by Online Judge depends on a lot of other factors as well, maybe their cache response etc. I have an experience in problem LEVY of April '13 where I used the same code, it was getting TLE a couple of times, then next time it got AC, and then again it kept on getting TLE. So there might be slight difference of 0.01 (approx.) seconds.
Thank you for these examples for LUCKY10
Overall, I will still stand with my point that using
goto or not using it doesn’t make any change in execution time from what I have read about control flows and all. If someone comes up with a decent logic then I will be very glad to learn that.
EDIT: Regarding goto being a bad practice
It can be that bad
It is considered bad for structured programming, which is more about matching the structure of the code with the structure of the data.
Using unconstrained memory addreses (goto or raw pointers) provide too many opportunities to make easily avoidable mistakes. Source
if(you want to read more about goto being harmful) then goto label;
Go To Statement Considered Harmful
The short answer is that goto statements generally cause messy and hard to read code. This reduces the ability to update that code. Also it was proven that nearly everything that goto was used for could be done using other control statements like the while loop or function calls.
i know that i should not be using goto…but i dont get the mistake…i removed the spaces in between the terms in the expression(if any due to mismatch in the example and the explanation) and resubmitted it but it still gave WA…i checked many cases…the code gives correct ans and the a,b,c values that it generates is also correct…dont understand y WA!!!
got my mistake…thanks neways!!!
If you can check properly what I have written is the statement with “I have experienced” as prefix. And I can give you the proof showing the difference if you are so eager. And if I have written, then for sure I would have compiled the versions. Anyways, nice links provided by you.
Yeah sure give the example, that will be beneficial for me to learn something new.
It happened in LUCKY10 problem(check my submissions) of long contest. The difference comes about 0.02 , which may be natural. But somewhere I had studied that Goto keywork may access critical memory location resulting in increased time or rte.
Cool. Next time I need to answer anything,I am supposed to google it out first. Thanks for your support.
No worries buddy. What matters is we should learn new things.
@kunal361 >> I have updated my answer based on your query.
+1 for the image