-
Reversing an array
I need to find out how to reverse an array.. here is the code
// REVERSE.CPP
//
// This program reverses the order of the elements in an array.
//
// Author: Leon Noel
#include <iostream.h>
#include "apvector.h"
void Reverse(apvector<int> &a);
int main()
{
apvector<int> test(6);
int i;
// Set the test values in the array:
test[0] = 1;
test[1] = 1;
test[2] = 2;
test[3] = 3;
test[4] = 5;
test[5] = 8;
// reverse the array:
Reverse(test);
// Display the array:
for (i = 0; i < 6; i++)
cout << test[i] << ' ';
cout << endl;
return 0;
}
void Reverse(apvector<int> &a)
// Reverses the elements of the array
{
int i, ...;
while {...}
}
}
-
Reply
Here is some code I quickly wrote in notepad, I HAVENT compiled this code yet.
Code:
const arrlimit = 6;
void Reverse(apvector<int> &a,apvector<int> &tmp)
{
int x = 0;
for (int i=arrlimit;i>0;i--)
{
a[i] = tmp[x];
x++;
}
}
-
for (int i=arrlimit-1;i>=0;i--)
And in case you're allowed to use proper C++ (many courses don't) here's what to do:
Code:
#include <vector>
#include <algorithm>
using std::vector;
template<typename T>
inline void Reverse(vector<T> &v) {
std::reverse(v.begin(), v.end());
}
And to put it parksie's way:
If your teacher doesn't like it send him/her here and we'll deal with it. The ap classes are evil and any course that thinks it is any good shouldn't use them. STL templates are the way to go.