Question

(c++) Write a predicate function that checks whether two vectors have the same elements in the...

(c++) Write a predicate function that checks whether two vectors have the same elements in the same order.

Write a predicate function that checks whether two vectors have the same elements in the same order

bool equals(vector<int> a, vector<int> b)

provided code :

#include <iostream>
#include <vector>

using namespace std;

bool equals(vector<int> a, vector<int> b) {
// TODO
}

int main() {

// TODO

// Print Expected, "Vectors a and b are " (print "equal." if they are. Otherwise, print "not equal.") "\n"

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

#include <iostream>

#include <vector>

using namespace std;

bool equals(vector<int> a, vector<int> b) {

if (a.size() != b.size()) {

return false;

}

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

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

return false;

}

}

return true;

}

int main() {

vector<int> a {1, 2, 3, 4, 5};

vector<int> b {1, 2, 3, 4, 5};

vector<int> c{1, 2, 4, 3, 5};

if (equals(a, b)) {

cout << "a and b are equal" << endl;

} else {

cout << "a and b are not equal" << endl;

}

if (equals(a, c)) {

cout << "a and c are equal" << endl;

} else {

cout << "a and c are not equal" << endl;

}

return 0;

}

OUTPUT

​​​​​​​

Add a comment
Know the answer?
Add Answer to:
(c++) Write a predicate function that checks whether two vectors have the same elements in the...
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
  • In C++ Please for both questions and thank you! Write a predicate function named isEqual that...

    In C++ Please for both questions and thank you! Write a predicate function named isEqual that is passed in 2 vectors of integers and returns true if the vectors are equal, otherwise returns false Two vectors are equal if they have exactly the same values, all in exactly the same location within the vector. You may not use the overloaded vector operator ==, since that is the functionality you are to reproduce. Hint: At what point in your solution can...

  • This is a c++ code /**    Write a function sort that sorts the elements of...

    This is a c++ code /**    Write a function sort that sorts the elements of a std::list without using std::list::sort or std::vector. Instead use list<int>::iterator(s) */ using namespace std; #include <iostream> #include <iomanip> #include <string> #include <list> void print_forward (list<int>& l) { // Print forward for (auto value : l) { cout << value << ' '; } cout << endl; } void sort(list<int>& l) { write code here -- do not use the built-in list sort function }...

  • Fix the function so that it checks if the string is a palindrome #include <iostream> using...

    Fix the function so that it checks if the string is a palindrome #include <iostream> using namespace std; //Fix the function so that it checks if the string is a palindrome //(same forwards as backwards ex: racecar / radar) bool is_palindrome(string str, int i){ //base cases if (/* add the condition */) return false;    if (/* add the condition */) return true;    //recursive call return is_palindrome(str, i-1); } int main(){ string x; cin >> x;    if (is_palindrome(x,x.length())){...

  • Write a program that write a value-returning function, isVowel, that returns the value true if a...

    Write a program that write a value-returning function, isVowel, that returns the value true if a given character is a vowel and otherwise returns false. You must insert the following comments at the beginning of your program and write our commands in the middle: Write a C++ Program: /* // Name: Your Name // ID: Your ID */ using namespace std; #include <iostream> using namespace std; bool isVowel(char ch); int main() {     char ch;     …     …         ...

  • I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string>...

    I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string> using namespace std; class Triple { private: int a, b, c; public: Triple(); // all elements have value 0 Triple(int k); // all elements have value k Triple(int x, int y, int z); // specifies all three elements Triple(string s); // string representation is "(a,b,c)" string toString(); // create a string representation of the vector void fromString(string s); // change the vector to equal...

  • C++: questions are in fish.h. Write the code in fish.cpp and main.cpp #ifndef FISH_H #define FISH_H...

    C++: questions are in fish.h. Write the code in fish.cpp and main.cpp #ifndef FISH_H #define FISH_H #include <vector> class Fish { public: // (1 point) // Write code to initialize edible to is_edible, age to 0, size to 1. Fish (bool is_edible); // (1 point) // Print the vital stats (size and age) of the fish. void Print(); // (1 point) // If fish is at least the age of reproduce_age, reproduce with probability // of reproduce_probability. If reproduce, return...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • C++ myStack.lh stackADT.h Instructions main.cpp 1 #include«iostream» 2 #include<stdlib.h> 3 #include«conio.h> 4 #include«ostream» 5 using namespace std; 6 const int SIZE-5; //Stack size 7 //...

    C++ myStack.lh stackADT.h Instructions main.cpp 1 #include«iostream» 2 #include<stdlib.h> 3 #include«conio.h> 4 #include«ostream» 5 using namespace std; 6 const int SIZE-5; //Stack size 7 //class declaration 8 class stack Instructions Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same Overload the relational operatorfor the class stackType that returns true if two stacks of the same type are the same; it returns false...

  • C++ Write a function SwapArrayEnds() that swaps the first and last elements of the function's array...

    C++ Write a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The array's size may differ from 4. #include <iostream> using namespace std; /* Your solution goes here */ int main() {    const int SORT_ARR_SIZE = 4;    int sortArray[SORT_ARR_SIZE];    int i = 0;    sortArray[0] = 10;    sortArray[1] = 20;    sortArray[2] = 30;    sortArray[3] = 40;...

  • Declare a function that checks whether two strings are equal. In function main, read two strings,...

    Declare a function that checks whether two strings are equal. In function main, read two strings, call the function and print “equal” if the strings are equal and “not equal” otherwise. The input strings contain at most 50 characters. Example of input: alphabet alphabet Corresponding output: equal Example of input: week weak Corresponding output: not equal In C Language

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