Question

In: Computer Science

can someone finish and check my code on main. cpp? Its not working for me even...

can someone finish and check my code on main. cpp? Its not working for me even though im sure my code make sense
is it possible to output each function to show they work. this is supposed to be a vector class library made from allocated memory
i have included templated functions in the class file to help create the rest of the functions.
Thank you so much

note: i did not include main.cpp because it  was empty- im hoping someone can test my vector class functions on int main. thank you

#ifndef VECTOR_H
#define VECTOR_H
#include
#include 
#include 
 
using namespace std;
 
 
template 
T* allocate(int n){
    return new T[n];
}
template 
void copy_list(T *dest, T* src, int many_to_copy){
    for (int i=0; i < many_to_copy; i++){
        *dest++ = *src++;//destination pointer points to source pointer
    }
    *dest= NULL;
    dest -=many_to_copy;
    src -= many_to_copy;
 
    delete [] src;//deletes src array
    src = nullptr;
}
template 
void print_list(T* list, int size){
    cout<< "Content of list is:\n"<
for(int i = 0; i < size; i++){//loops "size" times
        cout<<  setw(4)<< *list<
list++; //list array increments
        }
    list = list-size;
    cout<< endl;
}
// adds element to end of list
template 
T* push_back(T* list,T entry, int& size){
    T* p =list;// ponts to beginning of list
    T* pnew = allocate(size+1);
    for(int i=0; i
*pnew=*p;
        pnew++= p++;
    }
    *pnew = entry;
    size++; // old size gets increased by one
    pnew -= size-1;
    delete list;
    list =nullptr;
    return pnew;
}
template 
T* pop_back(T* list, int& size){
    size--;
    T* p = &list;
    T* pnew = allocate(size);// listnew points to dynamic array
    for(int i=0;i
pnew=p;
        pnew++;
        p++;
        }
    pnew-= size-1;
      delete list;
      list = nullptr;
      return pnew;
}
template 
T* reallocate(T* arr, int& currentcapacity, int ncapacity){
    T *pointer = allocate(ncapacity);
    copy_list(pointer,arr,currentcapacity);
    currentcapacity = ncapacity;
    return pointer;
}
template 
T* add_entry(T* list, const T& new_entry, int& size, int& capacity){
     list = push_back(list,new_entry,size);
     if(size == capacity) {
          cout<<"Increasing capacity "<
list = reallocate(list,capacity,capacity*2);
     }
    return list;
}
template 
T* search_entry(T* list, const T& find_me, int size) {
     for(int i=0;i
if(*list == find_me) {
             return list;
          }
         list++;
     }
     if(*list != find_me) {
         cout<<"Not found!"<
list-=size;
         print_list(list,size);
         list = nullptr;
         return list;
     }
}
template 
T* remove_entry(T* list, const T& delete_me, int& size, int& capacity) {
 
    T * pointernew = search_entry(list,delete_me,size);
    list = pop_back( list, pointernew, size);
    if(size/4 == capacity) {
        cout<<"Reducing capacity "<
list = reallocate(list,capacity,capacity/2);
    }
     return list;
}
class Vector
{
public:
    Vector();
    Vector(unsigned int size = 100);
 
    // big three:
    //big three functions:
    Vector(const Vector& to_be_copied)          //copy ctor: no return type
    {
        _how_many = 100;
        _capacity= 100;
        list =new int [_capacity+1];
    }
    Vector& operator=(const Vector& rhs)   //assignment operator
    {
        const bool debug = true;
        if (debug){
            cout<<".   .   .   .   ["<list<<"].operator("<
}
        if (this == &rhs){
            return *this;
        }
    }
    ~Vector();
    //member access functions:
    template 
    const T operator [](const unsigned int index) const{//
            assert(index < length());
            return at(index);
    }
    template 
    T& operator [](const unsigned int index){
        assert(index < length());
        return at(index);
    }
    template 
    T& at(int index)             //returns reference to item at position index
    {
        assert(index < length());
            return list[index];
    }
    template 
    const T at(int index) const  //returns a const item at position index
    {
        assert(index < length());
            return list[index];
    }
    template 
    T& front() const             //returns item at position 0.
    {
       * list-=_how_many-1;
        return *list;
    }
    template 
    T& back() const             //return item at the last position
    {
        * list+=_how_many;
         return *list;
    }
    //Push and Pop functions:
    template 
    Vector& operator +=(const T& item){} //push_back
    template 
    void push_back(const T& item);      //append to the end
     template 
    T pop_back();                       //remove last item and return it
    //Insert and Erase:
    template 
    void insert(int insert_here, const T& insert_this);//insert at pos
    void erase(int erase_index);//erase item at position
    template 
    int index_of(const T& item);        //search for item. retur index.
    //size and capacity:
    void set_size(int size);            //enlarge the vector to this size
    //2mm
    void set(const int* old_list);
    void set_capacity(int capacity);    //allocate this space
    int size() const {return _how_many;} //return _size
    int capacity() const {return _capacity;} //return _capacity
    bool empty() const;                 //return true if vector is empty
    int check_error() const;
    //OUTPUT:
   template 
   friend ostream& operator <<( ostream& outs, const Vector& _a );
    template 
    void print_list(T* list, int size);
private:
 
    int _how_many;//size
    int _capacity;//cap
    int _error;//errors
 
    int* list;
 
    int vector_len(const int* list) const;
    int length() const;//reutrns vectorlength function
    template 
    void v_cpy(T* dest, const T* src) const;
};
#endif // VECTOR_H
 
 

//vector.cpp

#include "vector.h"
//CTORS
Vector::Vector(){
    _error =0;
    list = nullptr;
    set(list);
}
Vector::Vector(unsigned int _size){//defaultcotr
    _size =100;
    list = nullptr;
    set(list);
}
Vector::~Vector(){//destructor
    const bool debug = false;
    if (debug){
        cout<<".   .   .   .   Vector::~Vecotr being called."<
}
    if (list!=nullptr)
        delete[] list;
}
template 
void Vector::push_back(const T& item){
}
//Insert and Erase:
template 
void Vector::insert(int insert_here, const T& insert_this){}//inserts item from list
void Vector::erase(int erase_index){}// erease item from list
template 
int Vector::index_of(const T& item){}//search array and return index
void Vector::set_size(int size){_how_many=size;}//returns size
void Vector::set_capacity(int capacity){_capacity= capacity;}
 
//returns length of list
int Vector::vector_len(const int* list) const{
    const bool debug = false;
    if (debug) cout<<".   .   .   .  Vector::vector_len  ("<
int length = 0;
    while(*list){
        length++;
        list++;
    }
    if (debug) cout<
return length;
}
 
int Vector::length() const{
    return vector_len(list);
}
template 
void Vector:: v_cpy(T* dest, const T* src) const{
        while (src){
            *dest++ = *src++;//destination pointer points to source pointer
        }
        *dest= NULL;
        //dest -=many_to_copy;
        //src -= many_to_copy;
        delete [] src;//deletes src array
        src = nullptr;
}
void Vector::set(const int* old_list){
    if (list)
        delete[] list;
    int len = vector_len(old_list);
    //this should be an allocate function.
    list = new int [len+1];
    v_cpy(list, old_list);
}
 
 
 
 
 
 
 
 
 

Solutions

Expert Solution

#include<bits/stdc++.h>
#ifndef VECTOR_H
#define VECTOR_H


using namespace std;


template
T* allocate(int n){
return new T[n];
}
template
void copy_list(T *dest, T* src, int many_to_copy){
for (int i=0; i < many_to_copy; i++){
*dest++ = *src++;//destination pointer points to source pointer
}
*dest= NULL;
dest -=many_to_copy;
src -= many_to_copy;


delete [] src;//deletes src array
src = nullptr;
}
template
void print_list(T* list, int size){
cout<< "Content of list is:\n"<
for(int i = 0; i < size; i++){//loops "size" times
cout<< setw(4)<< *list<
list++; //list array increments
}
list = list-size;
cout<< endl;
}
// adds element to end of list
template
T* push_back(T* list,T entry, int& size){
T* p =list;// ponts to beginning of list
T* pnew = allocate(size+1);
for(int i=0; i
*pnew=*p;
pnew++= p++;
}
*pnew = entry;
size++; // old size gets increased by one
pnew -= size-1;
delete list;
list =nullptr;
return pnew;
}
template
T* pop_back(T* list, int& size){
size--;
T* p = &list;
T* pnew = allocate(size);// listnew points to dynamic array
for(int i=0;i
pnew=p;
pnew++;
p++;
}
pnew-= size-1;
delete list;
list = nullptr;
return pnew;
}
template
T* reallocate(T* arr, int& currentcapacity, int ncapacity){
T *pointer = allocate(ncapacity);
copy_list(pointer,arr,currentcapacity);
currentcapacity = ncapacity;
return pointer;
}
template
T* add_entry(T* list, const T& new_entry, int& size, int& capacity){
list = push_back(list,new_entry,size);
if(size == capacity) {
cout<<"Increasing capacity "<
list = reallocate(list,capacity,capacity*2);
}
return list;
}
template
T* search_entry(T* list, const T& find_me, int size) {
for(int i=0;i
if(*list == find_me) {
return list;
}
list++;
}
if(*list != find_me) {
cout<<"Not found!"<
list-=size;
print_list(list,size);
list = nullptr;
return list;
}
}
template
T* remove_entry(T* list, const T& delete_me, int& size, int& capacity) {


T * pointernew = search_entry(list,delete_me,size);
list = pop_back( list, pointernew, size);
if(size/4 == capacity) {
cout<<"Reducing capacity "<
list = reallocate(list,capacity,capacity/2);
}
return list;
}
class Vector
{
public:
Vector();
Vector(unsigned int size = 100);


// big three:
//big three functions:
Vector(const Vector& to_be_copied) //copy ctor: no return type
{
_how_many = 100;
_capacity= 100;
list =new int [_capacity+1];
}
Vector& operator=(const Vector& rhs) //assignment operator
{
const bool debug = true;
if (debug){
cout<<". . . . ["<list<<"].operator("<
}
if (this == &rhs){
return *this;
}
}
~Vector();
//member access functions:
template
const T operator [](const unsigned int index) const{//
assert(index < length());
return at(index);
}
template
T& operator [](const unsigned int index){
assert(index < length());
return at(index);
}
template
T& at(int index) //returns reference to item at position index
{
assert(index < length());
return list[index];
}
template
const T at(int index) const //returns a const item at position index
{
assert(index < length());
return list[index];
}
template
T& front() const //returns item at position 0.
{
* list-=_how_many-1;
return *list;
}
template
T& back() const //return item at the last position
{
* list+=_how_many;
return *list;
}
//Push and Pop functions:
template
Vector& operator +=(const T& item){} //push_back
template
void push_back(const T& item); //append to the end
template
T pop_back(); //remove last item and return it
//Insert and Erase:
template
void insert(int insert_here, const T& insert_this);//insert at pos
void erase(int erase_index);//erase item at position
template
int index_of(const T& item); //search for item. retur index.
//size and capacity:
void set_size(int size); //enlarge the vector to this size
//2mm
void set(const int* old_list);
void set_capacity(int capacity); //allocate this space
int size() const {return _how_many;} //return _size
int capacity() const {return _capacity;} //return _capacity
bool empty() const; //return true if vector is empty
int check_error() const;
//OUTPUT:
template
friend ostream& operator <<( ostream& outs, const Vector& _a );
template
void print_list(T* list, int size);
private:


int _how_many;//size
int _capacity;//cap
int _error;//errors


int* list;


int vector_len(const int* list) const;
int length() const;//reutrns vectorlength function
template
void v_cpy(T* dest, const T* src) const;
};
#endif // VECTOR_H


//vector.cpp

#include "vector.h"
//CTORS
Vector::Vector(){
_error =0;
list = nullptr;
set(list);
}
Vector::Vector(unsigned int _size){//defaultcotr
_size =100;
list = nullptr;
set(list);
}
Vector::~Vector(){//destructor
const bool debug = false;
if (debug){
cout<<". . . . Vector::~Vecotr being called."<
}
if (list!=nullptr)
delete[] list;
}
template
void Vector::push_back(const T& item){
}
//Insert and Erase:
template
void Vector::insert(int insert_here, const T& insert_this){}//inserts item from list
void Vector::erase(int erase_index){}// erease item from list
template
int Vector::index_of(const T& item){}//search array and return index
void Vector::set_size(int size){_how_many=size;}//returns size
void Vector::set_capacity(int capacity){_capacity= capacity;}


//returns length of list
int Vector::vector_len(const int* list) const{
const bool debug = false;
if (debug) cout<<". . . . Vector::vector_len ("<
int length = 0;
while(*list){
length++;
list++;
}
if (debug) cout<
return length;
}


int Vector::length() const{
return vector_len(list);
}
template
void Vector:: v_cpy(T* dest, const T* src) const{
while (src){
*dest++ = *src++;//destination pointer points to source pointer
}
*dest= NULL;
//dest -=many_to_copy;
//src -= many_to_copy;
delete [] src;//deletes src array
src = nullptr;
}
void Vector::set(const int* old_list){
if (list)
delete[] list;
int len = vector_len(old_list);
//this should be an allocate function.
list = new int [len+1];
v_cpy(list, old_list);
}


Related Solutions

This problem has already been half answered, but can someone finish it and check the answers....
This problem has already been half answered, but can someone finish it and check the answers. I'll include the link. Finished ASAP please! https://www.chegg.com/homework-help/questions-and-answers/use-weighted-average-costing-wanda-company-produces-finished-product-two-processing-depart-q29753062 USE WEIGHTED AVERAGE COSTING: Wanda Company produces its finished product in two processing departments--Mixing and Finishing. The following information is available for the month of March: Mixing Department: The beginning work-in-process inventory was $17,130 ($14,880 direct materials and $2,250 conversion costs) and consisted of 1,200 units. During March, an additional 10,600 units were started into production....
Can someone look into my code and tell me what do you think: Thats Palindrome; //class...
Can someone look into my code and tell me what do you think: Thats Palindrome; //class name Palindrome public class Palindrome {    public static void palindromeChecker(String... str) {        // takes string one by one        for (String s : str) {            // creates a stringbuilder for s            StringBuilder sb = new StringBuilder(s);            // reverses the sb            sb.reverse();            // checks if both...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method is not static in class StaticInitializationBlock, please define the main method as:    public static void main(String[] args) This is what I'm working on class A { static int i; static { System.out.println(1); i = 100; } } public class StaticInitializationBlock { static { System.out.println(2); } public static void main(String[] args) { System.out.println(3); System.out.println(A.i); } }
Can someone please tell me if these calculations are correct! I'm reviewing my notes, and my...
Can someone please tell me if these calculations are correct! I'm reviewing my notes, and my professor said to always multiply the lipids by 3 and then divide by 7 to get the total amount of cals of lipids per day... I'm not completely sure why you do that? Can someone explain. Why don't you just stop at 700 cals for lipids? 1. Calculate the number of calories and grams protein for the following TPN solution: D50W in 500cc 10%...
Please finish this code and make it work. This is my homework and my professor wont...
Please finish this code and make it work. This is my homework and my professor wont allow me to change the code in main, it's a set of huge numbers need to sort by radixsort with bit operation. #include <iostream> using namespace std; void radixLSD_help(int *items, int length, int bit) {    // – Count number of items for each bucket.    // – Figure out where each bucket should be stored (positions // of the first and last element...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
Can someone check to see if anything needs to be changed from this code? The instructions...
Can someone check to see if anything needs to be changed from this code? The instructions are confusing and I'm not sure if this fulfills everything because my compiler won't run it properly. DIRECTIONS: In this project we will develop classes to implement a Day Planner program. Be sure to develop the code in a step by step manner, finish phase 1 before moving on to phase 2. Phase 1 The class Appointment is essentially a record; an object built...
Please make my Code working and pass the test but do NOT change anything in main...
Please make my Code working and pass the test but do NOT change anything in main function, thank you. #include <iostream> using namespace std; void sort(int *A, int n){    for(int passes = 0;passes < 2;passes++) { // shift can have only two values either 0 or 16, used for shifting purpose int shift = passes * 16; int N = 1<<(16 + 1);    // Temporary array for storing frequency of upper or lower 16 bits int temp[N];   ...
can someone please check this code? – Enter Quantity of Numbers Design a program that allows...
can someone please check this code? – Enter Quantity of Numbers Design a program that allows a user to enter any quantity of numbers until a negative number is entered. Then display the highest number and the lowest number. For the programming problem, create the pseudocode and enter it below. Enter pseudocode here start     Declarations           num number           num high             num low              housekeeping()     while number >=0 detailLoop()      endwhile      finish() stop housekeeping( )           output...
Can someone show me the R code to accomplish this? In R, Construct scatter plots of...
Can someone show me the R code to accomplish this? In R, Construct scatter plots of y versus x, y versus ln(x), ln(y) versus ln(x) and 1/y versus 1/x. Include your R code in a separate file. The article “Reduction in Soluble Protein and Chlorophyll Contents in a Few Plants as Indicators of Automobile Exhaust Pollution” (Intl. J. of Environ. Studies, 1983: 239-244) reported the accompanying data on x distance from a highway (meters) and y lead content of soil...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT