Reversing functions

So for my last assignment I need to reverse the order of an array without creating another array. I have the header
void reverse (int arr[],int length)

So example if it were 74681 before reverse it should be 18647 after calling reverse. It should work for any length integer array. No keyboard input or no printing to console. I need to do the following using static initialization:

Pass an array of 10 elements then verify they are reversed upon returning from the function.

Pass an array of 9 elements then verify they are reversed upon returning from the function.

Pass an array of 1 element then verify its unchanged upon returning from the function.


Pass an array of 0 elements then verify its unchanged upon returning from the function.

I will post what I have so far later
roughly..

end = max_array_index;
beg = 0;
for(... arraysize/2 ...)
{
swap(array[beg], array[end]);
beg++; end--;
}

There are better ways but that is the straightforward algorithm. Just make sure that for odd values it is swapping the same value (IE swap(array[5],array[5]) and for even values this never happens, so it works for both even and odd sized arrays, if you do it right, assuming your swap can handle swapping something with itself.
Last edited on
This is what I have so far
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
#include <iostream>

using namespace std;

void reverse(int[], int);

int main() {

	int count;
	int Myarr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int arrReverse[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
	int arrLength = 10;

	reverse(Myarr, arrLength);

	for (int i = 0; i < arrLength; i++)

		if (Myarr[i] == arrReverse[i])
			count++;

	if (count < arrLength)
		cout << "Error: Array not reversed\n" << endl;

	else
		cout << "Array reversed!" << endl;

	return 0;
}

void reverse(int arr[], int length) {

	int temp, i;
	for (i = 0; i < length / 2; ++i) {
		temp = arr[length - i - 1];
		arr[length - i - 1] = arr[i];
		arr[i] = temp;
	}
	for (i = 0; i < length; ++i) {
		cout << arr[i] << " ";
	}

}


Im just not sure if Im doing whats being asked?
Last edited on
@Dmtg93

Im just not sure if Im doing whats being asked?

No, I don't so.

In your OP, you said "It should work for any length integer array.", but you specify the length of 10.
Also, "Pass an array of 9 elements then verify they are reversed upon returning from the function.", but all you've passed is an array of 10. Same with passing 1 or 0 length array.

How's this??
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
#include <iostream>

using namespace std;

void reverse (int arr[],int i);

int main()
{
	int i, x;
	do
	{
		cout << endl << "Enter a 0 to end program.." << endl;
		cout << endl << "MaxSize for the array, to equal what ? : ";
		cin >> i;
		int* arr = new int[i];
		if(i)
		{
			for (x = 0; x < i; x++)
			{
				arr[x] = rand() % 100;
			}
			
			cout << "Before reversing array contents.." << endl;
			for(int x=0;x<i;x++)
				cout << arr[x] << " ";
			reverse (arr,i);
			cout << endl << endl << "After reversing array contents.." << endl;
			for(int x=0;x<i;x++)
				cout << arr[x] << " ";
			cout << endl << endl;

			delete arr;
		}
	}while (i!=0);
	return 0;
}

void reverse (int arr[],int i)
{
	int split = i/2, holder;
	for(int x=0;x<split;x++)
	{
		holder=arr[x];
		arr[x]=arr[(i-x)-1];
		arr[(i-x)-1]=holder;
	}
}

Last edited on
whitenite,

There can't be any user input and we need to use static initialization. I believe we are just using main to test the reverse function under those conditions.
No keyboard input or no printing to console.
Pass an array of 10 elements then verify they are reversed upon returning from the function.

Those are a slightly awkward combination of requirements.

Certainly during development and testing I would use cout liberally to show what is going on.
However it seems there is a requirement for the program to verify its own results. That is, after calling the
reverse() function there should be code to compare the outcome with the expected result.

Unfortunately, because of the requirement of "no printing to console" the program won't be able to tell the outside world what it actually achieved?

Maybe I'm misunderstanding the requirements?
I would guess that no printing refers to the function, not main.
Otherwise, its total nonsense.
Agreed; it looks to me like it's directing the OP to limit the function just to doing what it's supposed to do (sensibly). The test driver program (i.e. the main function) handles the job of specifying the input to the function, and displaying the output.
Topic archived. No new replies allowed.