Write a function that reverses the elements of an array in place. The function must accept only one pointer value and return void.
#include <iostream>
using namespace std;
//a function that reverses the elements of an array in place. The function must accept only one pointer value and return void.
//The start point of the program
void reversArray(int* elements){
int startPosition=0;
int size = sizeof(elements);
while (startPosition < size)
{
int temp = elements[startPosition];
elements[startPosition] = elements[size];
elements[size] = temp;
startPosition++;
size--;
}
}
int main(){
int* elements;
int size;
cout<<"Enter size of array: ";
cin>>size;
elements=new int[size];
for(int i=0;i<size;i++){
cout<<"Enter element "<<(i+1)<<": ";
cin>>elements[i];
}
cout<<"\nArray elements:\n";
for(int i=0;i<size;i++){
cout<<elements[i]<<" ";
}
reversArray(elements);
cout<<"\n\nArray elements after reversing:\n";
for(int i=0;i<size;i++){
cout<<elements[i]<<" ";
}
cout<<"\n\n";
system("pause");
return 0;
}
Comments
Leave a comment