Reverse Array Write a function that accepts an int array and the array\'s size a
ID: 3553857 • Letter: R
Question
Reverse Array
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.
Following is an example (shaded text are input by user):
::a.out
Input the size of array:
7
Input the content of array:
9 18 6 0 5 22 3
The contents of the original array are:
9 18 6 0 5 22 3
The contents of the copy are:
3 22 5 0 6 18 9
Explanation / Answer
#include <iostream>
using namespace std;
void showRevValues(int[], int); // Function Prototype
int main()
{
const int SIZE = 7;
int numbers [SIZE] = {1,4,8,15,65,77,94};
// Display the contents of the numbers array (for reference).
for (int count = 0; count < SIZE; count++)
{
cout << "The elements of the original array are: ";
cout << numbers[count];
}
showRevValues(numbers, SIZE);
return 0;
}
//***********************************************
// The showRevValues function will take the *
// original array elements and return those *
// same elements in the reverse order, however. *
//***********************************************
void showRevValues(int values[ ], int size)
{
int *numPtr; // Pointer
for (int count = 0; count < size; count++)
{
numPtr--;
cout << *numPtr << " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.