0

Write a program which can interchange/swap elements in such a way that the first element is swapped with last element and so on.

Actually the question is Write a program which can interchange/swap elements in such a way that the first element is swapped with last element ,second element is swapped with the second last element and so on, only if anyone or both the elements are odd. E.g.If initially array of 7 elements is: 5,16, 4, 7, 19, 8, 2 After execution 2, 16, 19, 7, 4, 8, 5 https://code.sololearn.com/cgJMOUz638Em/?ref=app

21st Mar 2019, 6:31 PM
uday kiran
uday kiran - avatar
2 Respuestas
+ 7
First of all if you are taking array size by the user then you have to create dynamic array. Dynamic arrays are created on heap and their size is determined at runtime. Secondly, in your if condition replace ar[n-1] with ar[(n-1)-i] and the program should work. Third, you don't need to decrement n. Edit: Replace ar[n-1] with ar[(n-1)-i] everywhere in your code.
21st Mar 2019, 8:52 PM
blACk sh4d0w
blACk sh4d0w - avatar
0
Thanks for answering and why can't i use ar [n-1]
22nd Mar 2019, 4:18 PM
uday kiran
uday kiran - avatar