Why one solution got AC while other WA

https://www.codechef.com/viewsolution/35703183
https://www.codechef.com/viewsolution/35703169

i am getting AC if i use cin to get “n”…but getting wrong answer when using scanf.
can anyone please help?

Why is this happening?
@algmyr @carre @galencolin
I initially thought it’s due to undefined behavior, but there doesn’t seem to be any such behavior in the code.

Found a similar issue here.

3 Likes

It gives AC in C++ 17.

2 Likes

Weird. :no_mouth:

2 Likes

This language is so confusing. :man_shrugging: :man_shrugging:

1 Like

So true! Does using such incoherent macros really speed up your typing? I don’t think so. Never a fan of loop macros and rep macros :stuck_out_tongue:

8 Likes

I agree!

1 Like

I don’t have an answer but as suggested by akshmit and aneee I would remove that huge amount of macros

2 Likes

Yeah this is so weird , i mean if this was the reason for WA then what will the participant do?

1 Like

Corrected.
https://www.codechef.com/viewsolution/35717253

2 Likes

So it was undefined behaviour after all. That’s makes sense.

1 Like

I want to stand up for macros - I use a terrible IDE, so I need to write code fast to balance out the issues it causes :stuck_out_tongue:

(but definitely, when asking for help or showcasing code, try to get rid of them)

2 Likes

Just curious…which ide do you use by the way?

Yet another case where just compiling with sanitizers immediately gives away the issue. Compiling with -fsanitize=address,undefined and running against the sample shows it’s an out of bounds access. So it’s UB.

Specifically the a[ans] is out of bounds, indexed by ans = n+1. Your code randomly passed because it’s UB and you depend on what is just after your allocated memory (which can be anything).

(Unrelated, but please consider not using variable length arrays (array with a variable size). They are not standard C++, use a vector like sane people)

5 Likes

Isn’t accessing out of bounds of a vector also UB? I remember resizing a vector to n, and accessing v[n]. It gave some junk value.

Like I said, it’s unrelated. But usage of VLAs just bugs me. :wink:

Though with vector you can compile with flags like -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC which will notify you of an out of bounds access. With vector you have more tools at your disposal (and it’s standard C++).

For a reference of useful things to know, see CF post “Catching silly mistakes with GCC”.

4 Likes

This is nothing new :joy:

That kind of thing is almost exclusively due to code with undefined behavior.

1 Like

Please point out the specific lines. That wold be helpful.

I don’t know. I haven’t thoroughly looked at your code. I saw nothing obvious at a cursory glance, doesn’t mean there is nothing there though.

1 Like