I don't know what is happening in this program, I am modifying an array of 6 elements with this function, which is supposed to swap the elements in this way:
ex array[6]={1,2,3,4,5,6}
;
output:
3 2 1 6 5 4
But instead, I am getting:
output:
3 2 1 4 5 6
Could you tell me what I am doing wrong please?
the function:
void invert (int *buf){
int i,j,swap;
for (i = 0 ; i < ( 3 - 1 ); i++)
{
for (j = 0 ; j < 3 - i - 1; j++)
{
swap = buf[j];
buf[j] = buf[j+1];
buf[j+1] = swap;
}
}
for (i =3 ; i < (6 - 1); i++)
{
for (j = 3 ; j < 6 - i - 1; j++)
{
swap = buf[j];
buf[j] = buf[j+1];
buf[j+1] = swap;
}
}
}
I called in the main like this:
main ( int argc , char * argv [])
{
int k;
int array[6]={1,2,3,4,5,6};
invert(array);
for(k=0;k<6;k++)
{
printf("%d ",array[k]);
}
printf("\n");
}
I apreciate your help