Question

I was wondering how to do the following. This will all be in a header file...

I was wondering how to do the following. This will all be in a header file to be called in a .cpp file. They all have to be template functions because the .cpp file will test a few different values for each function. This should be done in C++

======

Write a variadic template for a function named problem5 that joins an arbitrary number of strings together. This function will serve as the terminating case for two strings and your variadic template version will extend that functionality to an arbitrary number of strings. std::string problem5(std::string a, std::string b) {return a+" "+b;}

======

Write a template function named problem6 that accepts a constant integer value as its template argument. Remember, the syntax for this is "template", where N is then a template parameter. The function itself accepts an int as its argument and returns true if the argument is divisible by the template argument and false otherwise.

======

Write a template function named problem7 that accepts iterators to the beginning and end of a range as the first two arguments and a function as the third argument. Sort the elements in the range from beginning and up to but not including end using the function argument instead of the less than operator.

=========================

Here is the code in the .cpp file to test the three functions above.

#ifndef SKIP5
        std::string str_a = "hello";
        std::string str_b = "world";
        std::string str_c = "goodbye";
        std::string str_d = "moon";
        if ("hello world goodbye moon" == problem5(str_a, str_b, str_c, str_d)) {
                std::cout<<"Test 5 passed.\n";
                score += 15;
        }
        else {
                std::cout<<"Test 5 failed.\n";
        }
#else
        std::cout<<"Skipping test 5 (remove the #define SKIP5 in the .h file)\n";
#endif

#ifndef SKIP6
        if (problem6<3>(6) and problem6<2>(100) and
                        not problem6<3>(1000) and not problem6<12>(44)) {
                std::cout<<"Test 6 passed.\n";
                score += 10;
        }
        else {
                std::cout<<"Test 6 failed.\n";
        }
#else
        std::cout<<"Skipping test 6 (remove the #define SKIP6 in the .h file)\n";
#endif

#ifndef SKIP7
        std::vector doubles = {1.1, 78.9, 2.2, 3.5, 1.13, 0, -8};
        std::vector chars = {'a', '2', '1', 'q', 'f'};
  auto double_comp = [](double a, double b) {return abs(a) < abs(b);};
  auto char_comp = [](char a, char b) {return a > b;};
        problem7(doubles.begin(), doubles.end(), double_comp);
        problem7(chars.begin(), chars.end(), char_comp);
        if (std::is_sorted(doubles.begin(), doubles.end(), double_comp) and
                        std::is_sorted(chars.begin(), chars.end(), char_comp)) {
                std::cout<<"Test 7 passed!\n";
                score += 20;
        }

#else
        std::cout<<"Skipping test 7 (remove the #define SKIP7 in the .h file)\n";
#endif
0 0
Add a comment Improve this question Transcribed image text
Answer #1

template <typename T>

bool problem6(T N , int arg)

{ if(arg/N==0)

{return true;}

else return false; }

template <typename T>

bool problem7(T beg , T end, void *func(T[]))

{int i=beg;

T temp=arr[i];

for (i = beg ; i <= end; i++) {

    temp=i;

    while ( temp > 0 && arr[temp] < array[temp-1]) {

     xyz          = arr[temp];

      array[temp]   = array[temp-1];

      array[temp-1] = xyz;

      temp--;

    }

void func(int arr[])

{//enter array

}


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
I was wondering how to do the following. This will all be in a header file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please put the following pseudocode into C++. Below the pseudocode is a header file as well...

    Please put the following pseudocode into C++. Below the pseudocode is a header file as well as a cpp file that needs to be included. // Start // Declarations // Automobile myAuto // string vin // string make // string model // string color // output "Please enter the Vehicle Identification Number: " // input vin // output "Please enter the Make: " // input make // output "Please enter the Mode: " // input model // output "Please enter...

  • You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function...

    You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function into a file named Q1.cpp. Q1.cpp should only include your function implementation, the necessary #include directives if needed, and should not contain anything else such as the main function or global variable declarations. Test your code using a separate main.cpp file where you implement a sufficient number of test cases. You should use Q1.h as a header file and Q1main.cpp as a main function...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • C++, Change the destroy_list function in the header file to a recursive destroy_list function, main is...

    C++, Change the destroy_list function in the header file to a recursive destroy_list function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();    void...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • Write a C++ program that includes the following: Define the class Student in the header file Stu...

    Write a C++ program that includes the following: Define the class Student in the header file Student.h. #ifndef STUDENT_H #define STUDENT_H #include <string> #include <iostream> using namespace std; class Student { public: // Default constructor Student() { } // Creates a student with the specified id and name. Student(int id, const string& name) { } // Returns the student name. string get_name() const { } // Returns the student id. int get_id () const { } // Sets the student...

  • 1. Write CppUnitLite tests to verify correct behavior for all the exercises. Using C++ 2. Please...

    1. Write CppUnitLite tests to verify correct behavior for all the exercises. Using C++ 2. Please show all outputs. Write functions to add one day, another function to add one month, and yet another function to add one year to a Date struct. struct Date { int year; int month; int day; }; Pass Dates by reference when appropriate (i.e., Date& or const Date&). For example, the following function returns by value a new Date instance with one day added...

  • Given the following code: #ifndef TREE_H #define TREE_H #include <iostream> #include "TreeNode.h" template< typename NODETYPE > class Tree { public: Tree() : rootPtr( nullptr ) {}...

    Given the following code: #ifndef TREE_H #define TREE_H #include <iostream> #include "TreeNode.h" template< typename NODETYPE > class Tree { public: Tree() : rootPtr( nullptr ) {} void insertNode( const NODETYPE &value ) { insertNodeHelper( &rootPtr, value ); } void preOrderTraversal() const { preOrderHelper( rootPtr ); } void inOrderTraversal() const { inOrderHelper( rootPtr ); } private: TreeNode< NODETYPE > *rootPtr; void insertNodeHelper( TreeNode< NODETYPE > **ptr, const NODETYPE &value ) { if ( *ptr == nullptr ) * ptr = new...

  • C++ The following task is to be submitted with one Header file that contains all of...

    C++ The following task is to be submitted with one Header file that contains all of the classes and two CPP or source files that contain the function definitions for the classes and the main. 1. Take what we did in class and create a File class for easy reading and writing access. The constructor should take a string and should open a file. 2. Create a function for writing bytes to a file. The function should be a template...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT