3 ms·
You don't; it's basically, "here's an array, now reverse it" (keep in mind a pointer is not an array). http://www.lysator.liu.se/faq/c-faq/c-2.html http://www.
by downer 19y ago
You don't; it's basically, "here's an array, now reverse it" (keep in mind a pointer is not an array).
http://www.lysator.liu.se/faq/c-faq/c-2.html http://www.lysator.liu.se/faq/c-faq/c-2.html
The general point is that interviews of this type are counterproductive.
- neilc 19y agoAn array can be variably-sized in C (C99, that is). And thank you, I know the difference between an array and a pointer. You just seem to have arbitrarily interpreted the question to limit it to C89 and a fixed-size array in order to make your solution work.
- downer 19y agoThis has nothing to do with it being statically sized or variably sized. You have to know what size it is when you are going to reverse it. The solution presented works as long as the programmer knows what the size is. If you want to posit any kind of sane question, then you can have a sane solution.
- neilc 19y agoWell, suppose I have an array `A' of size `len'. How would you write an algorithm that reverses that array in place, without using any temporary variables, and that works for arbitrary values of A and len? I think the answer is "you can't". You can't claim to have a solution for the problem if it only works for a fixed input, and if the solution itself would be different for different inputs.
- downer 19y ago> I think the answer is "you can't". That's what I said a few posts ago. > You can't claim to have a solution for the problem if it only works for a > fixed input, and if the solution itself would be different for different > inputs. "Reverse this array". Done. "Reverse an unspecified array of arbitrary length" is a different problem :)