Question

In: Computer Science

c++ code Reverse the order of elements in a vector of integers using two functions: (a)...

c++ code

Reverse the order of elements in a vector of integers using two functions:

(a) The first function takes the input vector a and return a vector b with the input data but in the reverse order, e.g. input: 1, 2, 3 and output 3, 2, 1.

(b) The second function is a recursive one and it reverses the input data in place (without using an additional vector).

Solutions

Expert Solution

#include <iostream>
#include <vector>

using namespace std;

/**
 *
 * @param vec vector to reverse elements. This vector remains unchanged
 * @return reverse of vector
 */
vector<int> reverseReturn(const vector<int> &vec) {
    vector<int> result;
    for(int i = vec.size()-1; i >= 0; --i) {
        result.push_back(vec[i]);
    }
    return result;
}

/**
 * changes the parameter vector to hold the reverse of elements
 * @param vec
 */
void reverse(vector<int> &vec) {
    int temp;
    for(int i = 0; i < vec.size()/2; ++i) {
        temp = vec[i];
        vec[i] = vec[vec.size()-i-1];
        vec[vec.size()-i-1] = temp;
    }
}

void print(vector<int> v) {
    for(int i = 0; i < v.size(); ++i) {
        cout << v[i] << " ";
    }
    cout << endl;
}

int main() {
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    cout << "Original vector: ";
    print(v);
    reverse(v);
    cout << "Reverse vector: ";
    print(v);
    cout << "Reverse of reversed vector: ";
    vector<int> v1 = reverseReturn(v);
    print(v1);
    return 0;
}

Related Solutions

Write code to reverse the order of elements on a stack S.c++ ii. Using one additional...
Write code to reverse the order of elements on a stack S.c++ ii. Using one additional queue. iii. using an additional stack and a non array variable c++ .
The following code was meant to print out the elements in an array in reverse order....
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {         reverse(a, index); What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of...
I want to copy all the elements in f1() to f2() but in reverse order (C++)...
I want to copy all the elements in f1() to f2() but in reverse order (C++) this is my code: #include <iostream> #include <iomanip> using namespace std; const int R=10; const int C=10; int Array[R][C] ; ///////////////////////////////////// void f1(){ for(int i=0 ; i<R ; i++){    for(int j=0 ; j<C ; j++){ Array[i][j]= (rand () %100) + 1; } } for(int i=0 ; i<R ; i++){    for(int j=0 ; j<C ; j++){    cout<<"["<<i<<"]["<<j<<"]="<<setw(3)<<Array[i][j]<<" ";    } cout<<endl; }...
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly.
  The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {          reverse(a, index); What does it do? Explain why it behaves in this way.                                    There is more than one error in the code. Correct the code so that it will recursively print out...
Directions: Using a vector of integers that you define. Write a C++ program to run a...
Directions: Using a vector of integers that you define. Write a C++ program to run a menu driven program with the following choices: 1) Display the ages 2) Add an age 3) Display the average age 4) Display the youngest age 5) Display the number of students who can vote 6) Remove all students less than a given age 7) Quit Make sure your program conforms to the following requirements: 1. Write a function called getValidAge that allows a user...
Using a vector of integers that you define. Write a C++ program to run a menu...
Using a vector of integers that you define. Write a C++ program to run a menu driven program with the following choices: 1) Display the ages 2) Add an age 3) Display the average age 4) Display the youngest age 5) Display the number of students who can vote 6) Remove all students less than a given age 7) Quit Make sure your program conforms to the following requirements: 2. Write a function called getValidAge that allows a user to...
Write a C++ code using while loop which is getting the two integers from the user,...
Write a C++ code using while loop which is getting the two integers from the user, and output how many numbers are multiples of 5, and how many numbers are multiples of 7 between the two integers (inclusive).
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Using c++, 1: Create a vector of 20 randomly generated integers, then 2. Create a new...
Using c++, 1: Create a vector of 20 randomly generated integers, then 2. Create a new vector that will only store the even numbers from the original vector. 3. Display the original vector and the vector of even integers to the console.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT