+ 1

Program to swap consecutive elements without using another 1D array

For eg if array is 2 4 5 1 6 9 3 2 4 the function should change the array as 4 2 1 5 9 6 2 3 4 // last element in case of odd should be //retained

23rd Aug 2017, 11:05 AM
Soutik
Soutik - avatar
4 Answers
+ 2
For loops, I think you could do with using double for loops, one for the row and one for the column. Also because you swap the two consecutive elements immediately, you could use the temp variable to store the current element to be swapped. Then when you move on to the next items to be swapped, use the same temp var to store that element. You don't need to keep track of the previously swapped element.
23rd Aug 2017, 12:40 PM
Joni Jimenez
Joni Jimenez - avatar
+ 1
is this as it is? A 2d array? or just 1d? Hmm a temp variable would do instead of another array
23rd Aug 2017, 11:51 AM
Joni Jimenez
Joni Jimenez - avatar
+ 1
Joni it is a 2D array but I can't figure out how to write the loops using temp variable!
23rd Aug 2017, 12:00 PM
Soutik
Soutik - avatar
+ 1
can you please help me by providing the code??
23rd Aug 2017, 1:03 PM
Soutik
Soutik - avatar