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();
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.
Using ONLY the following header functions: #include "cmpt_error.h" #include <iostream> #include <string> #include <vector> #include <cassert>...
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 { 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...
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++ 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...
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...
#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 = 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...
(10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std; // sorted array in descending order int list[SIZE] (23, 19, 17, 13, 11, 7, 5, 3, 1, 0); // recursively binary search the array list for key // return the index to list if key is found. else return -1 int recBinarySearch (int key) // Please implement the recursive function.. Please implement the C++ function recBinarySearch that recursively binary searches the value key in...
Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN = 1; const int MAX = 10; int getRandom(int low, int high); int main() { int random_num = 0; int player_num; int tries; int seed = static_cast<int>(time(0)); bool guessed = false; srand(seed); // call the getRandom function below tries = 4; while ( tries > 0 && !guessed ) { cout << "Enter a number within the range 1 to...
#include using namespace std; vector split_string(string); // Complete the findMedian function below. int findMedian(vector arr) { } int main() { ofstream fout(getenv("OUTPUT_PATH")); int n; cin >> n; cin.ignore(numeric_limits::max(), '\n'); string arr_temp_temp; getline(cin, arr_temp_temp); vector arr_temp = split_string(arr_temp_temp); vector arr(n); for (int i = 0; i < n; i++) { int arr_item = stoi(arr_temp[i]); arr[i] = arr_item; } int result = findMedian(arr); fout << result << "\n"; fout.close(); return 0; } vector split_string(string input_string) { string::iterator new_end = unique(input_string.begin(), input_string.end(), []...