Eatclub Interview Question
ConsultantsCountry: United States
void reverseArrSect(int iArr[n],int iSec)
{
int iTemp = -1;
cout << endl << "reverse sections of array";
for(int i=0;i<n;i+=iSec)
{
iTemp = iArr[i];
iArr[i] = iArr[i+2];
iArr[i+2] = iTemp;
cout << iArr[i] <<','<< iArr[i+1] << ','<<iArr[i+2] << ',';
}
}
Very easy program. Written in c#
private static void ReverseArray() {
// in C#, given a list of numbers [4,8,20,11,12,18,50,10,11] and divided in sections of 3
// Reverse each section without using another array
int[] array = { 4,8,20,11,12,18,50,10,11 };
Console.WriteLine("Original array");
for(int i=0;i<array.Length;i++) {
Console.Write("{0} ", array[i]);
}
// Traverse the array stepping by 3
for (int i=0; i<array.Length; i+=3) {
// Just I need to swap first and third
int temp = array[i];
array[i] = array[i+2];
array[i+2] = temp;
}
Console.WriteLine("Reversed");
for(int i=0;i<array.Length;i++) {
Console.Write("{0} ", array[i]);
}
}
- ItsOnlyJenn December 13, 2015