1.

Write a function in C++, which accepts an integer array and its size as arguments and swap the elements of every even location with its following odd location.Example: if an array of nine elements initially contains the elements as 2, 4, 1, 6, 5, 7, 9, 23, 10 then the function should rearrange the array as 4, 2, 6, 1, 7, 5, 23, 9, 10

Answer»

void ElementSwap(int A[],int size)

{ int lim,tmp;

if(size%2!=0)  //if array has odd no. of element

lim = size-1;

else

lim = size;

for(int i=0;i<lim;i+=2)

{  tmp = A[i];

A[i] = A[i+1];

A[i+1]=tmp;

}

}



Discussion

No Comment Found

Related InterviewSolutions