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

Create a C++ program that creates random vectors based on user parameters, or al

ID: 669848 • Letter: C

Question

Create a C++ program that creates random vectors based on user parameters, or allows the user to input values. It also allows you to inverse the values in a vector, create a new vector that is an existing vector reversed, and a function with a default parameter for printing vectors normally and in reverse order. Note that when printing in reverse order, you should not modify the contents of the vector.

Complete the following three stubs so that the progam compiles, runs, and executes correctly:

reverseVectorNew: Must create a new vector and not modify the vector it is based on.

reverseVectorOriginal: Must modify an existing vector.

printVector: Uses a default parameter.

Any bugs are an exercise for you to solve!

#include <vector>

#include <iostream>

#include <algorithm>

#include <string>

using namespace std;

vector<int> reverseVectorNew()

{ // FIXME: What should return type be? update parameters }

void reverseVectorOriginal()

{ // FIXME: What should return type be? update parameters }

void printVector(string label, vector<int> v)

{ //FIXME: update parameters, use defaualt parameter for direction to print }

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

vector<int> v;

vector<int> reverseVectorNew(){
   vector<int> temp;
   while (v.size() > 0){
       temp.push_back(v.back());
       v.pop_back();
   }
   return temp;
}

void reverseVectorOriginal(){
   vector<int> temp;
   while (v.size() > 0){
       temp.push_back(v.back());
       v.pop_back();
   }
   v = temp;
}

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

int main(){
   int n,size;
   cout << "Enter the size of vector : ";
   cin >> size;
   for (int i = 0; i < 10; i++){
       cout << "Enter a number : ";
       cin >> n;
       v.push_back(n);
   }
   return 0;
}