This simplifed final exam will require you to submit a short program on Canvas,
ID: 3914560 • Letter: T
Question
This simplifed final exam will require you to submit a short program on Canvas, but will NOT require you to attend a proctored final exam session on the MCC campus. Write a function named, reverse, which The header for the function, reverse, is: reverses the order of an array's elements without creating another array. void reverse( int arr[), int len where arr is an array containing any number of integer elements and len 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 Your submission should be called, FinalExam.cpp, and must also 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: 1. 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 /I create the array to test int myArray[] " { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 M int myArrayRev[-9 8.7.6. 5,4.3.2.10 int arrLen 10 reverse( myArray for (int -0:&arrLenz; if (myArray[i)myArrayRevt) count++ f (countExplanation / Answer
Since this is a cpp program ideally we should program it in an object-oriented fashion. However, I have also added an alternate code(Version 2) which does not use classes and objects(non OOP). The core logic for reverse is the same in both. Both implementations are equally correct. Feel free to choose whatever suits you better.
Tested the code, comments and output have been added for better understanding.
Version 1:(Using classes and object)
code:
#include<iostream>
using namespace std;
class FinalExam//Class FinalExam
{
public:
void reverse(int arr[], int len);
};
void FinalExam::reverse(int arr[], int len)
{
/*A simple way of implementing array reverse without creating another array is by using 2 variable. One variable points at first index while the second variable points at the last index. Values at both indexes are swapped continuously while increasing the first variable and decreasing the second. We stop this process when both values are equal.
*/
int i,j,t;
for(i=0,j=len-1;i<len;i++,j--)
{
if(i<j)//Exit Condition
{
//Swapping 2 nos
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
int main()
{
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;
FinalExam ob;//Since we used a class, object creation is necessary
cout<<"Testing array 1 ";
ob.reverse(myArray,arrLen);
for(int i=0;i<arrLen;i++)
{
if(myArray[i] == myArrayRev[i])
count++;
}
if(count<arrLen)
cout<<"Error: Array 1 NOT reversed "<<endl;
else
cout<<"Array 1 successfully reversed "<<endl;
//Test Array 2
int myArray2[] = {10,20,30,40,50,60,70,80,90};//To avoid confusion I have used different variables
int myArrayRev2[] = {90,80,70,60,50,40,30,20,10};
arrLen = 9;count = 0;
cout<<"Testing array 2 ";
ob.reverse(myArray2,arrLen);
for(int i=0;i<arrLen;i++)
{
if(myArray2[i] == myArrayRev2[i])
count++;
}
if(count<arrLen)
cout<<"Error: Array 2 NOT reversed "<<endl;
else
cout<<"Array 2 successfully reversed "<<endl;
//Test Array 3
int myArray3[] = {10};
int myArrayRev3[] = {10};
arrLen = 1;count = 0;
cout<<"Testing array 3 ";
ob.reverse(myArray3,arrLen);
for(int i=0;i<arrLen;i++)
{
if(myArray3[i] == myArrayRev3[i])
count++;
}
if(count<arrLen)
cout<<"Error: Array 3 NOT reversed "<<endl;
else
cout<<"Array 3 successfully reversed "<<endl;
//Test Array 4
int myArray4[] = {};
int myArrayRev4[] = {};
arrLen = 0;count = 0;
cout<<"Testing array 4 ";
ob.reverse(myArray4,arrLen);
for(int i=0;i<arrLen;i++)
{
if(myArray4[i] == myArrayRev4[i])
count++;
}
if(count<arrLen)
cout<<"Error: Array 4 NOT reversed "<<endl;
else
cout<<"Array 4 successfully reversed "<<endl;
}
Output:
Testing array 1
Array 1 successfully reversed
Testing array 2
Array 2 successfully reversed
Testing array 3
Array 3 successfully reversed
Testing array 4
Array 4 successfully reversed
-----------------------------------------------------------------------------------------------------------------------------------------
Version 2:(Without using classes and object)
code:
#include<iostream>
using namespace std;
//No classes directly function and implementation
reverse(int arr[], int len)
{
int i,j,t;
for(i=0,j=len-1;i<len;i++,j--)
{
if(i<j)//Exit Condition
{
//Swapping 2 nos
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
int main()
{
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;
cout<<"Testing array 1 ";
reverse(myArray,arrLen);
for(int i=0;i<arrLen;i++)
{
if(myArray[i] == myArrayRev[i])
count++;
}
if(count<arrLen)
cout<<"Error: Array 1 NOT reversed "<<endl;
else
cout<<"Array 1 successfully reversed "<<endl;
//Test Array 2
int myArray2[] = {10,20,30,40,50,60,70,80,90};//To avoid confusion I have used different variables
int myArrayRev2[] = {90,80,70,60,50,40,30,20,10};
arrLen = 9;count = 0;
cout<<"Testing array 2 ";
reverse(myArray2,arrLen);
for(int i=0;i<arrLen;i++)
{
if(myArray2[i] == myArrayRev2[i])
count++;
}
if(count<arrLen)
cout<<"Error: Array 2 NOT reversed "<<endl;
else
cout<<"Array 2 successfully reversed "<<endl;
//Test Array 3
int myArray3[] = {10};
int myArrayRev3[] = {10};
arrLen = 1;count = 0;
cout<<"Testing array 3 ";
reverse(myArray3,arrLen);
for(int i=0;i<arrLen;i++)
{
if(myArray3[i] == myArrayRev3[i])
count++;
}
if(count<arrLen)
cout<<"Error: Array 3 NOT reversed "<<endl;
else
cout<<"Array 3 successfully reversed "<<endl;
//Test Array 4
int myArray4[] = {};
int myArrayRev4[] = {};
arrLen = 0;count = 0;
cout<<"Testing array 4 ";
reverse(myArray4,arrLen);
for(int i=0;i<arrLen;i++)
{
if(myArray4[i] == myArrayRev4[i])
count++;
}
if(count<arrLen)
cout<<"Error: Array 4 NOT reversed "<<endl;
else
cout<<"Array 4 successfully reversed "<<endl;
}
Please Comment if you need an edit or further clarifications.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.