Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I finished filling out a c++ problem which should pass all the unit tests. There are requirements that need to be met: 1. The default

I finished filling out a c++ problem which should pass all the unit tests.

There are requirements that need to be met:

1. The default capacity of the vector should be 10 elements, and the default size should be 0.

2. When a push_back() is performed, the vector is increased by 5 if the vector is already full.

3. The pop_back() should change the size by -1, but should not affect the capacity.

4. The getElementAt() should ensure the subscript is within range, however, the [] operator should not.

When I run the program, I got these results:

Test parameterized constructor capacity failed:

Capacity is incorrectly set to 32767 when it should have been 10 elements

Test parameterized constructor size failed:

Size is incorrectly set to 1043318464 when it should have been 0 elements

Test copy constructor capacity failed:

Capactiy is incorrectly set to 32765 when it should have been 10 elements

Test copy constructor size failed:

Size is incorrectly set to 134552032 when it should have been 0 elements

Test push_back of 1 element failed:

Push_back of 1 element on an empty vector produces a capacity of 10 and size of 0

Test push_back of 1 element and pop_back of 1 element failed:

Returned unexpected error code (-11)

Test push_back of 7 elements failed:

Push_back of 7 element on an empty vector produces a capacity of 10 and size of 0

Test getElementAt() failed:

Returned unexpected error code (-11)

Test [] operator overload failed:

Returned unexpected error code (-11)

Test push_back of 7 elements and popping off all 7

Returned unexpected error code (-11)

image text in transcribed

image text in transcribed

image text in transcribed

Is there any way I can pass all the unit tests for this program?

---My code---

---main.cpp---

#include "SimpleVectorOfInts.h"

#include

#include

#include

#include

using namespace std;

int main()

{

// test default constructor

SimpleVectorOfInts emptyVector;

// NOTE: These asserts will fail until the default constructor is working properly.

assert(emptyVector.size() == 0);

assert(emptyVector.capacity() == 10);

/*

NOTE: You can put whatever testing code you want in this file. This file will not

be used as a part of the evaluation. This assignment is evalutated by unit-tesing

the SimpleVectorOfInts interface and implmentation.

*/

// Testing the push_back function

for(int i = 0; i

emptyVector.push_back(i);

assert(emptyVector.size() == 12);

assert(emptyVector.capacity() == 15);

// Testing the getElementAt function

assert(emptyVector.getElementAt(5) == 5);

// Testing the [] operator

assert(emptyVector[7] == 7);

// Testing the pop_back function

emptyVector.pop_back();

assert(emptyVector.size() == 11);

assert(emptyVector.capacity() == 15);

cout

return 0;

}

----SimpleVectorOfInts.h----

#ifndef SimpleVectorOfInts_SimpleVectorOfInts_h

#define SimpleVectorOfInts_SimpleVectorOfInts_h

#include

#include

#include

#include // Needed for the exit function

#include

using namespace std;

class SimpleVectorOfInts

{

private:

int *dynamicArrayPtr; // To point to the allocated array

int numberOfElements; // Number of elements in the array

int arrayCapacity; // capacity of the dynamic array

void memError(); // Handles memory allocation errors

void subError(); // Handles subscripts out of range

public:

// Default constructor

SimpleVectorOfInts();

// Constructor declaration

SimpleVectorOfInts(int);

// Copy constructor declaration

SimpleVectorOfInts(const SimpleVectorOfInts &);

// Destructor declaration

~SimpleVectorOfInts();

// Accessor to return the array size

int size() const

{ return numberOfElements; }

// Accesssor to return the capacity of the vector

int capacity() const

{ return arrayCapacity; }

// Accessor to return a specific element

int getElementAt(int position);

// Overloaded [] operator declaration

int &operator[](const int &);

void push_back(int); // New push_back member

int pop_back(); // New pop_back member

};

#endif

----SimpleVectorOfInts.cpp---

#include

#include

#include "SimpleVectorOfInts.h"

//***********************************************************

// Default Constructor for SimpleVectorOfInts class. Sets *

// the default size and capacity for the vector and *

// allocates memory for the the internal array. *

//***********************************************************

SimpleVectorOfInts::SimpleVectorOfInts()

{

// put your code here

numberOfElements = 0; //

arrayCapacity = 10;

}

//***********************************************************

// Constructor for SimpleVectorOfInts class. Sets the size *

// of the array and allocates memory for it. *

//***********************************************************

SimpleVectorOfInts::SimpleVectorOfInts(int s)

{

// put your code here

dynamicArrayPtr = new int[s]; //

}

//*************************************************

// Copy Constructor for SimpleVectorOfInts class. *

//*************************************************

SimpleVectorOfInts::SimpleVectorOfInts(const SimpleVectorOfInts &obj)

{

// put your code here

}

//********************************************

// Destructor for SimpleVectorOfInts class. *

//********************************************

SimpleVectorOfInts::~SimpleVectorOfInts()

{

// put your code here

delete [] dynamicArrayPtr; //

}

//*******************************************

// memError function. Displays an error message and *

// terminates the program when memory allocation fails. *

//******************************************

void SimpleVectorOfInts::memError()

{

cout

exit(EXIT_FAILURE);

}

//******************************************

// subError function. Displays an error message and *

// terminates the program when a subscript is out of range. *

//************************************

void SimpleVectorOfInts::subError()

{

cout

}

//*******************************************************

// getElementAt function. The argument is a subscript. *

// This function returns the value stored at the sub- *

// cript in the array. *

//*******************************

int SimpleVectorOfInts::getElementAt(int sub)

{

// put your code here

if( sub =numberOfElements) //

{

subError();

}

return dynamicArrayPtr[sub];

}

//*************************************

// Overloaded [] operator. The argument is a subscript. *

// This function returns a reference to the element *

// in the array indexed by the subscript. *

//**************************************

int &SimpleVectorOfInts::operator[](const int &sub)

{

return dynamicArrayPtr[sub];

}

//**************************************

// The push_back function pushes its argument onto the back *

// of the vector. *

//****************************************

void SimpleVectorOfInts::push_back(int val)

{

// put your code here

if(numberOfElements == arrayCapacity) //

{

arrayCapacity += 5;

int *temp = new int[arrayCapacity];

for(int i = 0; i

temp[i] = dynamicArrayPtr[i];

delete [] dynamicArrayPtr;

dynamicArrayPtr = temp;

dynamicArrayPtr[numberOfElements++] = val;

}

}

//***********************************************************

// The pop_back function removes the last element *

// of the vector. It also returns that value. *

//***********************************************************

int SimpleVectorOfInts::pop_back()

{

// put your code here & fix return

if(numberOfElements == 0) //

{

subError();

}

return dynamicArrayPtr[--numberOfElements];

}

3:Unit test 0/10 Test parameterized constructor - capacity Test feedback Capacity is incorrectly set to 32765 when it should have been 10 element 4:Unit test 0/10 Test copy constructor - size Test feedback size is incorrectly set to 1171379440 when it should have been 0 elemer 5:Unit test 0/10 Test parameterized constructor - size Test feedback Size is incorrectly set to 47026592 when it should have been 0 elements 6:Unit test 0/10 Test copy constructor-capacity Test feedback Capactiy is incorrectly set to 32764 when it should have been 10 element 7:Unit test 0/10 Test push_back of 1 element Push_back of 1 element on an empty vector produces a capacity of 10 and 8:Unit test 0/10 Test push_back of 1 element and pop_back of 1 element Your output 9:Unit test 0/10 Test push_back of 7 elements 10:Unit test 0/10 Test getElementAt() Returned unexpected error code (11) Your output 11:Unit test 0/10 Test [ operator overload Returned unexpected error code (11) 12:Unit test 0/10 Test push_back of 7 elements and popping off all 7 Returned unexpected error code (11) a.out: main.cpp:29: int main(): Assertion 'emptyVector.size() = = 12 failed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

3319234609, 978-3319234601

More Books

Students also viewed these Databases questions

Question

What are the stages of project management? Write it in items.

Answered: 1 week ago

Question

What are the purposes of collection messages? (Objective 5)

Answered: 1 week ago