Question

In: Computer Science

C++ Zybook LAB15.3.1: What order? (function templates Define two generic functions called GetVector() and CheckOrder(). GetVector()...

C++ Zybook LAB15.3.1: What order? (function templates

Define two generic functions called GetVector() and CheckOrder(). GetVector() takes a string of items delimited by spaces and an item of a generic type (i.e., char, int, double, or string) and returns a vector of the given item type. CheckOrder() checks if the vector of items is in ascending, neither, or descending order. The function should return 'a' if the items are in ascending order, 'u' if the items are unordered, and 'd' if the items are in descending order.

To test your functions, you are provided a main program which: Reads items from the input one line at a time, Uses a function called DataType() to determine the type of data (e.g., char, int, etc.) in the line, Constructs a vector of the appropriate type using your GetVector() function, and Then calls your CheckOrder() to output how the items are ordered. The items can be different types, including integers, strings, characters, or doubles.

Ex. If the input is: bat hat mat sat 63.2 196.5 123.5 the output is: Order: a Order: u

Code:

#include <string>
#include <sstream>
#include <iostream>
#include <vector>

using namespace std;

// TODO: Define a generic function called GetVector() that
// takes a string of items delimited by spaces and
// an item of a generic type (i.e., char, int, double,
// or string) and returns a vector of the given item
// type. The GetVector() function signature is below.
template <typename T>
vector<T> GetVector(string items, T item);

// TODO: Define a generic function called CheckOrder() that
// takes a vector of either char, double, int or string
// type as an argument and returns 'a' if the contents
// of the vector is ascending, 'd' if descending and 'u'
// if it is neither. A vector of zero or one items is
// considered 'u'. The CheckOrder() signature is below.
template <typename T>
char CheckOrder(vector<T> v);

char DataType(string items);

int main(int argc, char* argv[]) {
// Read in items
string items, str_item = "";
char char_item = ' ';
int int_item = 0;
double double_item = 0.0;;
vector<string> sv;
vector<int> iv;
vector<double> dv;
vector<char> cv;

getline(cin, items);
while (!cin.eof()) {
// check for the type of data: DataType returns 'c' for char,
// 'd' for double, 'i' for int, and 's' for string
char type = DataType(items);

// populate the vector
switch (type) {
case 'c': cv = GetVector(items, char_item);
cout << "Order: " << CheckOrder(cv) << endl;
break;
case 'd': dv = GetVector(items, double_item);
cout << "Order: " << CheckOrder(dv) << endl;
break;
case 'i': iv = GetVector(items, int_item);
cout << "Order: " << CheckOrder(iv) << endl;
break;
default : sv = GetVector(items, str_item);
cout << "Order: " << CheckOrder(sv) << endl;
break;
}
getline(cin, items);
}

return 0;
}

char DataType(string items) {
bool chars = true;
bool strings = true;
bool ints = true;
bool doubles = true;

for (auto c : items) {
if (isalpha(c)) { ints = false; doubles = false; }
if (c == '.') { ints = false; }
}

if (ints) { return 'i'; }
if (doubles) { return 'd'; }

stringstream ss(items);
string s;

while (ss >> s) {
if (s.length() > 1) { chars = false; }
}

if (chars) { return 'c'; }

return 's';
}

Solutions

Expert Solution

Hello! :)

Here is the completed code to solve the assignment:

prog.cpp:

#include <string>
#include <sstream>
#include <iostream>
#include <vector>

using namespace std;

// TODO: Define a generic function called GetVector() that
// takes a string of items delimited by spaces and
// an item of a generic type (i.e., char, int, double,
// or string) and returns a vector of the given item
// type. The GetVector() function signature is below.
template <typename T>
vector<T> GetVector(string items, T item);

// TODO: Define a generic function called CheckOrder() that
// takes a vector of either char, double, int or string
// type as an argument and returns 'a' if the contents
// of the vector is ascending, 'd' if descending and 'u'
// if it is neither. A vector of zero or one items is
// considered 'u'. The CheckOrder() signature is below.
template <typename T>
char CheckOrder(vector<T> v);

char DataType(string items);

int main(int argc, char* argv[]) {
    // Read in items
    string items, str_item = "";
    char char_item = ' ';
    int int_item = 0;
    double double_item = 0.0;;
    vector<string> sv;
    vector<int> iv;
    vector<double> dv;
    vector<char> cv;

    getline(cin, items);
    while (!cin.eof()) {
        // check for the type of data: DataType returns 'c' for char,
        // 'd' for double, 'i' for int, and 's' for string
        char type = DataType(items);

        // populate the vector
        switch (type) {
            case 'c': cv = GetVector(items, char_item);
                cout << "Order: " << CheckOrder(cv) << endl;
                break;
            case 'd': dv = GetVector(items, double_item);
                cout << "Order: " << CheckOrder(dv) << endl;
                break;
            case 'i': iv = GetVector(items, int_item);
                cout << "Order: " << CheckOrder(iv) << endl;
                break;
            default : sv = GetVector(items, str_item);
                cout << "Order: " << CheckOrder(sv) << endl;
                break;
        }
        getline(cin, items);
    }

    return 0;
}

char DataType(string items) {
    bool chars = true;
    bool strings = true;
    bool ints = true;
    bool doubles = true;

    for (auto c : items) {
        if (isalpha(c)) { ints = false; doubles = false; }
        if (c == '.') { ints = false; }
    }

    if (ints) { return 'i'; }
    if (doubles) { return 'd'; }

    stringstream ss(items);
    string s;

    while (ss >> s) {
        if (s.length() > 1) { chars = false; }
    }

    if (chars) { return 'c'; }

    return 's';
}

template <typename T>
vector<T> GetVector(string items, T item)
{
    vector<T> itemsVector;

    // parsing and inserting the items
    stringstream ss(items);
    T element;
    while(ss >> element)
    {
        itemsVector.emplace_back(element);
    }

    return itemsVector;
}

template <typename T>
char CheckOrder(vector<T> v)
{
    // initially v is neither ascending nor descending
    bool a{false};
    bool d{false};

    for(int i{1}; i < v.size(); ++i)
    {
        if(v[i - 1] < v[i])
        {
            a = true; // at least one ascending consecutive pair found
        }
        else if(v[i - 1] > v[i])
        {
            d = true; // at least one descending consecutive pair found
        }
    }

    if(a ^ d)
    {
        // only one of a and d is true
        return a ? 'a' : 'd';
    }
    else
    {
        // both a and d are true or false
        return 'u';
    }
}

Here is the snapshot of a demo run with the input you suggested and few other ones:

Hope this helps! :)


Related Solutions

In C++ please. 3. Define templates and explain their purpose. Differentiate standalone function and class templates....
In C++ please. 3. Define templates and explain their purpose. Differentiate standalone function and class templates. Give an example of a class template and a function template.
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and create a test to validate its functionality. The data consists of persons with the attributes of name, last name, age, height and weight. - Remembrer that, Their structure consists of: Head: Pointer to the first element of the queue Tail: Pointer to the last element of the queue And the following operations: Pop: Removes the element at the head Top: Returns the current element...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
Design two function templates as follows: - First function template will be used to sort arrays...
Design two function templates as follows: - First function template will be used to sort arrays of different data types in either descending or ascending order. This function template is required to have the following parameters: an array of a generic type, an integer representing the size of the array, and a character representing the order of sorting (i.e., ascending or descending). The last argument (i.e., the order of sorting) is required to be a default argument. The default order...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called in the code at the very bottom. So you should be able simply to run the script to test that your functions work as expected. ''' ''' * function name: say_hi * parameters: none * returns: N/A * operation: Uhh, well, just say "hi" when called. And by "say" I mean "print". * expected output: >>> say_hi() hi ''' ''' * function name: personal_hi...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called...
''' Problem 1: Formin' Functions Define and complete the functions described below. The functions are called in the code at the very bottom. So you should be able simply to run the script to test that your functions work as expected. ''' ''' * function name: say_hi * parameters: none * returns: N/A * operation: Uhh, well, just say "hi" when called. And by "say" I mean "print". * expected output: >>> say_hi() hi ''' ''' * function name: personal_hi...
c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?
c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to...
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to 10 different arrays of integers. Each array has 100 integer elements. The arrays are filled with random integer numbers.
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT