Question: Assignment Description We will implement a struct int Vector using procedural programming that has behavior similar to C + + ' s stan - dard

Assignment Description
We will implement a struct int Vector using procedural programming that has behavior similar to C++'s stan-
dard template library std::vector. Vector will not have any member functions. We will restrict our Vector to
only have member variables. We will write helper-functions to construct and destruct our Vector and to add
elements to the Vector, check the size of the Vector, etc. See below for function prototypes and descriptions.
Helper-Functions and Vector Member Variables
The helper-functions that operate on our Vector will be written as stand-alone functions. The member variables
and helper-functions are listed below and follow the C++ standard std::vector class definition. We will only
implement a subset of the std::vector features for Assignment 4. The exception handling is optional.
A file called CSVector.h with a struct Vector; & all function declarations (i.e. prototypes) and
definitions
# include
# include
// struct vector data type
struct Vector {
int size =0; int capacity =0; int * data = nullptr ;
};
// Vector prototypes
// Construct a Vector v with a default size 0.
void construct_vector ( Vector & v, int size =0, int initVal =0);
// Destroy Vector v and return memory to the freestore ( heap ).
void destroy_vector ( Vector & v );
//( optional ) Helper function to copy v.
void copy_data ( Vector & v );
// Returns pointer to the first element in Vector v.
int * data ( const Vector & v );
// Returns the number of elements in Vector v.
int size ( const Vector & v );
// Returns a reference to the element at location i or throws an exception
int & at ( Vector & v, int i );
// Returns a reference to the last element or throws an exception
int & back ( const Vector & v );
// Returns the allocated storage for Vector v.
int capacity ( const Vector & v );
// Erases the elements of Vector v but does not change capacity.
void clear ( Vector & v );
// If Vector v is empty return true, else false.
bool empty ( const Vector & v );
// Returns a reference to the first element or throws an exception
int & front ( const Vector & v );
// Add element to the end of the Vector v.
void push_back ( Vector & v, int element );
// Search for a key in Vector v, return index of key or -1 if not found
int find ( Vector &v, int key );
The function definitions (i.e. implementations) also included in a file called CSVector.h
1//
2// Assignment 4: Implement all Vector functions ( i . e . define the functions )
3//
4
5// Construct a Vector v with a default size 0.
6 void construct_vector ( Vector & v , int size =0, int initVal =0)
7{
8// Your A4 code goes here
9}
10
11// Destroy Vector v and return memory to the freestore ( heap ).
12 void destroy_vector ( Vector & v )
13{
14// Your A4 code goes here
15}
16
17//( optional ) Helper function to copy v .
18 void copy_data ( Vector & v )
19{
20// Your A4 code goes here
21}
22
23// Returns pointer to the first element in Vector v .
24 int * data ( const Vector & v )
25{
26// Your A4 code goes here
27}
28
29// Returns the number of elements in Vector v .
30 int size ( const Vector & v )
31{
32// Your A4 code goes here
33}
34
35// Returns a reference to the element at location i in Vector v .
36// Throws out_of_range exception if out of bounds
37 int & at ( Vector & v , int i )
38{
39 if ( empty ( v ))
40 throw std :: out_of_range (" at exception ");
41
42// Your A4 code goes here
43}
44
45// Returns a reference to the first element in the Vector .
46// Throws out_of_range exception if Vector is empty .
47 int & front ( const Vector & v )
48{
49 if ( empty ( v ))
50 throw std :: out_of_range (" front exception ");
51
52// Your A4 code goes here
53}
54
55// Returns a reference to the last element in Vector v .
56// Throws out_of_range exception if Vector is empty
57 int & back ( const Vector & v )
58{
59 if ( empty ( v ))
60 throw std :: out_of_range (" back exception ");
61
62// Your A4 code goes here
63}
64
65// Returns the allocated storage for Vector v .
66 int capacity ( const Vector & v )
67{
68// Your A4 code goes here
69}
70
71// Erases the elements of Vector v but does not change capacity .
72 void clear ( Vector & v )
73{
74// Your A4 code goes here
75}
76
77// If Vector v is empty return true , else false .
78 bool empty ( const Vector & v )
79{
80// Your A4 code goes here
81}
82
83// Add element to the end of the Vector v .
84 void push_back ( Vector & v , int element )
85{
86// Your A4 code goes here
87}
88
89// Search for a key in Vector v , return index of key or -1 if not found
90 int find ( Vector &v , int key )
91{
92// Your A4 code goes here
93 return -1; // not found
94}
Example driver code to test functions with Vector a (e.g., at(a,i), construct vector(a), at, destroy vector(a),
front(a), back(a), etc.): i provied SS below
Assignment Description We will implement a struct

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!