Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Requirements Do not change any of the function names. Use pass by value, pass by reference and pass by constant reference appropriately. Add needed

C++

Requirements

Do not change any of the function names.

Use pass by value, pass by reference and pass by constant reference appropriately.

Add needed code as indicated in the skeleton program.

Update formal and actual parameters to match requirements shown in comments.

Remove system("pause");

See sample input and output files for guidance.

_________________________________________________

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

ifstream ifs;

ofstream ofs;

istream& getInputStream(bool interactive) {

if (interactive) { // set input stream to be the keyboard

return cin;

}

// otherwise set input stream to be a file

////////////////////////////////////////////////////////////////////////////////

// FIXME: Add code here to open input file

// there is an ifstream global variable named ifs that you should use

// get the filename from the keyboard and open ifs

// ask repeatedly until you successfully open the file

////////////////////////////////////////////////////////////////////////////////

return ifs;

}

ostream& getOutputStream(bool interactive) {

if (interactive) { // set output stream to be the console/screen

return cout;

}

// otherwise set output stream to be a file

////////////////////////////////////////////////////////////////////////////////

// FIXME: Add code here to open input file

// there is an ostream global variable named ofs that you should use

// get the filename from the keyboard and open ofs

// ask repeatedly until you successfully open the file

//////////////////////////////////////////////////////////////////////////////////

return ofs;

}

vector reverseVectorNew() {

///////////////////////////////////////////////////////////////////////////

// FIXME: You need to return a vector that is a reverse of the vector passed

// as a parameter

// - vector of ints

// You will need to determine whether each variable needs to be

// pass by value or pass by reference or pass by const reference

///////////////////////////////////////////////////////////////

}

void reverseVectorOriginal() {

///////////////////////////////////////////////////////////////////////////

// FIXME: You need to reverse the values in this vector permanently

// Add formal parameters in this order

// - vector of ints

// You will need to determine whether each variable needs to be

// pass by value or pass by reference or pass by const reference

///////////////////////////////////////////////////////////////

}

void printVector() {

///////////////////////////////////////////////////////////////////////////

// FIXME: You should print out the vector

// the label should be on a line and

// all numbers output with a space following the number on

// the following line

// Add formal parameters in this order

// - string with the label for the vector

// - vector of ints

// - ostream for the output stream to use

// - default parameter that indicates to reverse order of printing when true

// You will need to determine whether each variable needs to be

// pass by value or pass by reference or pass by const reference

///////////////////////////////////////////////////////////////

}

void printAllVectors() {

///////////////////////////////////////////////////////////////////////////

// FIXME: You should print out each vector in the list of vectors

// by using the printVector function. Don't duplicate the print Vector functionality.

// Each time you call the printVector function, you need to pass in "Vector #:" where

// # is replaced with the number. Getting the number to print correctly withe the

// output string is tricky. Put your vector number inside to_string(numToPrint) and this

// will handle the conversion of the int to a string for output.

// Add formal parameters in this order

// - vector of vector of ints

// - ostream for the output stream to use

// - default parameter that indicates to reverse order of printing when true

// You will need to determine whether each variable needs to be

// pass by value or pass by reference or pass by const reference

///////////////////////////////////////////////////////////////

}

int getVectorIndex(string prompt, int range, bool interactive, istream& is, ostream& os) {

// Interactive mode: 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

int vectorNum = 0;

if (interactive) {

while ((vectorNum < 1) || (vectorNum >(range))) {

os << prompt << " (";

for (int i = 0; i < range; ++i) {

os << (i + 1);

if (i != range - 1)

os << " ";

}

os << "): ";

is >> vectorNum;

if (is.fail()) {

is.clear(); // unset failbit

is.ignore(numeric_limits::max(), ' '); // skip bad input

os << "Must be an integer from the list in parenthesis." << endl;

}

os << endl;

}

return vectorNum - 1;

}

else {

bool error = false;

if (is >> vectorNum) { // read an integer

if (vectorNum < 1 || vectorNum > range) { // integer is out of bounds

error = true;

}

}

else // failed to read

error = true;

// reading from file must be correct, can't go back and get corrected input

if (error) {

os << "Invalid vector selection" << endl;

os << "Exiting program" << endl;

exit(EXIT_FAILURE); // exit immediately since the file doesn't work

}

// if we get this far, we have a valid vectorNum (1..range)

return vectorNum - 1; // adjust for zero based indexing

}

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::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::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 loadVector() {

///////////////////////////////////////////////////////////////

// FIXME: Add code to load a vector

// This code should work if in interactive mode or in file mode

// In interactive mode, prompt users with

// "Enter integers. When finished enter a letter."

// No prompts in file mode

// Read ints repeatedly until you fail to get an int.

// Add formal parameters in this order

// - bool for whether in interactive mode

// - istream for the input stream to use

// - ostream for the output stream to use

// You will need to determine whether each variable needs to be

// pass by value or pass by reference or pass by const reference

///////////////////////////////////////////////////////////////

}

vector loadRandomVector(int num, int min, int max) {

// only used for interactive program for testing purposes

vector 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 >& d, bool interactive, istream& is, ostream& os) {

if (interactive && getMakeRandom()) { // boolean short circuit

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(//FIXME));

}

}

void printMenu(int range) {

cout << endl << "Menu: " << endl;

cout << " A: Add Vector" << endl;

if (range > 0) { // need at least one vector

cout << " S: Sort Vector" << endl;

cout << " R: Reverse Vector Content" << endl;

cout << " Z: Get New Reversed Vector" << endl;

cout << " P: Print Vector (Front to Back)" << endl;

cout << " W: Print Vector (Back to Front)" << endl;

}

if (range > 1) { // need more than one vector

cout << " K: Print All Vectors (Front to Back)" << endl;

cout << " G: Print All Vectors (Back to Front)" << endl;

}

cout << " Q: Quit" << endl;

}

void processSelection(char selection, vector>& allVectors, bool interactive, istream& is, ostream& os)

{

string prompt = "";

bool addBlankLine = true;

int selectedVector = 0;

switch (selection) {

case 'A':

addVector(allVectors, interactive, is, os);

os << "Add vector " << allVectors.size() << endl;

break;

case 'S':

selectedVector = getVectorIndex("Which vector do you want to sort?", allVectors.size(), interactive, is, os);

sort(allVectors.at(selectedVector).begin(), allVectors.at(selectedVector).end());

os << "Sort vector " << selectedVector + 1 << endl;

break;

case 'R':

selectedVector = getVectorIndex("Which vector do you want to reverse?", allVectors.size(), interactive, is, os);

prompt = "Vector " + to_string(selectedVector + 1) + ": ";

reverseVectorOriginal(//FIXME); // FIXME: Add vector parameter/argument/assignment?

os << "Reverse vector " << selectedVector + 1 << endl;

break;

case 'Z':

selectedVector = getVectorIndex("Which vector do you want to copy into a new Vector and reverse?", allVectors.size(), interactive, is, os);

prompt = "Vector " + to_string(selectedVector + 1) + ": ";

allVectors.push_back(//FIXME); // FIXME: Add vector parameter/argument/assignment?

os << "Add vector " << allVectors.size() << " based on reversing vector " << (selectedVector + 1) << endl;

break;

case 'P':

selectedVector = getVectorIndex("Which vector do you want to print?", allVectors.size(), interactive, is, os);

prompt = "Vector " + to_string(selectedVector + 1) + ": ";

os << "Print" << endl;

printVector(//FIXME); // FIXME: Add vector parameter/arguments

break;

case 'W':

selectedVector = getVectorIndex("Which vector do you want to print?", allVectors.size(), interactive, is, os);

prompt = "Vector " + to_string(selectedVector + 1) + ": ";

os << "Print reversed" << endl;

printVector(//FIXME); // FIXME: Add vector parameter/arguments

break;

case 'K':

os << "Print all" << endl;

printAllVectors(//FIXME); // FIXME: Add vector parameter/arguments

break;

case 'G':

os << "Print all reversed" << endl;

printAllVectors(//FIXME); // FIXME: Add vector parameter/arguments

break;

default:

addBlankLine = false;

}

if (addBlankLine) os << endl;

}

int main() {

bool interactive = true; // set to false when ready to use files

istream &is = getInputStream(interactive);

ostream &os = getOutputStream(interactive);

vector< vector > allVectors;

char selection = ' '; // set to value that will do nothing when processed

do {

processSelection(selection, allVectors, interactive, is, os);

if (interactive) {

printMenu(allVectors.size());

os << endl << "Enter Choice: ";

//cout << "Num Vectors: " << v.size() << endl;

}

// get input from keyboard or file

is >> selection;

selection = toupper(selection);

// set to uppercase so we handle 'q' and 'Q'

if (interactive) os << endl;

} while (selection != 'Q' && is.good());

}

______________________________________________

Output File Ex.

Print all reversed No Vectors

Add vector 1

Add vector 2 based on reversing vector 1

Print all Vector 1: -6 -2 2 2 -9 -3 0 -2 -4 -7 Vector 2: -7 -4 -2 0 -3 -9 2 2 -2 -6

Add vector 3

Print Vector 3: 39 26 40 84 30 31 46 64 97 39 48 51 89 33 82 20 11 32 83 64 98 45 70 51 82

Reverse vector 3

Print Vector 3: 82 51 70 45 98 64 83 32 11 20 82 33 89 51 48 39 97 64 46 31 30 84 40 26 39

Print all reversed Vector 1: -7 -4 -2 0 -3 -9 2 2 -2 -6 Vector 2: -6 -2 2 2 -9 -3 0 -2 -4 -7 Vector 3: 39 26 40 84 30 31 46 64 97 39 48 51 89 33 82 20 11 32 83 64 98 45 70 51 82

Add vector 4 based on reversing vector 3

Print all Vector 1: -6 -2 2 2 -9 -3 0 -2 -4 -7 Vector 2: -7 -4 -2 0 -3 -9 2 2 -2 -6 Vector 3: 82 51 70 45 98 64 83 32 11 20 82 33 89 51 48 39 97 64 46 31 30 84 40 26 39 Vector 4: 39 26 40 84 30 31 46 64 97 39 48 51 89 33 82 20 11 32 83 64 98 45 70 51 82

Sort vector 2

Print Vector 2: -9 -7 -6 -4 -3 -2 -2 0 2 2

Add vector 5 based on reversing vector 2

Print all Vector 1: -6 -2 2 2 -9 -3 0 -2 -4 -7 Vector 2: -9 -7 -6 -4 -3 -2 -2 0 2 2 Vector 3: 82 51 70 45 98 64 83 32 11 20 82 33 89 51 48 39 97 64 46 31 30 84 40 26 39 Vector 4: 39 26 40 84 30 31 46 64 97 39 48 51 89 33 82 20 11 32 83 64 98 45 70 51 82 Vector 5: 2 2 0 -2 -2 -3 -4 -6 -7 -9

______________________________________________

Input File Ex.

k G a -6 -2 2 2 -9 -3 0 -2 -4 -7 e z 1 K a 39 26 40 84 30 31 46 64 97 39 48 51 89 33 82 20 11 32 83 64 98 45 70 51 82 s 3 P 3 r 3 p 3 g Z 3 k s 2 p 2 z 2 k

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

Draw and explain the operation of LVDT for pressure measurement

Answered: 1 week ago

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago