Question: I need help translating this C++ for-loop function into a rescursive function. This is the function I have: int smallestDifference_optimized(const std::vector & a, const std::vector

I need help translating this C++ for-loop function into a rescursive function.

This is the function I have:

int smallestDifference_optimized(const std::vector& a, const std::vector& b) {

int tmp=100000; int total=0; if(a.size()==b.size()||a.size()

}

return total;

}

I need to implement this in a recurstive way.

This will be the shell of it:

int smallestDifference_recursive_utility(const std::vector& a, const std::vector& b, int aPos, int bPos) {

}

int smallestDifference_recursive(const std::vector& a, const std::vector& b) { return smallestDifference_recursive_utility(a, b, a.size(), b.size()); }

The main.cpp for this program is:

#include #include #include #include #include #include #include #include

#include "smallest_difference.h"

typedef int (*differenceFunction)(const std::vector&, const std::vector&);

void printSet(const std::vector& v, const std::string& name) { std::cout << "Set " << name << ": ["; std::copy(v.begin(), v.end() - 1, std::ostream_iterator(std::cout, ", ")); std::cout << v.back() << "]" << std::endl; }

void findDifference(const std::vector& a, const std::vector& b, differenceFunction func) { std::clock_t start = std::clock(); int d = func(a, b); std::clock_t end = std::clock(); float t = static_cast(end - start) / CLOCKS_PER_SEC; std::cout << "The smallest difference is " << d << ", found in " << std::fixed << std::setw(8) << std::setprecision(6) << std::setfill('0') << t << " seconds." << std::endl; }

int main(int argc, char** argv) { if (argc != 3) { std::cout << "Usage: " << argv[0] << " a-sequence-file b-sequence-file" << std::endl; return 0; }

std::ifstream aIn(argv[1]); std::ifstream bIn(argv[2]);

std::vector a; std::vector b;

std::copy(std::istream_iterator(aIn), std::istream_iterator(), std::back_inserter(a)); std::copy(std::istream_iterator(bIn), std::istream_iterator(), std::back_inserter(b));

std::cout << "Looking for the smallest difference between the following sets:" << std::endl; printSet(a, "a"); printSet(b, "b");

std::cout << "Using Recursive function: "; findDifference(a, b, smallestDifference_recursive); std::cout << std::endl; std::cout << "Using Amortized function: "; findDifference(a, b, smallestDifference_amortized); std::cout << std::endl;

std::cout << "Using Optimized function: "; findDifference(a, b, smallestDifference_optimized); std::cout << std::endl;

return 0; }

The smallest_difference.h file is:

#pragma once #ifndef __SMALLEST_DIFFERENCE_H__ #define __SMALLEST_DIFFERENCE_H__

#include

int smallestDifference_recursive(const std::vector& a, const std::vector& b);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!