Search This Blog

Thursday, July 2, 2009

Reverse 2 dimentional array in C/C++

I need to reverse 2 dimentional array in C/C++.
Example: i need to reverse
10010110 t0 01101001
10000000 to 00000001

my 2 dimention array:

myArray[0][0] = 1
myArray[0][1] = 0
myArray[0][2] = 0
myArray[0][3] = 1
myArray[0][4] = 0
myArray[0][5] = 1
myArray[0][6] = 1
myArray[0][7] = 0

myArray[1][0] = 1
myArray[1][1] = 0
myArray[1][2] = 0
myArray[1][3] = 0
myArray[1][4] = 0
myArray[1][5] = 0
myArray[1][6] = 0
myArray[1][7] = 0

//in main
void main()
{

//call function
reverseArray(myArray, 2);

}

// function reverseArray
void reverseArray(char array[][8], int row) {
int i;
int j;
int size;
char swap;

for (j = 0; j<=(row-1); j++)
{

i=0;
size = 8;

for(i;i<--size;i++)
{
swap=array[j][i];
array[j][i]=array[j][size];
array[j][size]=swap;
}
}
}

this return the value i need. Hope this help

No comments: