Write a value returning function called Reverse that accepts a pointer to an int
ID: 3622912 • Letter: W
Question
Write a value returning function called Reverse that accepts a pointer to an int array and the arrays’s size as arguments. The function should create a copy of the array, except that the element values should be in reverse order in the copy. The function should return a pointer to the new reversed array.For example:
If the original array has the contents: 10 15 20 25 30 35 40 45 50 55 60
The second array (which should be created using dynamic memory allocation and a pointer) will contain 60 55 50 45 40 35 30 25 20 15 10
Use pointer notation whenever possible.
Explanation / Answer
#include<iostream.h>
int * reverseArray(int *a,int length)
{
int *revArray=new int[length];
for(int j=length-1;j>=0;j--)
{
revArray[length-(j+1)]=a[j];
}
return revArray;
}
int main()
{
int a[5]={10,15,20,25,30,35,40,45,50,55,60};
int *revArray;
int arrayLen=11;
cout<<" Ordinary array: ";
for(int i=0;i<arrayLen;i++)
{
cout<<a[i]<<" ";
}
revArray=reverseArray(a,arrayLen);
cout<<" Reverse Array: ";
for(i=0;i<arrayLen;i++)
{
cout<<revArray[i]<<" ";
}
return 1;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.