Skip to main content

Topics Covered


  • Using Header Files
  • Makefiles with multiple sources into an executable
  • Arrays
  • Basic Sorting
  • Pointers

 

Implementing a Vector using Arrays


For working with Header files see Chapter 3 of the textbook, specifically sections 3.7-3.8.

Please copy the following header file into a file titled vector.h

#ifndef VECTOR
#define VECTOR

class vector{
    private:
        int size;
        int capacity;
        int *arr;
        // When you reach capacity, resize to double the size
        void resize();
    public:
        // Default Value with size equal to 0 and capacity 4
        vector(const int s = 0, const int c = 4);
        
        // Get the number of items in the vector
        int getSize();
        
        // Return a boolean value stating whether there is currently any items in the vector
        bool is_empty();
        
        // Returns items at given index, -1 if out of bounds
        int itemAt(int index);
        
        // Display the items currently stored in the vector
        void displayItems();
        
        // Inserts item after last item in vector
        void push(int item);
        
        // Inserts item at specified index, shifts that index's value and trailing 
        // elements to the right. Return -1 if out of bounds
        int insert(int index, int item);
        
        // Insert item at index 0. Hint: use insert()
        void prepend(int item);
        
        // Remove item from the end of the vector and return the value
        void pop();
        
        // Delete item at index, shifting all trailing elements left
        void deleteItem(int index);
        
        // Looks for value and removes index holding it (even if in multiple places)
        void remove(int item);
        
        // Looks for value and returns first index with that value, -1 if not found
        int find(int item);
};

#endif

 

 

Please copy the following contents into a file titled vector.cpp

#include <iostream>
#include "vector.h"
using namespace std;

vector :: vector(const int s, const int c) : size(s), capacity(c)
{arr = new int[c];}

int vector :: getSize()
{
    // YOUR CODE HERE
}

bool vector :: is_empty()
{
    // YOUR CODE HERE
}

int vector :: itemAt(int index)
{
    // YOUR CODE HERE
}

void vector :: displayItems()
{
    // YOUR CODE HERE
}

void vector :: push(int item)
{
    // YOUR CODE HERE
}

int vector :: insert(int index, int item)
{
    // YOUR CODE HERE
} 

void vector :: prepend(int item)
{
    // YOUR CODE HERE
}

void vector :: pop()
{
    // YOUR CODE HERE
}

void vector :: deleteItem(int index)
{
    // YOUR CODE HERE    
}

void vector :: remove(int item)
{
    // YOUR CODE HERE
}

int vector :: find(int item)
{
    // YOUR CODE HERE
}

void vector :: resize()
{
    // Double the capacity of the array of elements
    int * temp; 
    temp = new int[2*capacity];
    
    for (int i=0; i<capacity; i++) {
      temp[i] = arr[i];
    }
    
    capacity *= 2;
    
    delete [] arr;
    
    arr = temp;
}

 

Develop a main.cpp file that includes vector.h and test the functionality of your newly developed functions.

#include <iostream>
#include "vector.h"

int main()
{
    vector v(0, 4);
    std::cout << v.getSize() << std::endl;
    std::cout << "Is empty: " << v.is_empty() << std::endl;
}

 

Finally, create a Makefile which compiles everything into an executable titled main.


What to Submit?


LAB2_LastName_FirstName.zip
|
|—–> vector.h
|
|—–> vector.cpp
|
|——>main.cpp
|
|——>Makefile