Question

Using ONLY the following header functions: #include "cmpt_error.h" #include <iostream> #include <string> #include <vector> #include <cassert>...

Using ONLY the following header functions:

#include "cmpt_error.h"
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
using namespace std;

Create a C++ function that satisfies the condition using recursion, NO WHILE OR FOR LOOPS.

Pre-condition:
     a.size() == b.size(), and a.size() > 0
Post-condition:
     Returns a vector equal to {min(a[0],b[0]), min(a[1],b[1]),
     min(a[2],b[2]), ..., min(a[n],b[n])}, where n == a.size().

For example, min_vec({3, 4, 1}, {2, 5, 2}) returns the new vector {2, 4, 1}.

These are the function headers:
vector<int> min_vec(const vector<int>& a, const vector<int>& b);
void min_vec_test();
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

//helper function

void help_min_vec(const vector<int>& a, const vector<int>& b,vector <int>& c,int idx)
{
   if(idx>=a.size()) return;
   c.push_back(min(a[idx],b[idx]));
   return help_min_vec(a,b,c,idx+1);
}
vector<int> min_vec(const vector<int>& a, const vector<int>& b)
{
   vector <int> c;
   help_min_vec(a,b,c,0);
   return c;
}
void min_vec_test(){
   cout<<"Testing min_vec... ";
   assert((min_vec({1},{2})== vector <int> {1}));
   assert((min_vec({1,10},{5,2}) == vector <int> {1,2} ));
   assert((min_vec({3,4,1},{2,5,2})== vector <int> {2,4,1}));
   cout<<"all min_vec tests passed!\n";
}

===========

I have tested it using :

Output:

=========

Since we are creating vectors using extended intializers use c++11 or above to run the code.

min_vec() functions calls another helper function help_min_vec() which takes four arguments. The first three are vector and last one is int. The purpose of variable idx is to maintain check on which element we have reached and push the current minimum value for that index. After that it is incremented and recursive call is made thus avoiding the use of the loops. The testing function uses assert() to check the min_vec() function returned output.

Please comment if you don't understand any part of code.

Add a comment
Know the answer?
Add Answer to:
Using ONLY the following header functions: #include "cmpt_error.h" #include <iostream> #include <string> #include <vector> #include <cassert>...
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
  • Using ONLY the following header functions: #include <iostream> #include <string> #include <cassert> #include <vector> using namespace...

    Using ONLY the following header functions: #include <iostream> #include <string> #include <cassert> #include <vector> using namespace std; Create a C++ function that satisfies the condition using recursion, NO WHILE OR FOR LOOPS. Pre-condition: s.size() == t.size() Post-condition: Returns a vector where the first string is the first character of s followed by the first character of t, the second string is the second character of s followed by the second character of t, and so on. For example, zip("abc", "xyz")...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • #include "stdafx.h" #include <iostream> #include <vector> #include <cassert> using namespace std; // function prototypes int loadDisk(vector<int>...

    #include "stdafx.h" #include <iostream> #include <vector> #include <cassert> using namespace std; // function prototypes int loadDisk(vector<int> &firstPeg, int numDisks); void printPeg(vector<int> &peg); int hanoi(struct pegType &startPeg, struct pegType &swapPeg, struct pegType &endPeg, int numDisk); void moveDisk(struct pegType &startPeg, struct pegType &endPeg); struct pegType {    vector <int> diskStack;    int name; }; int main() {    int numDisk = 7;    int i;    pegType peg1, peg2, peg3;    peg1.name = 1;    peg2.name = 2;    peg3.name = 3;...

  • Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n)...

    Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n) {     vector <int> v1;     // Print the number of 2s that divide n     while (n%2 == 0)     {         printf("%d ", 2);         n = n/2;         v1.push_back(2);     }     // n must be odd at this point. So we can skip     // one element (Note i = i +2)     for (int i = 3; i <=...

  • C++ Standard Deviation with arrays and vectors: #include <vector> #include <cmath> #include <iostream> using namespace std;...

    C++ Standard Deviation with arrays and vectors: #include <vector> #include <cmath> #include <iostream> using namespace std; void fillVector(vector <double> &); double average(const vector <double> &); int main() { return 0; } void fillVector(vector <double> &v) { double d; while (cin >> d) { v.push_back(d); } } double average(const vector <double> &v) { double sum = 0.; for (int i = 0; i < v.size(); i++) { sum += v.at(i); } return sum / v.size(); } standardDeviation() //stuck on this part...

  • C++ Implement the removeBad function: #include <list> #include <vector> #include <algorithm> #include <iostream> #include <cassert> using...

    C++ Implement the removeBad function: #include <list> #include <vector> #include <algorithm> #include <iostream> #include <cassert> using namespace std; vector<int> destroyedOnes; class Movie { public: Movie(int r) : m_rating(r) {} ~Movie() { destroyedOnes.push_back(m_rating); } int rating() const { return m_rating; } private: int m_rating; }; // Remove the movies in li with a rating below 50 and destroy them. // It is acceptable if the order of the remaining movies is not // the same as in the original list. void...

  • #include <cstring>       // for strlen() #include <cassert>       // for assert() #include <iostream>       //...

    #include <cstring>       // for strlen() #include <cassert>       // for assert() #include <iostream>       // for cout using namespace std; class MyString {    private:        char *m_data;        int m_length;    public:        MyString(const char *source = "")        {            assert(source); // make sure source isn't a null string            // Find the length of the string            // Plus one character for a terminator       ...

  • #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int...

    #include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int N = nums.size(); vector<int> result; vector<int> a(101); vector<int> b(101); for (int i = 0; i < N; i++) { a[nums[i]]++; // what does this mean? } for (int i = 1; i < 101; i++) { b[i] = a[i - 1] + b[i - 1]; } for (int i = 0; i < N; i++) { result.push_back(b[nums[i]]); } for (int i = 0; i...

  • #include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS =...

    #include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS = 8; vector <double> inverse(NUM_ITEMS); int j; double temp; for (int i = 0; i < NUM_ITEMS; i++) { inverse.at(i) = 1 / (i + 1.0); } cout << fixed << setprecision(2); cout << "Original vector..." << endl; for (int i = 0; i < NUM_ITEMS; i++) { cout << inverse.at(i) << " "; } cout << endl; cout << "Reversed vector..." << endl; for...

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