In: Computer Science
Im trying to figure out the errors as to why these test case will not work for myvector.h. The issue I have with these test cases is the fact that when I implement these test cases into a grader the program declares that it is not working. So I need help in myvector.h to make the test cases work for my function.
myvector.h
#pragma once
#include // print debugging
#include // malloc, free
using namespace std;
template
class myvector
{
private:
T* A;
int Size;
int Capacity;
public:
// default constructor:
myvector()
{
Size = 0;
Capacity = 1000;
A = new T[Capacity];
}
// constructor with initial size:
myvector(int initial_size)
{
Capacity = initial_size;
Size = initial_size;
A = new T[Capacity];
}
// copy constructor for parameter passing:
myvector(const myvector& other)
{
//
// we have to make a copy of the
"other" vector, so...
//
A = new T[other.Capacity]; //
allocate our own array of same capacity
Size = other.Size; // this vector
is same size and capacity
Capacity = other.Capacity;
// make a copy of the elements
into this vector:
for (int i = 0; i < Size;
++i)
A[i] =
other.A[i];
}
int size()
{
//
//
//
return Size;
}
T& at(int i)
{
return A[i]; // this is WRONG,
but compiles
}
void push_back(T value)
{
if (Size >= Capacity) {
int *temp = new int[2 * Capacity];
for (int i = 0; i < Size; i++)
temp[i] = A[i];
A = temp;
}
Capacity = 2 *
Capacity; // Correction in your code
A[Size] = value;
Size++;
}
T erase(int i)
{
T temp = A[i]; // copying
data from ith index
for (int j = i; j < Size-1;
j++)
{
A[j] = A[j +
1]; // moving data forward
}
Size--; // size reduction
return temp;
}
T& operator[](int i)
{
return A[i];
}
T* rangeof(int i, int j)
{
int sz = j - i + 1;
T* arr = new T[sz]; //dynamically
array allocation.
for (int k = i; k <= j;
k++)
{
arr[k - i] =
A[k];
}
return arr;
}
};
.rangeof for int vector
test case 12.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include "myvector.h"
#include "catch.hpp"
using namespace std;
TEST_CASE("Test 12", "[Project01]")
{
myvector V;
int N = 100;
for (int i = 0; i < N; i++)
V.push_back(i * 10);
REQUIRE(V.size() == N);
// range of 10 elements [0..9]:
int* A = V.rangeof(0, 9);
for (int i = 0; i < 10; ++i)
REQUIRE(A[i] == (i * 10));
}
test15.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include "myvector.h"
#include "catch.hpp"
using namespace std;
TEST_CASE("Test 15", "[Project01]")
{
myvector V;
int N = 100;
for (int i = 0; i < N; i++)
V.push_back(i * 10);
REQUIRE(V.size() == N);
// range of 10 elements [2..12]:
int* A = V.rangeof(2, 12);
// just one element:
int* B = V.rangeof(99, 99);
// all 100 elements:
int* C = V.rangeof(0, 99);
for (int i = 2; i <= 12; ++i)
REQUIRE(A[i-2] == (i * 10));
REQUIRE(B[0] == (99 * 10));
for (int i = 0; i <= 99; ++i)
REQUIRE(C[i] == (i * 10));
}
test20.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include "myvector.h"
#include "catch.hpp"
using namespace std;
TEST_CASE("Test 20", "[Project01]")
{
myvector V;
int N = 10;
for (int i = 0; i < N; i++)
V.push_back(i*2);
REQUIRE(V.size() == N);
// remove them all:
for (int i = 0; i < N; ++i)
{
int x = V.erase(0); // keep erasing the first element:
REQUIRE(x == (i*2));
}
REQUIRE(V.size() == 0);
V.push_back(123);
REQUIRE(V.size() == 1);
REQUIRE(V[0] == 123);
V.push_back(456);
REQUIRE(V.size() == 2);
REQUIRE(V[1] == 456);
REQUIRE(V[0] == 123);
V.push_back(999);
REQUIRE(V.size() == 3);
REQUIRE(V[1] == 456);
REQUIRE(V[0] == 123);
REQUIRE(V[2] == 999);
}
// myvector.h
#pragma once
#include <iostream>
using namespace std;
template <class T>
class myvector
{
private:
T *A;
int Size;
int Capacity;
public:
// default constructor:
myvector()
{
Size = 0;
Capacity = 1000;
A = new T[Capacity];
}
// constructor with initial size:
myvector(int initial_size)
{
Capacity = initial_size;
Size = 0; // Correction : no elements present in vector
A = new T[Capacity];
}
// copy constructor for parameter passing:
myvector(const myvector& other)
{
//
// we have to make a copy of the "other" vector, so...
//
Size = other.Size; // this vector is same size and capacity
Capacity = other.Capacity;
A = new T[Capacity]; // allocate our own array of same capacity
// make a copy of the elements into this vector:
for (int i = 0; i < Size; ++i)
A[i] = other.A[i];
}
int size()
{
return Size;
}
T& at(int i)
{
if(i >=0 && i<Size) // if valid index
return A[i];
else // not a valid index raise exception
throw std::out_of_range("Index out of range");
}
void push_back(T value)
{
// if vector is full
if (Size >= Capacity) {
int *temp = new int[2 * Capacity]; // create a new array of twice its capacity
Capacity = 2 * Capacity; // Correction : increment the capacity
// copy the elements from A to temp
for (int i = 0; i < Size; i++)
temp[i] = A[i];
delete [] A; // delete A
A = temp; // point A to temp
}
A[Size] = value; // insert the value into A
Size++; // increment the size
}
T erase(int i)
{
if(i >=0 && i < Size) // valid index
{
T temp = A[i]; // copying data from ith index
// move the data up
for (int j = i; j < Size-1; j++)
{
A[j] = A[j + 1]; // moving data forward
}
Size--; // size reduction
return temp;
}else // invalid index
throw std::out_of_range("Index out of range");
}
T& operator[](int i)
{
if(i>=0 && i <Size) // valid index
return A[i];
else // invalid index
throw std::out_of_range("Index out of range");
}
T* rangeof(int i, int j)
{
// validate i and j
if(i>=0 && i < Size && j >=0 && j < Size && j >= i)
{
int sz = j - i + 1;
T* arr = new T[sz]; //dynamically array allocation.
for (int k = i; k <= j; k++)
{
arr[k - i] = A[k];
}
return arr;
}
return NULL; // invalid values for i or/and j
}
};
//end of myvector.h