Programming in C++ This is an assignment that utilizes .h file and the main .cpp
ID: 657291 • Letter: P
Question
Programming in C++
This is an assignment that utilizes .h file and the main .cpp file
Link to main .cpp file to test compiling: http://pastebin.com/B5KsGgDG
/*******************************************************************************
* Modify this file to create template functions and classes so that the tests
* in homework6_main.cpp pass.
*
* As you implement the tests remove the #define SKIPX statement to enable
* those tests in the testing file.
******************************************************************************/
#ifndef __HOMEWORK6_H__
#define __HOMEWORK6_H__
#include <string>
#include <utility>
#include <vector>
//Write a function named problem1 that accepts two arguments to the beginning
//and end iterators of a container. Return a std::pair whose first value is an
//iterator to the smallest element in the range and whose second value is an
//iterator to the largest element in the range. You can create a pair easily
//with the std::make_pair function: std::make_pair(x, y) returns a pair with x
//as the first elememnt and y as the second. This is a template function and
//should work with any containers.
#define SKIP1
//Write a function named problem2 that accepts two arguments: a two dimensional
//vector of a template type and a value of the sample template type as is
//stored in the 2D vector. Search for a value in the given two dimensional 2D
//vector and return the x,y coordinate of an element with that value or return
//std::make_pair(-1, -1) if the element is not present.
#define SKIP2
//Write a function named problem3 that accepts iterators to the beginning and
//end of a range and an element to search for. The function returns the number
//of steps taken if binary search were used to find the element or determine
//that it is not present. Base your code upon the code example from class.
#define SKIP3
//Write a template class named problem4 that stores a single value of the
//template type that is initialized to 0 in the class' null constructor.
//A member function named "save" accepts a single value of the template type
//and adds it to the class' variable.
//A member function named "spend" takes no arguments and returns the value of
//the variable and sets it to 0.
#define SKIP4
//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;
}
#define SKIP5
//Write a template function named problem6 that accepts a constant integer
//value as its template argument. Remember, the syntax for this is
//"template<int N>", 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.
#define SKIP6
//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.
#define SKIP7
#endif
Explanation / Answer
#ifndef __HOMEWORK6_H__
#define __HOMEWORK6_H__
#include <string>
#include <utility>
#include <vector>
using namespace std;
template <typename T> pair<T,T> problem1(T begin1,T end1)
{
T mini=begin1;
T maxi=end1;
for(T t=begin1;t!=end1;t++)
{
if(*mini>*t)
mini=t;
if(*maxi<*t)
maxi=t;
}
return make_pair(mini,maxi);
}
template <typename T> pair<int,int> problem2(vector< vector<T> > &matrix,T element)
{
if(matrix.size()==0)
return make_pair(-1,-1);
for(int i=0;i<matrix.size();i++)
{
for(int j=0;j<matrix[0].size();j++)
{
if(matrix[i][j]==element)
return make_pair(i,j);
}
}
return make_pair(-1,-1);
}
template <typename T> int problem3(typename vector<T>::iterator begin1,typename vector<T>::iterator end1,T key)
{
int numberOfSteps=1;
int start=0, end=end1-begin1-1;
int mid=start+(end-start)/2;
while(start<=end &&*(begin1+mid)!=key)
{
cout<<mid<<endl;
if(*(begin1+mid)<key)
start=mid+1;
else
end=mid-1;
mid=start+ (end-start)/2;
numberOfSteps++;
}// While Loop End
return numberOfSteps;
}
//Write a function named problem3 that accepts iterators to the beginning and
//end of a range and an element to search for. The function returns the number
//of steps taken if binary search were used to find the element or determine
//that it is not present. Base your code upon the code example from class.
template <typename T> class problem4
{
T var;
public:
problem4()
{
var=NULL;
}
void save(T x)
{
var+=x;
}
T spend()
{
T temp;
temp=var;
var=NULL;
return temp;
}
};
//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;
// }
#define SKIP5
template <int N> bool problem6(int n)
{
return (n%N==0);
}
// template <typename T> problem7(typename vector<T>::iterator begin1,typename vector<T>::iterator end1,T key)
//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.
#define SKIP7
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.