+ 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
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.
+ 1
is this as it is? A 2d array? or just 1d? Hmm a temp variable would do instead of another array
+ 1
Joni it is a 2D array but I can't figure out how to write the loops using temp variable!
+ 1
can you please help me by providing the code??