How do I reverse the following Array using Class/Void structure?

How do I reverse the following Array using Class/Void structure?
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
// Array Class Will L.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <time.h>
using namespace std;

class ArrayClass
{
public:
ArrayClass();
void printArray();
void randArray(int upper, int lower);

private:
int a[100];
int n;
};

ArrayClass::ArrayClass()
	{
		
		n = 100;
		for (int i = 0; i < n; i++) a[i] = 0;

}
void ArrayClass::printArray()
	{
		std::cout << std::endl;
		for (int i = 0; i < n; i++)
			{
				if (i%10 == 0) std::cout << "\n" << a[i];
				else std::cout << "\t" << a[i];
			}
	}

void ArrayClass::randArray(int upper,int lower)
{
		

			cout << "What do you want the upper bound to be? " << endl;
			cin >> upper;
			cout <<"What do you want the lower bound to be? " << endl;
			cin >> lower;
	for (int i = 0; i < n; i++) a[i] = rand()%upper+lower;
	
		std::cout << std::endl;
		for (int i = 0; i < n; i++)
			{
				if (i%10 == 0) std::cout << "\n" << a[i];
				else std::cout << "\t" << a[i];
			}
	}


int main()
{
	srand(time(NULL));
	ArrayClass test;
	test.printArray();
	cout << endl;
	test.randArray(1,100);
	cout << endl;
	
	system("pause");
	return 0;
}

First of all I think it is bad form to put the result of a logical test on the same line as the test. If you continue to do this, you will often miss a breakpoint when you are using a debugger.

To answer your question, write a function that starts at the end of your array and prints out each character until you get to the beginning.
1
2
3
4
5
6
7
8
9
10
11
reverse_array(A, size):
    if size is less than or equal to 1 then
        return
    otherwise
        let b be a pointer to A
        let e be a pointer to (A + size - 1)

        swap elements at b and e

        reverse_array(A + 1, size - 2)
end
Last edited on
Topic archived. No new replies allowed.