Question

In: Computer Science

Im trying to figure out the errors as to why these test case will not work...

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);

}

Solutions

Expert Solution

// 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


Related Solutions

Im trying to figure out how to draw this Now, in the innermost circle (0.1 m...
Im trying to figure out how to draw this Now, in the innermost circle (0.1 m radius), assume we found 8 species of plant. In the second smallest circle, we found 10 species. In the third smallest circle, we found 13 species. In the largest circle, we found 14 species. Note that when moving from the smallest circle to the next-smallest circle, we did NOT find 10 additional species...The 10 species in the second smallest circle includes the 8 found...
I'm trying to figure out how did they come up with the answer for the Test...
I'm trying to figure out how did they come up with the answer for the Test Value, and P Vaule. With this in formation provided. is there a formula to figuring out this problem. Parameter and Hypothesis:  What is the true percentage of all my Facebook Friends who would identify summer as their favorite season?  Hypothesis (po) = 50% Test Value:  -1.15 po=.50 p (p-hat) = .463 n = 242 P-Value:  0.2502 I want to know the process of how...
I am trying to figure out which test analysis to use for my research questions. I...
I am trying to figure out which test analysis to use for my research questions. I was thinking about think about multivariate because of the number of variable being addressed in the study but there is also the possibility to use univariate to address each question. What are the current levels of police satisfaction in CMPD jurisdictions? What is the public’s perception of crime in CMPD jurisdictions? Does “hot spot” policing reduce crime in CMPD jurisdictions? How does broken windows...
plz answer all of them. my test is due in a hour and im trying to...
plz answer all of them. my test is due in a hour and im trying to pass 1.Suppose that the average and standard deviation of the number of points scored in an NBA game per player are 17.06 and 6.42, respectively. Calculate an interval that is symmetric around the mean such that it contains approximately 68% of players scores. Assume that the points scored has a normal distribution. 2. The daily stock price for International Business Machines (IBM) historically has...
I was trying to figure out the flow of electrons in a battery connected to a...
I was trying to figure out the flow of electrons in a battery connected to a circuit. Conventionally, current is from the (+) terminal to the (-) terminal of the battery. Realistically it flows the other way round; from the (-) terminal to the (+) terminal. My question is, assuming electron flow is from the (-) terminal, would the battery's cathode be located at the (+) terminal and it's anode at the (-) terminal or would it be vice versa?...
Im having issues with this project, I keep geting errors and idk why. here are the...
Im having issues with this project, I keep geting errors and idk why. here are the instructions for the project: "Create "simple integer calculator using the MARIE computer. The program should execute as follows: Using the INPUT instruction wait for the user to enter a decimal number. Using the INPUT instruction wait for the user to enter a second decimal number. Using the INPUT instruction wait for the user to enter the character +, - or *. Perform the desired...
I'm trying to figure out the enthalpy of fusion, then compare it to the enthalpy fusion...
I'm trying to figure out the enthalpy of fusion, then compare it to the enthalpy fusion of ice, for an experiment that I did in a class the other day. It's really confusing me! The process involved placing ice into a calorimeter with room temperature water inside of it, then recording the change in temperature over a period of time. Thanks for the help. Calorimeter mass = 7.96g Calorimeter sp.heatcapacity = 3.3J/gK Calorimeter temp change = (21.2C - 13.1C) Ice...
Every airline in the world is trying to figure out how best to respond to an...
Every airline in the world is trying to figure out how best to respond to an unrest in the world amid ever-evolving security threats. No airline is immune to this threat. Compare and contrast security strategies used in other countries to the procedures used here in the USA. Cite specific examples.
Assume that XYZ company was started a year ago. As they are trying to figure out...
Assume that XYZ company was started a year ago. As they are trying to figure out how to account for the uncollectible accounts, they are unsure of which method to use. If you recall in your principles class, it was broken down in to the Allowance Method and the Direct Write-Off Method. This book breaks the Allowance Method into two areas: 1. The sales revenue approach; and 2. The gross accounts receivable approach. There are not sure which method to...
A manager is trying to figure out which of her top 2 salespeople is the best,...
A manager is trying to figure out which of her top 2 salespeople is the best, as measured by average number of sales made per week. Thus, she’s set up the following hypothesis test that they are equally as good, & if she can reject that hypothesis, she will conclude that the salesperson with the highest average weekly sales (Salesperson #2) is the best. She’s looked back at all the weeks worked for both employees to calculate the statistics below....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT