Array Manipulation

Hey guys, I want to copy my first Array to my second Array in reverse order.

Here is my code (I use Microsoft Visual Studious so the syntax may be different):

void copyArray1(int myArray1[], int myArray2[],
int numOfElements1, int numOfElements2)
{
numOfElements2 = numOfElements1;

for (int i = (numOfElements1 + 1); i >= 0; --i)
{
myArray2[i] = myArray1[i];
}

printArray2(myArray2, numOfElements2);
}

My concern is regarding the for loop. It won't reverse the array. When I do the for loop like this: for (int i = 0; i < numOfElements; ++i), it produces the right output, but when I compile my code that is higlighted bold, it doesn't change. Can anyone help me? Thank you for any help. By the way, I am new to C++ and this forum, so if I didn't pass by refernce or anything like that, can you please correct me? Thanks.
use code tags please, it makes everything easier to read.

1
2
3
4
5
6
7
for(int i 0; i < LEN; i++) //len represents length of array
{
   myarray2[i] = myarray1[LEN-i];
}

printArray2(myArray2, numOfElements2);


this will copy the first array in reverse order. Hope this helps
for inverse order you need to invert the index
a[i] = b[i] will just copy each element in the same order, you need to start with the highest and decrease till you reach 0

1
2
3
4
for( int i=0; i<LEN; ++i)
{
   a[LEN-i]=b[i];
}
K thanks guys and ill make sure to use those code tags next time
Last edited on
Topic archived. No new replies allowed.