Vector v2; cout << \"Testing vector v2 with input data.\" << endl; v2.init_with_
ID: 3551052 • Letter: V
Question
Vector v2;
cout << "Testing vector v2 with input data." << endl;
v2.init_with_data(6, test_data, 3); // 5 4 2 0 0 0
cout << "Testing vector v2 size." << endl;
assert(6 == v2.get_size());
cout << "Testing vector v2 elements." << endl;
assert(test_data[0] == v2.get_elem(0));
assert(test_data[1] == v2.get_elem(1));
assert(test_data[2] == v2.get_elem(2));
assert(0 == v2.get_elem(3) == v2.get_elem(4) == v2.get_elem(5));
cout << "Testing vector v2 invalid index." << endl;
assert(-1 == v2.get_elem(13));
to fix the problem with the
function Vector::get_elem() so that it returns -1 when the passed position pos is outside allowed
range (consult the definition of the function Vector::set_elem()). W
Explanation / Answer
You just need to change the definition of "get_elem" function to this:
int get_elem(size_t pos){
if(pos >= size_t) return -1;
else return elems[pos];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.