Answer to Question #203889 in C++ for Preeti

Question #203889

Write a function that reverses the elements of an array in place. The function must accept only one pointer value and return void. 



1
Expert's answer
2021-06-06T08:56:33-0400
#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;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog