Modify the existing vector\'s contents, by erasing 200, then inserting 100 and 1
ID: 3762426 • Letter: M
Question
Modify the existing vector's contents, by erasing 200, then inserting 100 and 102 in the shown locations. Use Vector ADT's erase() and insert() only. Sample output of below program:
#include <iostream>
#include <vector>
using namespace std;
void PrintVectors(vector<int> numsList) {
int i;
for (i = 0; i < numsList.size(); ++i) {
cout << numsList.at(i) << " ";
}
cout << endl;
}
int main() {
vector<int> numsList;
numsList.push_back(101);
numsList.push_back(200);
numsList.push_back(103);
/* Your solution goes here */
PrintVectors(numsList);
return 0;
}
2)Write a unit test for addInventory(). Call redSweater.addInventory() with parameter sweaterShipment. Print the shown error if the subsequent quantity is incorrect. Sample output for failed unit test given initial quantity is 10 and sweaterShipment is 50:
Note: UNIT TEST FAILED is preceded by 3 spaces.
#include <iostream>
using namespace std;
class InventoryTag {
public:
InventoryTag();
int getQuantityRemaining() const;
void addInventory(int numItems);
private:
int quantityRemaining;
};
InventoryTag::InventoryTag() {
quantityRemaining = 0;
}
int InventoryTag::getQuantityRemaining() const {
return quantityRemaining;
}
void InventoryTag::addInventory(int numItems) {
if (numItems > 10) {
quantityRemaining = quantityRemaining + numItems;
}
}
int main() {
InventoryTag redSweater;
int sweaterShipment = 0;
int sweaterInventoryBefore = 0;
sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = 25;
cout << "Beginning tests." << endl;
// FIXME add unit test for addInventory
/* Your solution goes here */
cout << "Tests complete." << endl;
return 0;
}
Explanation / Answer
Program:
#include <iostream>
#include <vector>
using namespace std;
void PrintVectors(vector<int> numsList) {
int i;
for (i = 0; i < numsList.size(); ++i) {
cout << numsList.at(i) << " ";
}
cout << endl;
}
int main() {
vector<int> numsList;
numsList.push_back(101);
numsList.push_back(200);
numsList.push_back(103);
/*solution*/
numsList.erase(numsList.begin()+1);
numsList.insert(numsList.begin(), 100);
numsList.insert(numsList.begin()+2, 102);
PrintVectors(numsList);
return 0;
}
Program:
#include <iostream>
using namespace std;
class InventoryTag {
public:
InventoryTag();
int getQuantityRemaining() const;
void addInventory(int numItems);
private:
int quantityRemaining;
};
InventoryTag::InventoryTag() {
quantityRemaining = 0;
}
int InventoryTag::getQuantityRemaining() const {
return quantityRemaining;
}
void InventoryTag::addInventory(int numItems) {
if (numItems > 10) {
quantityRemaining = quantityRemaining + numItems;
}
}
int main() {
InventoryTag redSweater;
int sweaterShipment = 0;
int sweaterInventoryBefore = 0;
sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = 25;
cout << "Beginning tests." << endl;
/* solution */
redSweater.addInventory(sweaterShipment);
if (redSweater.getQuantityRemaining() !=sweaterShipment+sweaterInventoryBefore ) {
cout<<" UNIT TEST FAILED: addInventory()"<<endl;
}
cout << "Tests complete." << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.