write the suitable C++ code that fits in the FIXME and also update parameter whe
ID: 671771 • Letter: W
Question
write the suitable C++ code that fits in the FIXME and also update parameter where required. 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 } void printAllVectors(vector< vector<int> > w, bool reverse = false) { for (int i = 0; i < w.size(); ++i) { string prompt = "Vector " + to_string(i + 1) + ": "; printVector(prompt, w.at(i), reverse); } } void printMenu(int range) { cout << endl << "Menu: " << endl; cout << " 1: Add Vector" << endl; if (range > 0) { // need at least one vector cout << " 2: Sort Vector" << endl; cout << " 3: Reverse Vector Content" << endl; cout << " 4: Get Reversed Vector" << endl; cout << " 5: Print Vector (Front to Back)" << endl; cout << " 6: Print Vector (Back to Front)" << endl; } if (range > 1) { // need more than one vector cout << " 7: Print All Vectors (Front to Back)" << endl; cout << " 8: Print All Vectors (Back to Front)" << endl; } cout << " Q: Quit" << endl; } int getSelectedVector(string prompt, int range) { // prompt user to provide the vector they want // to use. Also provides a list of available choices. // ask repeatedly until valid number selected. // range is the number of vectors available if (range > 1) { int vec = 0; while ((vec <= 1) || (vec > (range))) { cout << prompt << " ("; for (int i = 0; i < range; ++i) { cout << (i + 1) << " "; } cout << "): "; cin >> vec; if (cin.fail()) { cin.clear(); // unset failbit cin.ignore(numeric_limits<streamsize>::max(), ' '); // skip bad input cout << "Must be an integer from the list in parenthesis." << endl; } cout << endl; } return vec - 1; } else if (range == 1) { return 0; } return -1; } int getPositiveNum(string prompt) { // prompts user for a number and repeats until a positive number is input int num = 0; while (num <= 0) { cout << prompt << endl; cin >> num; if (cin.fail()) { cin.clear(); // unset failbit cin.ignore(numeric_limits<streamsize>::max(), ' '); // skip bad input } if (num <= 0) { cout << "Must be a positive number." << endl; } } return num; } int getNum(string prompt) { int num = 0; bool done = false; while (!done) { cout << prompt << " "; if (cin >> num) { done = true; } if (cin.fail()) { cin.clear(); // unset failbit cin.ignore(numeric_limits<streamsize>::max(), ' '); // skip bad input cout << "Must be an integer." << endl; } } return num; } bool getMakeRandom() { char choice = 'k'; while ((choice!='r')&&(choice!='l')) { cout << "'L'oad Manually or create 'r'andomly. (L/R) "; cin >> choice; cout << endl; choice = tolower(choice); // tolower allows input of uppercase letters and lets the // while loop only look at two values } switch (choice) { case 'R': case 'r': return true; } return false; } vector<int> loadVector() { vector<int> v; int userNum = 0; cout << "Enter integers. When finished enter a letter." << endl; while (cin >> userNum) { v.push_back(userNum); } cin.clear(); // unset failbit cin.ignore(numeric_limits<streamsize>::max(), ' '); // skip bad input return v; } vector<int> loadRandomVector(int num, int min, int max) { vector<int> v; if (min > max) { // if get bad values, just assume order was inverted // and swap max and min int temp = min; min = max; max = temp; } for (int i = 0; i < num; ++i) { int val = rand() % (max - min) + min; //cout << "Generated: " << val << endl; v.push_back(val); } return v; } void addVector(vector< vector<int> >& d) { if (getMakeRandom()) { int numElements = getPositiveNum("How many elements should the vector have ?"); int valueMin = getNum("What is the minimum value? "); int valueMax = getNum("What is the maximum value? "); d.push_back(loadRandomVector(numElements, valueMin, valueMax)); } else { d.push_back(loadVector()); } } int main() { vector< vector<int> > v; int selectedVector = 0; char selection = ' '; string prompt = ""; while ((selection != 'q') && (selection != 'Q')) { switch (selection) { case '1': addVector(v); break; case '2': selectedVector = getSelectedVector("Which vector do you want to sort?", v.size()); sort(v.at(selectedVector).begin(), v.at(selectedVector).end()); break; case '3': selectedVector = getSelectedVector("Which vector do you want to reverse?", v.size()); prompt = "Vector " + to_string(selectedVector + 1) + ": "; reverseVectorOriginal(v.at(selectedVector)); break; case '4': selectedVector = getSelectedVector("Which vector do you want to reverse and copy into a new Vector?", v.size()); prompt = "Vector " + to_string(selectedVector + 1) + ": "; v.push_back(reverseVectorNew(v.at(selectedVector))); cout << "Vector " << v.size() << " added based on reversing vector " << (selectedVector + 1) << endl; break; case '5': selectedVector = getSelectedVector("Which vector do you want to print?", v.size()); prompt = "Vector " + to_string(selectedVector + 1) + ": "; printVector(prompt, v.at(selectedVector)); break; case '6': selectedVector = getSelectedVector("Which vector do you want to print?", v.size()); prompt = "Vector " + to_string(selectedVector + 1) + ": "; printVector(prompt, v.at(selectedVector), true); break; case '7': printAllVectors(v); break; case '8': printAllVectors(v, true); break; } printMenu(v.size()); //cout << "Num Vectors: " << v.size() << endl; cout << endl << "Enter Choice: "; cin >> selection; cout << endl; } }
Explanation / Answer
#include<iostream>
#include<vector>
using namespace std;
vector<int> reverseVectorNew(vector<int> old) {
vector<int> newv;
for(int i = old.size() - 1; i >= 0 ; i--)
newv.push_back(old[i]);
return newv;
// FIXME: What should return type be? update parameters
}
void reverseVectorOriginal(vector<int> &old) {
vector<int> newv;
for(int i = old.size() - 1; i >= 0 ; i--)
newv.push_back(old[i]);
old = newv;
// FIXME: What should return type be? update parameters
}
void printVector(string label, vector<int> v,bool reverse) {
if(reverse){
for(int i = v.size() - 1 ; i >= 0; i--)
cout<<v[i]<<endl;
}
else{
for(int i = 0 ; i < v.size(); i++)
cout<<v[i]<<endl;
}
//FIXME: update parameters, use defaualt parameter for direction to print
}
void printAllVectors(vector< vector<int> > w, bool reverse = false) {
for (int i = 0; i < w.size(); ++i) {
string prompt = "Vector " + to_string(i + 1) + ": ";
printVector(prompt, w.at(i), reverse);
}
}
void printMenu(int range) {
cout << endl << "Menu: " << endl;
cout << " 1: Add Vector" << endl;
if (range > 0) { // need at least one vector
cout << " 2: Sort Vector" << endl;
cout << " 3: Reverse Vector Content" << endl;
cout << " 4: Get Reversed Vector" << endl;
cout << " 5: Print Vector (Front to Back)" << endl;
cout << " 6: Print Vector (Back to Front)" << endl;
}
if (range > 1) { // need more than one vector
cout << " 7: Print All Vectors (Front to Back)" << endl;
cout << " 8: Print All Vectors (Back to Front)" << endl;
}
cout << " Q: Quit" << endl;
}
int getSelectedVector(string prompt, int range) {
// prompt user to provide the vector they want
// to use. Also provides a list of available choices.
// ask repeatedly until valid number selected.
// range is the number of vectors available
if (range > 1) {
int vec = 0;
while ((vec <= 1) || (vec > (range))) {
cout << prompt << " (";
for (int i = 0; i < range; ++i) {
cout << (i + 1) << " ";
}
cout << "): ";
cin >> vec;
if (cin.fail()) {
cin.clear(); // unset failbit
cin.ignore(numeric_limits<streamsize>::max(), ' '); // skip bad input
cout << "Must be an integer from the list in parenthesis." << endl;
}
cout << endl;
}
return vec - 1;
}
else if (range == 1) {
return 0;
}
return -1;
}
int getPositiveNum(string prompt) {
// prompts user for a number and repeats until a positive number is input
int num = 0;
while (num <= 0) {
cout << prompt << endl;
cin >> num;
if (cin.fail()) {
cin.clear(); // unset failbit
cin.ignore(numeric_limits<streamsize>::max(), ' '); // skip bad input
}
if (num <= 0) {
cout << "Must be a positive number." << endl;
}
}
return num;
}
int getNum(string prompt) {
int num = 0;
bool done = false;
while (!done) {
cout << prompt << " ";
if (cin >> num) {
done = true;
}
if (cin.fail()) {
cin.clear(); // unset failbit
cin.ignore(numeric_limits<streamsize>::max(), ' '); // skip bad input
cout << "Must be an integer." << endl;
}
}
return num;
}
bool getMakeRandom() {
char choice = 'k';
while ((choice!='r')&&(choice!='l')) {
cout << "'L'oad Manually or create 'r'andomly. (L/R) ";
cin >> choice;
cout << endl;
choice = tolower(choice);
// tolower allows input of uppercase letters and lets the
// while loop only look at two values
}
switch (choice) {
case 'R':
case 'r':
return true;
}
return false;
}
vector<int> loadVector() {
vector<int> v;
int userNum = 0;
cout << "Enter integers. When finished enter a letter." << endl;
while (cin >> userNum) {
v.push_back(userNum);
}
cin.clear(); // unset failbit
cin.ignore(numeric_limits<streamsize>::max(), ' '); // skip bad input
return v;
}
vector<int> loadRandomVector(int num, int min, int max) {
vector<int> v;
if (min > max) { // if get bad values, just assume order was inverted
// and swap max and min
int temp = min;
min = max;
max = temp;
}
for (int i = 0; i < num; ++i) {
int val = rand() % (max - min) + min;
//cout << "Generated: " << val << endl;
v.push_back(val);
}
return v;
}
void addVector(vector< vector<int> >& d) {
if (getMakeRandom()) {
int numElements = getPositiveNum("How many elements should the vector have ?");
int valueMin = getNum("What is the minimum value? ");
int valueMax = getNum("What is the maximum value? ");
d.push_back(loadRandomVector(numElements, valueMin, valueMax));
}
else {
d.push_back(loadVector());
}
}
int main() {
vector< vector<int> > v;
int selectedVector = 0;
char selection = ' ';
string prompt = "";
while ((selection != 'q') && (selection != 'Q')) {
switch (selection) {
case '1':
addVector(v);
break;
case '2':
selectedVector = getSelectedVector("Which vector do you want to sort?", v.size());
sort(v.at(selectedVector).begin(), v.at(selectedVector).end());
break;
case '3':
selectedVector = getSelectedVector("Which vector do you want to reverse?", v.size());
prompt = "Vector " + to_string(selectedVector + 1) + ": ";
reverseVectorOriginal(v.at(selectedVector));
break;
case '4':
selectedVector = getSelectedVector("Which vector do you want to reverse and copy into a new Vector?", v.size());
prompt = "Vector " + to_string(selectedVector + 1) + ": ";
v.push_back(reverseVectorNew(v.at(selectedVector)));
cout << "Vector " << v.size() << " added based on reversing vector " << (selectedVector + 1) << endl;
break;
case '5':
selectedVector = getSelectedVector("Which vector do you want to print?", v.size());
prompt = "Vector " + to_string(selectedVector + 1) + ": ";
printVector(prompt, v.at(selectedVector));
break;
case '6':
selectedVector = getSelectedVector("Which vector do you want to print?", v.size());
prompt = "Vector " + to_string(selectedVector + 1) + ": ";
printVector(prompt, v.at(selectedVector), true);
break;
case '7':
printAllVectors(v);
break;
case '8':
printAllVectors(v, true);
break;
}
printMenu(v.size());
//cout << "Num Vectors: " << v.size() << endl;
cout << endl << "Enter Choice: ";
cin >> selection;
cout << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.