Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Reversing an Array Write a program that prompts the user for an integer, then as

ID: 3761166 • Letter: R

Question

Reversing an Array Write a program that prompts the user for an integer, then asks the user to enter that many values. Store these values in an array and print the array. Then reverse the array elements so that the first element becomes the last element, the second element becomes the second to last element, and so on, with the old last element now first. Do not just reverse the order in which they are printed; actually change the way they are stored in the array. Do not create a second array; just rearrange the elements within the array you have. (Hint: Swap elements that need to change places.) When the elements have been reversed, print the array again.

Explanation / Answer

#include<iostream>

#include<vector>

using namespace std;

int main(){

   int n;

   cin>>n;

   vector<int> a[n];

   for(int i = 0; i < n; i++)

       cin>>a[i];

   for(int i = 0; i < n; i++)

       cout<<a[i]<<“ “;

   int left = 0,right = n-1

   while(left < right){

       temp = a[left];

       a[left] = a[right];

       a[right] = temp;

       left++;

       right—;

   }

for(int i = 0; i < n; i++)

       cout<<a[i]<<“ “;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote