Friday, July 31, 2009

C++ prog to reverse an array?

template%26lt;class T%26gt;


void reverse(T *array, size_t size) {


int i;





for (i = 0; i %26lt; size / 2; ++i) {


T temp = array[i];


array[size - i - 1] = array[i];


array[i] = temp;


}





}

C++ prog to reverse an array?
There are three ways to look at this problem. Since this is easily Googled and sounds like your homework I'll say something on proper scalable code since this question is kinda vague.





1. Simply make another array and then read the values into it backwards.... kinda dumb IMHO since you double the amount of memory you need and that doesn't use the CPUs registers very effecently.





2. Look and see if there's a function to do it





3. Do it the cool kids way and use a pointer and do some memory tricks. (this way is the most scalable


solution!) http://www.cprogramming.com/snippets/sho...


No comments:

Post a Comment