Write a function named. reverse. which reverses the order of an array\'s element
ID: 2084856 • Letter: W
Question
Write a function named. reverse. which reverses the order of an array's elements without creating another array. The header for the function, reverse, is: void reverse (int arr [], int len){... where arr is an array containing any number of integer elements and ten is the number of elements in the array. For example, if the array is {6, 4, 9, 1, 7} BEFORE calling reverse, then the array should be {7, 1, 9, 4, 6} AFTER calling reverse. Your code should work for any length of integer array containing any values. Your reverse function should not do any keyboard input and no printing to the console. include a main function that tests the function, reverse. The main function may do console output, but does not need any keyboard input. Arrays created in main for use in testing the reverse function should be created using static initialization as in the example below. Here are FOUR tests you should run from the main function by calling the reverse function: Pass an array with 10 elements and then verify the array elements are reversed upon returning from the function, reverse. Here is an example of doing this: //create the array to test int myArray [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9): int myArrayRev [] = (9, 8, 7, 6, 5, 4, 3, 2, 1, 0): int arrLen = 10: reverse (myArray): for (int i = 0: iExplanation / Answer
REQUIRED FUNCTION reverse()
void reverse(int arr[], int len)
{
if (len>1)
{
int t, j, n;
if (len%2 == 0)
n = len/2;
else
n = (len+1)/2;
for (j=0;j<n;j++)
{
t = arr[j];
arr[j] = arr[len-j-1];
arr[len-j-1] = t;
}
}
}
PART 1 CODE
#include <iostream>
using namespace std;
void reverse(int arr[], int len);
int main() {
//code
static int myArray[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int myArrayRev[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int arrLen = 10;
int count = 0;
reverse(myArray, arrLen);
for (int i=0; i<arrLen; i++)
{
if (myArray[i] == myArrayRev[i])
count++;
}
if (count < arrLen)
cout << "Error: array NOT reversed" <<endl;
else cout << "Array successfully reversed" <<endl;
return 0;
}
void reverse(int arr[], int len)
{
if (len>1)
{
int t, j, n;
if (len%2 == 0)
n = len/2;
else
n = (len+1)/2;
for (j=0;j<n;j++)
{
t = arr[j];
arr[j] = arr[len-j-1];
arr[len-j-1] = t;
}
}
}
PART 2 CODE
Change the number of elements in the array "myArray" and arrLen = 9.
#include <iostream>
using namespace std;
void reverse(int arr[], int len);
int main() {
//code
static int myArray[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int myArrayRev[] = {8, 7, 6, 5, 4, 3, 2, 1, 0};
int arrLen = 9;
int count = 0;
reverse(myArray, arrLen);
for (int i=0; i<arrLen; i++)
{
if (myArray[i] == myArrayRev[i])
count++;
}
if (count < arrLen)
cout << "Error: array NOT reversed" <<endl;
else cout << "Array successfully reversed" <<endl;
return 0;
}
void reverse(int arr[], int len)
{
if (len>1)
{
int t, j, n;
if (len%2 == 0)
n = len/2;
else
n = (len+1)/2;
for (j=0;j<n;j++)
{
t = arr[j];
arr[j] = arr[len-j-1];
arr[len-j-1] = t;
}
}
}
PART 3 CODE
Change the number of elements in the array "myArray" and arrLen = 1.
#include <iostream>
using namespace std;
void reverse(int arr[], int len);
int main() {
//code
static int myArray[] = {8};
int arrLen = 1;
reverse(myArray, arrLen);
for (int i=0; i<arrLen; i++)
{
cout << myArray[i] << endl; //to verify that the array is unchanged
}
return 0;
}
void reverse(int arr[], int len)
{
if (len>1)
{
int t, j, n;
if (len%2 == 0)
n = len/2;
else
n = (len+1)/2;
for (j=0;j<n;j++)
{
t = arr[j];
arr[j] = arr[len-j-1];
arr[len-j-1] = t;
}
}
}
PART 4 CODE
Change the array myArray to an empty array and arrLen = 0. There will be no output as the array is empty.
#include <iostream>
using namespace std;
void reverse(int arr[], int len);
int main() {
//code
static int myArray[] = {};
int arrLen = 0;
reverse(myArray, arrLen);
for (int i=0; i<arrLen; i++)
{
cout << myArray[i] << endl; //to verify that the array is unchanged
}
return 0;
}
void reverse(int arr[], int len)
{
if (len>1)
{
int t, j, n;
if (len%2 == 0)
n = len/2;
else
n = (len+1)/2;
for (j=0;j<n;j++)
{
t = arr[j];
arr[j] = arr[len-j-1];
arr[len-j-1] = t;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.