I cannot seem to figure this one out for the life of me. I have tried multiple w
ID: 3848289 • Letter: I
Question
I cannot seem to figure this one out for the life of me. I have tried multiple ways and have managed to get myself lost, any help will be appreciated.
Without using any std function templates, write a function to compare two lists of equal length, returning true if every integer in the first list is the negation of the corresponding element in the second list. For example, if the two lists contained [21, 0 -4] and [-21, 0, 4], the result would be true.
bool negationOf (const list<int>& L1,
const list<int>& L2);
Explanation / Answer
#include <iostream>
#include <list>
using namespace std;
bool negationOf (const list<int> &L1,
const list<int> &L2);
int main()
{
list<int> L1, L2;
L1.push_back(21);
L1.push_back(0);
L1.push_back(-4);
L2.push_back(-21);
L2.push_back(0);
L2.push_back(4);
cout<<"Result: "<<negationOf(L1, L2)<<endl;
return 0;
}
bool negationOf (const list<int> &L1,
const list<int> &L2) {
list<int>::const_iterator i,j;
for( i = L1.begin(), j = L2.begin(); i != L1.end(), j != L2.end(); ++i, ++j) {
if(*i != -*j){
return false;
}
}
return true;
cout << endl;
cout << endl;
}
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
Result: 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.