I need help with a function with an array

Hi everybody,
I'm starting learning C++ and I don't understand why this code for mirroring a array is not working.
All kinds of help would be very appreciated.
#include <iostream>

void mirror (int v[],int n){
int s;
int temp;
for (s=0;s<n;s++){
temp=v[s];
v[s]=v[n-1-s];
v[n-1-s]=temp;

}
}


int main()
{
int i=0;
int h[]={2,3,4,7,8,9};
mirror(h,6);
for (i=0;i<6;i++){
std::cout <<h[i]<<'\n';
}
return 0;
}
Last edited on
I don't see any vectors here. A C++ vector is a kind of container. Best not to say vector unless you mean an actual C++ vector.

Anyway, your mirror function switches each pair around twice. It does this:

{2,3,4,7,8,9}
becomes
{9,3,4,7,8,2}
becomes
{9,8,4,7,3,2}
becomes
{9,8,7,4,3,2}
becomes
{9,8,4,7,3,2}
becomes
{9,3,4,7,8,2}
becomes
{2,3,4,7,8,9}
Thanks for your help, I know where my mistake is now! And I didn't know that about the vectors, I was just referring to a one-row array, also helpful information.
its a flaw. C++ should not have named vectors (the container) after a thing defined in math. They sort of can be a math vector, but they are really a generic container that could be a real vector. Meanwhile they renamed several pointless things (like null, which was just fine) and left vector (which is somewhat bad).

Topic archived. No new replies allowed.