How to reverse the values stored in an array?

Okay so I have read in the values and stored them in an array. I can't figure out how to reverse those values stored in the array without declaring another array or just printing it out backwards. PLEASE HELP

#include <iostream>
#include <string>
using namespace std;
void display(string wordArray[], int low, int high);
int main() {

int low;
int high;
int range;
string words[20];


display(words, 0, 19);

cout<<"Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:"<<"\n";
cin>>low;
cin>>high;
range=(high-low) + 1;
cout<<"Enter "<<range<<" words:"<<"\n";
for (int i=low; i<= high; i=i+1) {
cout<< "[" <<i<<"]:"<<"\n";
cin>>words[i];
}

//REVERSE THE ARRAY

cout << "Before reversal:\n";
display(words, 0, 19);





cout << "After reversal:\n";
display(words, 0, 19);
return 0;
}

void display(string wordArray[], int low, int high) {
int i;
for (i = low; i <= high; i = i + 1) {
cout << "[" << i << "]: '" << wordArray[i] << "'\n";
}
}
Swap the first element with the last element. Then swap the second element with the second-to-last element. Continue to do this until you've swapped all the way down to the middle of the array. Then, your array should be the reverse of what it was when you started.
I tried this and it didn't work

cin.getline(words,19);
int count = cin.gcount();
for (int i = 0; i <count--; i++) {
swap(words[i], words[count]);
}
you need #include <algorithm> to use swap
I added that and for some reason it still won't work it gives me an error message
You need to copy and paste the exact error and show us the exact code and tell us what line the error is referring to. Also, use code tags:
http://www.cplusplus.com/articles/jEywvCM9/
Try this. Let me know if it matches your expected output.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>

using namespace std;

void display(string wordArray[], int low, int high);

int main() 
{
	int low;
	int high;
	int range;
	string words[20];
	string temp[20];

	cout << "Enter lower bound and upper bound"
		" so that 0 <= lower bound <= upper bound < 10:" << "\n";
	cout << "Lower Bound: ";
	cin >> low;
	cout << "Upper Bound: ";
	cin >> high;

	range = (high - low) + 1; 

	cout<<"\nEnter "<< range <<" words:"<<"\n";
	for (int i = low; i <= high; i = i + 1) 
	{
		cout << "[" << i << "]: ";
		cin >> words[i];
	}

	cout << "\nBefore reversal:\n";
	display(words, 0, 19);

	//REVERSE THE ARRAY
	for(int i = 0; i < 20; i++)
	{
		temp[i] = words[i];
	}

	for(int j = 0; j < 20; j++)
	{
		words[j] = temp[19 - j];
	}

	cout << "\nAfter reversal:\n";
	display(words, 0, 19);
	cin.ignore(256, '\n');
	cin.ignore(256, '\n');
	return EXIT_SUCCESS;
}

void display(string wordArray[], int low, int high) 
{
	int i;
	for (i = low; i <= high; i = i + 1)
		cout << "[" << i << "]: '" << wordArray[i] << "'\n";
}
In <algorithm>, there is also std::reverse which could be helpful: http://www.cplusplus.com/reference/algorithm/reverse/
or If you can use a 2nd array, read the first in reverse.
Topic archived. No new replies allowed.