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
  • 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...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

  • 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++ / 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...

  • 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...

  • This is a C++ programming question. Please provide the correct, workable code. Use the following three...

    This is a C++ programming question. Please provide the correct, workable code. Use the following three programs to help solve the problem. Provide comments throughout the code. Problem to solve: Code 1: Complex.h #pragma once #ifndef COMPLEX_H #define COMPLEX_H class Complex { private:    double real;    double imag; public:    // initialize the complex number to 0.0    Complex() : real(0.0), imag(0.0) {}    // initialize the complex number at declaration or new    Complex(double r, double i) :...

  • 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...

  • How to code up the postfixEval function using the instructions in the comments of the function?...

    How to code up the postfixEval function using the instructions in the comments of the function? // postfixEvalTest.cpp #include "Token.h" #include <iostream> #include <vector> #include <stack> using namespace std; // postfix evaluate function prototype double postfixEval(vector<Token> &expr); int main() { vector<Token> postfix; postfix.push_back(Token(VALUE,4.0)); postfix.push_back(Token(VALUE,3.0)); postfix.push_back(Token(VALUE,2.0)); postfix.push_back(Token(OPERATOR,'*')); postfix.push_back(Token(OPERATOR,'+')); postfix.push_back(Token(VALUE,18.0)); postfix.push_back(Token(VALUE,6.0)); postfix.push_back(Token(OPERATOR,'/')); postfix.push_back(Token(OPERATOR,'-')); double result = 0.0; //result = postfixEval(postfix); cout << "The result of 4 3 2 * + 18 6 / - .... is " << result << endl; vector<Token>...

  • 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**...

  • 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...

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
ADVERTISEMENT