Reversing an array: which method is efficient
ok i have figured out where i was wrong and have come up with this code here:
for (int i=0;i<=MAX/2;i++)
{
int temp;
temp = arr[i];
arr[i] = arr[(MAX-1)-i];
arr[(MAX-1)-i] = temp;
}
where MAX is the total number of elements an array has.
Another piece of code that i have found was this one, i like you to comment whether this one is efficient or the one above.
void Reverse(int *first,int *last)
{
int dum;
for(int x=0;((x>=0) && (x<=((last-first)/2)));x++)
{
dum=*(first+x);//store value of location first+x in dummy var
*(first+x)=*(last-(1+x)); // set loc first+x to whats in last-x
*(last-(1+x))=dum; // assign mem loc last-x dum
}
}
__________________
MAXOOD!
Life is an endless journey towards perfection
|