reverse array in functiuon

Anyone have an example of this? I am lost.

A function which takes an array of strings and reverses the order of the array. You cannot calculate its value using sizeof. So you will need to send a second argument with the size of the array. (And have a parameter ready to receive that input.)

all i have is this ->
http://cpp.sh/546oz
once you can use a vector, use std::reverse(myvector.begin(),myvector.end());

I expect the prof for this is looking for you to do it by hand, though.
for 1/2 the array, call std::swap on the first and last, then second and next to last, etc in a loop. If it has odd #, swapping the middle with itself is harmless.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
   vector<string> forward = { "alpha", "beta", "gamma", "delta", "epsilon" };
   vector<string> backward( forward.size() );
   reverse_copy( forward.begin(), forward.end(), backward.begin() );
   for ( string s : forward  ) cout << s << ' ';
   cout << '\n';
   for ( string s : backward ) cout << s << ' ';
}

Hello Gerardo559,

I agree with jonnin and lastchance that a vector is a better choice over the string array, but if you can not use it this may help.

I a not saying it is the best way to do this, but it does work.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <limits>
#include <string>

//#include <cmath>
//#include <cstdlib>
//#include <ctgmath>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

constexpr size_t MAXSIZE{ 6 };

std::string* reverseArray(std::string TheArray[]);

int main()
{

	std::string letters[MAXSIZE] // <--- Set up for testing. Remove the {}s and everything in between for normal run.
	{
		"This is string one.", // <--- Add or subtract lines as you want. Be sure to change "MAXSIZE" at the top.
		"This is string two.",
		"This is string three.",
		"This is string four.",
		"This is string five.",
		"This is string six"
	};

	std::string* lettersPtr{};

	//int size = sizeof letters / sizeof letters[0]; // <--- Not needed when using "MAXSIZE".

	std::cout << "\n     Before sort\n";

	for (auto& lc : letters)
		std::cout << "  " << lc << '\n';

	lettersPtr = reverseArray(letters);

	std::cout << "\n\n     Last Print in \"main\"";

	for (int lc = 0; lc < MAXSIZE; lc++)
		std::cout << "\n  " << *(lettersPtr + lc);

	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
}

std::string* reverseArray(std::string TheArray[])
{
	size_t index{};
	static std::string newArray[MAXSIZE];

	for (int lc = MAXSIZE-1; lc >= 0; lc--)
	{	
		newArray[index++] = TheArray[lc];
	}

	// <--- These next lines can be commented out or removed. The for loop in "main" replaces these lines.
	// <--- Mostly used for testing.
	
	//std::cout << "\n     After sort\n";

	//for (auto& lc : newArray)
	//	std::cout << "  " << lc << '\n';

	return newArray;

	//std::cout << std::endl; // <--- Used in debugging.
}

I rearranged your header files. The second group are no needed. "cmath" and "cstdlib", for me are included through "iostream" in VS, and there is nothing from the "cmath" header file that you are using in the program.

From what little I saw from the "ctgmath" header file it looks like code and functions that you would not be using for awhile if ever or something that would be used outside of school.

Lines 9 and 10 the comment and link should explain that.

In "main" I defined "letters" in this way for testing since you have no way of putting this information into the array other to hard code it for now.

The comment on line 31 should explain it.

Lines 33 - 36 may not be necessary, but good for debugging and testing.

The last bit of code before "return" is a fair replacement for system("pause");. Best not to use "system" anything.

In the function I changes the return type and since you have "MAXSIZE" in the global scope of the file you do not need to pass the size or even figure the size in the function.

Line 56 it eventually occured to me to make this "static" because even though you are returning the address of "newArray" the memory for the array is lost when the function ends. This way the memory will stay available when the function ends.

I realize that this program can be improved on, but it should give you an idea of what you can do.

Hope that helps,

Andy
To reverse an array, swap the first element with the last one. Then sway the second element with the next-to-last one, etc. Something like this:
1
2
3
4
5
6
7
8
9
    int limit = size/2;
    string tmp;

    for (int i=0; i<limit; ++i) {
        // This is the DIY way of doing std::swap(arr[i], arr[size-1-i]);
        tmp = arr[i];
        arr[i] = arr[size-1-i];
        arr[size-1-i] = tmp;
    }


Notice that the limit of the for loop is half way through the array if you went through the entire array then you'd reverse the array, and then reverse it again! The result would me no visible change.
Thanks for all the input guys. I think I got an idea of what to do.
Topic archived. No new replies allowed.