Reversing the order of an array

I'm having a lot of trouble doing a homework assignment for class. The directions are as followed:

Copy and Reverse Fun..
Create two arrays of strings: ForwardNames, BackwardNames.
Initialize forwardOrderNames with: “Fred”, “Tuyet”, “Annie”, “Moe”, “Ria”, “Luke”, “Jim”, “May”, ”Rex”, ”Omar”.
Use two ‘for’ loops, to copy each element in the forwardOrderNames array into the reverse order
reverseOrderNames array in reverse order …
Print out the names in the forwardOrderNames array. 0 to 9 index show order listed above
Print out the names in the reverseOrderNames array. 0 to 9 index show reverse order listed above.

This is what I have so far, and I am able to reverse it by using the same array, but I don't know how to reverse it by utilizing the second array reverseOrderNames[10]; the way the instructions want me to do it. I am very lost, any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include <iostream>
#include <algorithm>
using namespace std;

int main(){

string forwardOrderNames[10] = { "Fred", "Tuyet", "Annie", "Moe", "Ria", "Luke", "Jim", "May", "Rex", "Omar" };
string reverseOrderNames[10];

for (int index = 0; index < 10; index++){

	cout << forwardOrderNames[index] << endl;
}

cout << "\n";
	
for (int index = 9; index >= 0; index--){
	
        cout << forwardOrderNames[index] << endl;

}

system("pause");
return 0;
}
Last edited on
Arrays start numbering at 0, so an array of size 10 will have valid elements 0 through 9. Your for loop runs up to and including 10, so that last loop tries to access an invalid array element.

It sounds like the assignment wants you to copy the name at forwardOrderNames[0] to reverseOrderNames[9]? Then increment through the elements in the forward array while decrementing through the reverse array?
Oh woops, that equal sign is not supposed to be there. And thats exactly what the assingment wants, but I don't know really where to start to do that.
Think about what needs to happen:
1
2
3
4
5
6
7
8
9
10
reverseOrderNames[0] = forwardOrderNames[9];
reverseOrderNames[1] = forwardOrderNames[8];
reverseOrderNames[2] = forwardOrderNames[7];
reverseOrderNames[3] = forwardOrderNames[6];
reverseOrderNames[4] = forwardOrderNames[5];
reverseOrderNames[5] = forwardOrderNames[4];
reverseOrderNames[6] = forwardOrderNames[3];
reverseOrderNames[7] = forwardOrderNames[2];
reverseOrderNames[8] = forwardOrderNames[1];
reverseOrderNames[9] = forwardOrderNames[0];


We want this to be in a loop:
1
2
for ( int index = 0; index < 10; ++index )
  reverseOrderNames[ index ] = forwardOrderNames[ /* ? */ ];


The question becomes "what one operation can be done to index such that all of the values in the un-looped example hold?"

As a hint:
1
2
3
4
5
6
7
8
9
10
reverseOrderNames[0] = forwardOrderNames[9]; // 0 + 9 = 9
reverseOrderNames[1] = forwardOrderNames[8]; // 1 + 8 = 9
reverseOrderNames[2] = forwardOrderNames[7]; // 2 + 7 = 9
reverseOrderNames[3] = forwardOrderNames[6]; // 3 + 6 = 9
reverseOrderNames[4] = forwardOrderNames[5]; // 4 + 5 = 9
reverseOrderNames[5] = forwardOrderNames[4]; // 5 + 4 = 9
reverseOrderNames[6] = forwardOrderNames[3]; // 6 + 3 = 9
reverseOrderNames[7] = forwardOrderNames[2]; // 7 + 2 = 9
reverseOrderNames[8] = forwardOrderNames[1]; // 8 + 1 = 9
reverseOrderNames[9] = forwardOrderNames[0]; // 9 + 0 = 9 
Last edited on
Thank you, that's really insightful. I came up with the following:

1
2
3
4
for (int index = 0; index < 10; index++){
	reverseOrderNames[index] = forwardOrderNames[9 - index];
	cout << reverseOrderNames << endl;
}


But when i run this, it outputs "0037F884" 10 times on my sreen rather than output the names in reverse order.
Last edited on
This is just printing out the memory address of the first element in the array.
cout << reverseOrderNames << endl;

You need to access the array elements individually to print them out - like what you did for printing out the forward array.
Oh wow, I feel like a total idiot now... Thank you wildblue. And thank you Lowest0ne for walking me through it. I'm still very new to C++, and really look forward to strengthening my coding skills. Thanks guys!
If the end goal is just to print in the reverse order you could simply do
1
2
3
4
for(int i = size - 1; i > -1; --i)
{
    std::cout << array[i] << ' ' << std::endl;
}


Though if you wish to actually reverse it you could do it the way you are.
That is what we're here for :)

The next thing to do is get rid of the number 10. Here we have a number that represents the amount of elements in an array. We even use the number 9, because that is one less than the amount of elements. If you want to change the amount of elements in the array, your going to have to remember which 10s need to be changed. If your program was large enough, it might be tricky to remember that the 9 is related to the 10 at all.

These are called "magic numbers", and they are bad.

One thing you can do is declare a constant:
1
2
3
4
5
6
7
const int size = 10;

string forwardNames[ size ] = { /* ... */ };
string backwardsNames[ size ];

for ( int i = 0; i < size; ++i )
  backwardsNames[i] = forwardNames[ size - 1 - index ];


Now, if you wanted to change the amount of elements in the array you just need to change size and adjust forwardNames accordingly.

However, even better is to learn the sizeof operator ( I think it is an operator ).

Anyway, sizeof is used to know, well, the size of something:
1
2
3
std::cout << sizeof( int ); // probably prints 4
std::cout << sizeof( char* ); // prints 4 or 8, depending upon 32 or 64 operating system
std::cout << sizeof( std::string );  // prints out 28 for me 


Arrays also know their size:
1
2
std::string names[] = { "Fred", "Tuyet", "Annie", "Moe", "Ria", "Luke", "Jim", "May", "Rex", "Omar" };  // notice I didn't declare the size of the array
std::cout << sizeof( names ); // prints 280 for me 


Arrays declared and initialized like this must have at least one element in them. With that in mind, the length of an array can be found via:
1
2
3
4
5
6
std::string names[] = { "Fred", "Tuyet", "Annie", "Moe", "Ria", "Luke", "Jim", "May", "Rex", "Omar" };
const int size = sizeof( names ) / sizeof( names[0] ); // size = 10
std::string otherNames[ size ];

for ( int i = 0; i < size; ++i )
  // whatever 


Edit, you also may notice I use std::. This is the preferred method. Basically, namespaces create a scope, and by saying using namespace std; you remove that scope and thus remove the whole purpose of namespace in the first place:

Bad
1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main( void )
{
  cout << "hello\n";
}


Good
1
2
3
4
5
6
#include <iostream>

int main( void )
{
  std::cout << "hello\n";
}
Last edited on
Very useful knowledge! But the way my professor is teaching this introductory course to c++, is to use "using namespace std;" for now. That's why I do it that way, but I do see many others who type out the standard notation on these forums; and leads me to believe, that typing std:: is the correct way of doing it. What kind of problems can arise by utilizing "using namespace std"?
It defeats the purpose of a namespace. Which is to avoid naming conflicts. There are hundreds if not thousands of things in the std namespace.
Those may be helpful for him in the future but this is for his homework assignment so it probably will not be accepted.
Topic archived. No new replies allowed.