Question

C++ Language:

Do not use namespace3-D vectors Write a templated function, called add 3d, that can add two const references to 3-d vectors [vector<vector-vect

Test Case:

INPUT OF THE TEST CASE 1 #include <vector> 2 using std: : vector; 3 const vector<double> a{1, 4, -6}; const vector<double> b{

3-D vectors Write a templated function, called "add 3d", that can add two const references to 3-d vectors [vector>]. It should return a new 3-d vector that performed element-wise addition. You cannot use indexing on this problem, instead you must use iterators to access the values of end vector.
INPUT OF THE TEST CASE 1 #include 2 using std: : vector; 3 const vector a{1, 4, -6}; const vector b{3, 6, 2}; const vector expected{1, 3, 4, 6, -6, 2}; 6 zipper_merge(a, b); 8 vector result 9 ASSERT_EQ(result, expected); 10 11 const vector a2{'1', '4', '6'}; 12 const vector b2{'3', '6', '2'}; 13 const vector expected2{'1', '3', '4', '6', '6', '2'}; 14 15 vector result2 = zipper_merge(a2, b2); 16 ASSERT_EQ(result2, expected2);
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

#include <iostream>

#include <vector>

using std::vector;

vector<double> zipper_merge(vector<double> a, vector<double> b)

{

vector<double> result;

int i;

for (i = 0; i < a.size() && i < b.size(); i++)

{

result.push_back(a[i]);

result.push_back(b[i]);

}

if (i < a.size())

{

for (; i < a.size(); i++)

{

result.push_back(a[i]);

}

}

else if (i < b.size())

{

for (; i < b.size(); i++)

{

result.push_back(b[i]);

}

}

return result;

}

vector<char> zipper_merge(vector<char> a, vector<char> b)

{

vector<char> result;

int i;

for (i = 0; i < a.size() && i < b.size(); i++)

{

result.push_back(a[i]);

result.push_back(b[i]);

}

if (i < a.size())

{

for (; i < a.size(); i++)

{

result.push_back(a[i]);

}

}

else if (i < b.size())

{

for (; i < b.size(); i++)

{

result.push_back(b[i]);

}

}

return result;

}

void ASSERT_EQ(vector<double> a, vector<double> b)

{

for (int i = 0; i < b.size(); i++)

{

if (a[i] != b[i])

{

std::cout << "Test case failed!" << std::endl;

return;

}

}

std::cout << "Test case passed!" << std::endl;

}

void ASSERT_EQ(vector<char> a, vector<char> b)

{

for (int i = 0; i < b.size(); i++)

{

if (a[i] != b[i])

{

std::cout << "Test case failed!" << std::endl;

return;

}

}

std::cout << "Test case passed!" << std::endl;

}

int main()

{

const vector<double> a{1, 4, -6};

const vector<double> b{3, 6, 2};

const vector<double> expected{1, 3, 4, 6, -6, 2};

vector<double> result = zipper_merge(a, b);

ASSERT_EQ(result, expected);

const vector<char> a2{'1', '4', '6'};

const vector<char> b2{'3', '6', '2'};

const vector<char> expected2{'1', '3', '4', '6', '6', '2'};

vector<char> result2 = zipper_merge(a2, b2);

ASSERT_EQ(result2, expected2);

return 0;

}

Output:

Test case passed! Test case passed!

Add a comment
Know the answer?
Add Answer to:
C++ Language: Do not use namespace Test Case: 3-D vectors Write a templated function, called "add 3d", that can...
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
  • Write this function in c++.Use of string and <cstring> is not allowed .You are required to...

    Write this function in c++.Use of string and <cstring> is not allowed .You are required to use char* and implement the use of ** in this question . Gtest case to pass: #include "q1.cpp" #include <gtest/gtest.h> //-------------------Q1_8----------------- TEST(Question1_8, First) { char t1[]="Hello World"; char res1[] = "Hello"; char res2[] = "World"; char** r= StrTok(t1,' '); ASSERT_EQ(0, strcmp(r[0],res1) ); ASSERT_EQ(0, strcmp(r[1],res2) ); } TEST(Question1_8, Second) { char t1[]="Hello?World?OOP"; char res1[] = "Hello"; char res2[] = "World"; char res3[] = "OOP"; char**...

  • 22. (a) Find two vectors that span the null space of A 3 -1 2 -4 (b) Use the result of part (a) to find the matrix that projects vectors onto the null space of A. (c) Find two orthogonal vectors...

    22. (a) Find two vectors that span the null space of A 3 -1 2 -4 (b) Use the result of part (a) to find the matrix that projects vectors onto the null space of A. (c) Find two orthogonal vectors that span the null space of A. (d) Use the result of (c) to find the matrix that projects vectors onto the nul space of A. Compare this matrix with the one found in part (a). (e) Find the...

  • C++ Language: TestCase: Write a function, called "sort Lbefore e that can sort words into whether or not they conf...

    C++ Language: TestCase: Write a function, called "sort Lbefore e that can sort words into whether or not they conform to the 1 beforee, except after c rule, See htrps//en.oxtorddictionaries.com/spelling/-before-e-except-after-cl. This function takes 4 references to vectors of strings as parameters . The first vector is the input consisting of lowercase words (only alphabetic charactersl. The next vector should be filled with words that follow the rule The next vector should be filled with words that don't have ie or...

  • In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete t...

    In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete the requested function. You may not use ANY library functions or include any headers, except for <cstddef> for size_t. */ #include <cstddef> // size_t for sizes and indexes ///////////////// WRITE YOUR FUNCTION BELOW THIS LINE /////////////////////// ///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE /////////////////////// // These are OK after...

  • C++Language: The question is not allowed to use STL. Also, plz use noskipws and push_back for the question.The input is...

    C++Language: The question is not allowed to use STL. Also, plz use noskipws and push_back for the question.The input is more than one, so the code should be able to run more than once Strings are vectors words on that line and the line itself. What's a word? It is a series of alphabetic characters, separated by whitespace other non-alphabetic characters. Write a program that reads from cin and tor each line, outputs number Pretty easy huh, well, you aren't...

  • C++ / Visual Studio Implement the member function below that returns true if the given value...

    C++ / Visual Studio Implement the member function below that returns true if the given value is in the container, and false if not. Your code should use iterators so it works with any type of container and data. template< typename Value, class Container > bool member( const Value& val, const Container& cont ); just implement this function in the code below #include <iostream> #include <array> #include <deque> #include <list> #include <set> #include <string> #include <vector> using namespace std; #define...

  • 2 5 Do the vectors u = and v= 3 7 span R3? -1 1 Explain! Hint: Use Let a, a2,ap be vectors in R", let A = [a1a2..ap...

    2 5 Do the vectors u = and v= 3 7 span R3? -1 1 Explain! Hint: Use Let a, a2,ap be vectors in R", let A = [a1a2..ap The following statements are equivalent. 1. ai,a2,..,a, span R" = # of rows of A. 2. A has a pivot position in every row, that is, rank(A) Select one: Oa. No since rank([uv]) < 2 3=# of rows of the matrix [uv b.Yes since rank([uv]) =2 = # of columns of...

  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...

  • Thank you for your help! Homework 02 - Vectors, Strings, and Masking Function Name: badWeatherman Inputs: 1. (logical)...

    Thank you for your help! Homework 02 - Vectors, Strings, and Masking Function Name: badWeatherman Inputs: 1. (logical) Vector of suspect #1's answers to a lie detector (logical) Vector of suspect #2's answers to a lie detector (logical) Vector of suspect #3's answers to a lie detector (logical) Vector of suspect #4's answers to a lie detector 2" 3. 4. Outputs 1. (char) Sentence stating which suspect stole the fruit Banned Functions: isequal (, isequaln( Background Oh no, there has...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

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