Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
Rachels Courses
cs250 basic data structures with cpp
Commits
46a997b5
Commit
46a997b5
authored
Sep 15, 2020
by
Rachel Wil Sha Singh
💬
Browse files
Fixes to the smart dynamic array lab
parent
693b4cd2
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
592 additions
and
225 deletions
+592
-225
labs/04 Vector/Vector_SmartDynamicArray.zip
labs/04 Vector/Vector_SmartDynamicArray.zip
+0
-0
labs/04 Vector/Vector_SmartDynamicArray/DataStructure/Vector.cpp
... Vector/Vector_SmartDynamicArray/DataStructure/Vector.cpp
+224
-0
labs/04 Vector/Vector_SmartDynamicArray/DataStructure/Vector.hpp
... Vector/Vector_SmartDynamicArray/DataStructure/Vector.hpp
+0
-223
labs/04 Vector/Vector_SmartDynamicArray/Project-CodeBlocks/CS250-Project01-Vector.cbp
...ynamicArray/Project-CodeBlocks/CS250-Project01-Vector.cbp
+1
-0
labs/04 Vector/Vector_SmartDynamicArray/Project-CodeBlocks/CS250-Project01-Vector.depend
...micArray/Project-CodeBlocks/CS250-Project01-Vector.depend
+52
-0
labs/04 Vector/Vector_SmartDynamicArray/Project-CodeBlocks/CS250-Project01-Vector.layout
...micArray/Project-CodeBlocks/CS250-Project01-Vector.layout
+12
-2
labs/04 Vector/Vector_SmartDynamicArray/Project-CodeBlocks/test_result.html
...tor_SmartDynamicArray/Project-CodeBlocks/test_result.html
+237
-0
labs/04 Vector/Vector_SmartDynamicArray/Tester.hpp
labs/04 Vector/Vector_SmartDynamicArray/Tester.hpp
+66
-0
No files found.
labs/04 Vector/Vector_SmartDynamicArray.zip
View file @
46a997b5
No preview for this file type
labs/04 Vector/Vector_SmartDynamicArray/DataStructure/Vector.cpp
0 → 100644
View file @
46a997b5
#include "Vector.hpp"
/* ****************************************************************************/
/* ************************************************* 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
;
// placeholder
}
/**
@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
;
// placeholder
}
/**
@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
();
return
false
;
// placeholder
}
/**
@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
;
// placeholder
}
/**
@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
;
// placeholder
}
/* ****************************************************************************/
/* ********************************************** 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
""
;
// placeholder
}
/**
@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
logic_error
(
message
);
}
//! Marks when a function hasn't been implemented yet.
void
Vector
::
NotImplemented
()
const
/* NotImplemented */
{
throw
runtime_error
(
"Function not implemented yet!"
);
}
labs/04 Vector/Vector_SmartDynamicArray/DataStructure/Vector.hpp
View file @
46a997b5
...
...
@@ -54,227 +54,4 @@ class Vector
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
labs/04 Vector/Vector_SmartDynamicArray/Project-CodeBlocks/CS250-Project01-Vector.cbp
View file @
46a997b5
...
...
@@ -31,6 +31,7 @@
<Compiler>
<Add
option=
"-Wall"
/>
</Compiler>
<Unit
filename=
"../DataStructure/Vector.cpp"
/>
<Unit
filename=
"../DataStructure/Vector.hpp"
/>
<Unit
filename=
"../Program.hpp"
/>
<Unit
filename=
"../Tester.hpp"
/>
...
...
labs/04 Vector/Vector_SmartDynamicArray/Project-CodeBlocks/CS250-Project01-Vector.depend
0 → 100644
View file @
46a997b5
# depslib dependency file v1.0
1587657999 source:/home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/labs/04 Vector/Vector_SmartDynamicArray/cuTEST/TesterBase.cpp
"TesterBase.hpp"
"StringUtil.hpp"
<iostream>
<iomanip>
<cstdlib>
"Menu.hpp"
1587657999 /home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/labs/04 Vector/Vector_SmartDynamicArray/cuTEST/TesterBase.hpp
<string>
<list>
<vector>
<functional>
<fstream>
1587657999 /home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/labs/04 Vector/Vector_SmartDynamicArray/cuTEST/StringUtil.hpp
<string>
<sstream>
1587657999 /home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/labs/04 Vector/Vector_SmartDynamicArray/cuTEST/Menu.hpp
<iostream>
<string>
<vector>
<cstdlib>
<limits>
1587657999 source:/home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/labs/04 Vector/Vector_SmartDynamicArray/main.cpp
"Tester.hpp"
"Program.hpp"
"cuTEST/Menu.hpp"
"DataStructure/Vector.hpp"
<iostream>
1600189086 /home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/labs/04 Vector/Vector_SmartDynamicArray/Tester.hpp
<iostream>
<string>
"cuTEST/TesterBase.hpp"
"cuTEST/Menu.hpp"
"cuTEST/StringUtil.hpp"
"DataStructure/Vector.hpp"
1600125983 /home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/labs/04 Vector/Vector_SmartDynamicArray/DataStructure/Vector.hpp
<iostream>
<string>
<stdexcept>
1587657999 /home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/labs/04 Vector/Vector_SmartDynamicArray/Program.hpp
1600188874 source:/home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/labs/04 Vector/Vector_SmartDynamicArray/DataStructure/Vector.cpp
"Vector.hpp"
labs/04 Vector/Vector_SmartDynamicArray/Project-CodeBlocks/CS250-Project01-Vector.layout
View file @
46a997b5
...
...
@@ -2,9 +2,19 @@
<CodeBlocks_layout_file>
<FileVersion
major=
"1"
minor=
"0"
/>
<ActiveTarget
name=
"Debug"
/>
<File
name=
"../
DataStructure/Vecto
r.hpp"
open=
"1"
top=
"
1
"
tabpos=
"
2
"
split=
"0"
active=
"1"
splitpos=
"0"
zoom_1=
"0"
zoom_2=
"0"
>
<File
name=
"../
Teste
r.hpp"
open=
"1"
top=
"
0
"
tabpos=
"
4
"
split=
"0"
active=
"1"
splitpos=
"0"
zoom_1=
"0"
zoom_2=
"0"
>
<Cursor>
<Cursor1
position=
"121"
topLine=
"141"
/>
<Cursor1
position=
"310"
topLine=
"0"
/>
</Cursor>
</File>
<File
name=
"../DataStructure/Vector.cpp"
open=
"1"
top=
"1"
tabpos=
"3"
split=
"0"
active=
"1"
splitpos=
"0"
zoom_1=
"0"
zoom_2=
"0"
>
<Cursor>
<Cursor1
position=
"556"
topLine=
"3"
/>
</Cursor>
</File>
<File
name=
"../DataStructure/Vector.hpp"
open=
"1"
top=
"0"
tabpos=
"2"
split=
"0"
active=
"1"
splitpos=
"0"
zoom_1=
"0"
zoom_2=
"0"
>
<Cursor>
<Cursor1
position=
"728"
topLine=
"27"
/>
</Cursor>
</File>
<File
name=
"../Program.hpp"
open=
"1"
top=
"0"
tabpos=
"1"
split=
"0"
active=
"1"
splitpos=
"0"
zoom_1=
"0"
zoom_2=
"0"
>
...
...
labs/04 Vector/Vector_SmartDynamicArray/Project-CodeBlocks/test_result.html
0 → 100644
View file @
46a997b5
<!DOCTYPE html>
<html>
<head>
<title>
cuTEST Test Results
</title>
<style
type=
'text/css'
>
body
{
font-family
:
sans-serif
;
font-size
:
12pt
;
}
.warning
{
padding
:
20px
;
background
:
#ffed24
;
font-size
:
20px
;
text-align
:
center
;
}
.result-success
{
background
:
#24ff24
;
font-size
:
20px
;
text-align
:
center
;
}
.result-failure
{
background
:
#ff7095
;
font-size
:
20px
;
text-align
:
center
;
}
table
{
background
:
#ccccff
;
width
:
100%
;
border
:
solid
1px
#0000aa
;
}
table
tr
.pass
{
background
:
#ccffcc
;
}
table
tr
.fail
{
background
:
#ffcccc
;
}
table
tr
{
font-size
:
12pt
;
border
:
solid
1px
#000066
;
}
table
tr
td
{
padding
:
10px
;
border
:
solid
1px
#000033
;
}
table
tr
td
ul
li
,
table
tr
td
ol
li
{
margin
:
10px
0
;
}
table
tr
td
ol
{
list-style-type
:
upper-latin
;
}
table
tr
.test-set
{
}
table
tr
.test
{
font-size
:
14pt
;
}
table
tr
.prereq
{
}
table
tr
.result
{
text-align
:
center
;
font-size
:
14pt
;
font-weight
:
bold
;
}
table
tr
.expected
{
font-family
:
monospace
;
font-size
:
14pt
;
}
table
tr
.actual
{
font-family
:
monospace
;
font-size
:
14pt
;
}
table
tr
.comments
{
}
table
tr
.summary
{
background
:
#000000
;
color
:
#ffffff
;
}
table
tr
.descriptions
{
font-size
:
8pt
;
text-align
:
center
;
}
table
.spacer
{
height
:
100px
;
}
</style>
</head>
<body>
<div
class=
'warning'
>
Warning: Make sure to check if all tests finish (there will be a message at the end of the file) -
<br>
It is possible for your program to crash early, but still show that tests have passed because it hasn't gone through everything.
</div>
<table>
<tr>
<th
class=
'col_set'
>
Test set
</th>
<th
class=
'col_test'
>
Test
</th>
<th
class=
'col_prereq'
>
Prerequisite
<br>
functions
</th>
<th
class=
'col_result'
>
Pass/fail
</th>
<th
class=
'col_expected'
>
Expected
<br>
output
</th>
<th
class=
'col_actual'
>
Actual
<br>
output
</th>
<th
class=
'col_comments'
>
Comments
</th>
</tr>
<tr
class=
'fail'
>
<td
class=
'test-set'
>
Vector Constructor
</td>