Commit 8d045629 authored by Rachel Wil Sha Singh's avatar Rachel Wil Sha Singh 💬
Browse files

Writing a vector

parent 6917ef16
#ifndef _VECTOR_HPP
#define _VECTOR_HPP
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class Vector
{
private:
/* Member Variables */
//! A string pointer used to allocate memory for a dynamic array.
string* m_data;
//! Stores how many items have been added to the Vector; less than or equal to the m_arraySize.
int m_itemCount;
//! Stores how large the array currently is; update after Resize() is called.
int m_arraySize;
public:
/* Member Functions */
// Constructor and Destructor
Vector();
~Vector();
// Deal with end of vector
void Push( const string& newItem );
// Deal with middle of vector
string Get( int index ) const;
void Remove( int index );
// Helper functions
int Size() const;
bool IsFull() const;
bool IsEmpty() const;
private:
// Internal helper functions
bool IsInvalidIndex( int index ) const;
bool IsElementEmpty( int index );
// Memory mangement
void AllocateMemory( int newSize = 10 );
void DeallocateMemory();
void Resize();
// Special function, since we haven't covered exceptions yet
void Panic( string message ) const;
void NotImplemented() const;
friend class Tester;
};
/* ****************************************************************************/
/* ************************************************* INITIALIZATION FUNCTIONS */
/* ****************************************************************************/
/**
Set the m_data pointer to nullptr to encourage safe memory management.
Initilaize the m_itemCount and m_arraySize to 0.
*/
//! Called when Vector object is instantiated
Vector::Vector() /* Vector */
{
NotImplemented();
}
/**
Call the DeallocateMemory() function to protect against memory leaks.
*/
//! Called when Vector object is destroyed
Vector::~Vector() /* ~Vector */
{
NotImplemented();
}
/* ****************************************************************************/
/* ********************************************************* HELPER FUNCTIONS */
/* ****************************************************************************/
/**
@return bool true if the Vector is empty, false if it is not empty.
*/
//! If the Vector is empty, return true. Otherwise, return false.
bool Vector::IsEmpty() const /* IsEmpty */
{
NotImplemented();
return false; // temporary - delete me
}
/**
@return bool true if invalid index (less than 0 or >= m_arraySize),
or false if not invalid.
@param int index The index to look at.
*/
//! Check to see if a given index is invalid (i.e., negative).
bool Vector::IsInvalidIndex( int index ) const /* IsInvalidIndex */
{
NotImplemented();
return false; // temporary - delete me
}
/**
@return bool true if the Vector's array is full, or false if not full.
Use m_itemCount and m_arraySize to figure out if the array is full
*/
//! If the Vector is full, then return true. Otherwise, return false.
bool Vector::IsFull() const /* IsFull */
{
NotImplemented();
NotImplemented();
return false; // temporary - delete me
}
/**
@return int the amount of items stored in the Vector's array.
Use m_itemCount here.
*/
//! Return the amount of items currently stored in the Vector.
int Vector::Size() const /* Size */
{
NotImplemented();
return -1; // temporary - delete me
}
/**
@return bool true if the item at the index is an empty string (""),
or false if not.
Error check: If the index is invalid, call Panic()!
*/
//! Returns whether the element at the given index is empty or not.
bool Vector::IsElementEmpty( int index ) /* IsElementEmpty */
{
NotImplemented();
return false; // temporary - delete me
}
/* ****************************************************************************/
/* ********************************************** MEMORY MANAGEMENT FUNCTIONS */
/* ****************************************************************************/
/**
@return void
@param int newSize Defaults to 10 if nothing passed in. Allocates a dynamic
array via the m_data pointer.
If m_data is currently nullptr, then we can allocate memory
(otherwise exit the function without doing anything)...
* Set the m_arraySize to the newSize passed in.
* Set the m_itemCount to 0
* Allocate an array of size m_arraySize using the m_data pointer.
*/
//! Allocate memory for the dynamic array via the m_data pointer.
void Vector::AllocateMemory( int newSize /* = 10 */ ) /* AllocateMemory */
{
NotImplemented();
}
/**
@return void
1. If m_data is already nullptr, nothing needs to be done.
2. If m_data is NOT nullptr, then...
* deallocate memory stored at the m_data location.
* Set m_data to nullptr to prevent invalid memory access.
*/
//! Deallocate memory stored at the address pointed to by the m_data pointer.
void Vector::DeallocateMemory() /* DeallocateMemory */
{
NotImplemented();
}
/**
@return void
Resize() is called when the array is full. It will allocate a bigger array
in memory, copy all the data from the old array to the new array, and then
update the m_data pointer. Follow these steps:
1. Create a new dynamic variable - use a string* pointer,
and set its new size to the current array size, plus 10.
This is the "big array."
(This size is arbitrary; the amount to increase by is a design decision.)
2. Use a for loop to copy all items from the old (small) array TO the
new big array.
3. Afterwards, free up the memory stored at the address pointed to by
the m_data pointer.
4. Update the m_data pointer to point at the same location as the "big array" pointer.
5. Update the old array size to the new size (old size + 10).
*/
//! "Resizes" the dynamic array so that it can hold more items.
void Vector::Resize() /* Resize */
{
NotImplemented();
}
/* ****************************************************************************/
/* *********************************************** END-OF-ARRAY FUNCTIONALITY */
/* ****************************************************************************/
/**
@param const T& newItem - The new item to add to the Vector.
@return void
1. If the m_data pointer is currently nullptr, call the AllocateMemory()
function before continuing.
2. Otherwise, if the array is full (use IsFull()), call the Resize()
function before continuing.
3. After preparing the array (steps 1 and 2), search for an available
space in the array using a for loop. Once you find an empty spot
(m_data[i] == ""), then this is the index where you can add the new item.
4. Add the new item to the array, and increment the m_itemCount.
*/
//! Add a new item to the *end* of the m_data array.
void Vector::Push( const string& newItem ) /* Push */
{
NotImplemented();
}
/* ****************************************************************************/
/* ************************************************************** ANY ELEMENT */
/* ****************************************************************************/
/**
@return string The element value at the index passed in.
@param int index The index of the element to return.
Error check: If the index is invalid, call Panic()!
Otherwise, return the item at that index from m_data.
*/
//! Returns the element value at the index given.
string Vector::Get( int index ) const /* Get */
{
NotImplemented();
return ""; // temporary - delete me
}
/**
@return void
Sets the element at the given index to an empty string ("").
Also decrement m_itemCount.
Error check: If the index is invalid, call Panic()!
*/
//! Clears out the element in the array at the given index.
void Vector::Remove( int index ) /* Remove */
{
NotImplemented();
}
/* ****************************************************************************/
/* ************************************************* FUNCTION TO THROW ERRORS */
/* ****************************************************************************/
//! Call this function if something terrible goes wrong.
void Vector::Panic( string message ) const /* Panic */
{
throw runtime_error( message );
}
//! Marks when a function hasn't been implemented yet.
void Vector::NotImplemented() const /* NotImplemented */
{
throw runtime_error( "Function not implemented yet!" );
}
#endif
This diff is collapsed.
#ifndef _PROGRAM_HPP
#define _PROGRAM_HPP
#endif
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="CS250-Project01-Vector" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/CS250-Project01-Vector" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/CS250-Project01-Vector" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="../DataStructure/Vector.hpp" />
<Unit filename="../Program.hpp" />
<Unit filename="../Tester.hpp" />
<Unit filename="../cuTEST/Menu.hpp" />
<Unit filename="../cuTEST/StringUtil.hpp" />
<Unit filename="../cuTEST/TesterBase.cpp" />
<Unit filename="../cuTEST/TesterBase.hpp" />
<Unit filename="../main.cpp" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="../DataStructure/Vector.hpp" open="1" top="1" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="121" topLine="141" />
</Cursor>
</File>
<File name="../Program.hpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CS250-Project01-Vector", "CS250-Project01-Vector\CS250-Project01-Vector.vcxproj", "{147E7C0B-D44B-4434-93F5-868970F823C9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{147E7C0B-D44B-4434-93F5-868970F823C9}.Debug|x64.ActiveCfg = Debug|x64
{147E7C0B-D44B-4434-93F5-868970F823C9}.Debug|x64.Build.0 = Debug|x64
{147E7C0B-D44B-4434-93F5-868970F823C9}.Debug|x86.ActiveCfg = Debug|Win32
{147E7C0B-D44B-4434-93F5-868970F823C9}.Debug|x86.Build.0 = Debug|Win32
{147E7C0B-D44B-4434-93F5-868970F823C9}.Release|x64.ActiveCfg = Release|x64
{147E7C0B-D44B-4434-93F5-868970F823C9}.Release|x64.Build.0 = Release|x64
{147E7C0B-D44B-4434-93F5-868970F823C9}.Release|x86.ActiveCfg = Release|Win32
{147E7C0B-D44B-4434-93F5-868970F823C9}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7E75DBA7-7DDF-4522-9B8B-D607452C67C0}
EndGlobalSection
EndGlobal
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{147E7C0B-D44B-4434-93F5-868970F823C9}</ProjectGuid>
<RootNamespace>CS250Project01Vector</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\cuTEST\TesterBase.cpp" />
<ClCompile Include="..\..\main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\cuTEST\Menu.hpp" />
<ClInclude Include="..\..\cuTEST\StringUtil.hpp" />
<ClInclude Include="..\..\cuTEST\TesterBase.hpp" />
<ClInclude Include="..\..\DataStructure\Vector.hpp" />
<ClInclude Include="..\..\Program.hpp" />
<ClInclude Include="..\..\Tester.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\main.cpp">
<Filter>Source Files</Filter>
</ClCompile&