Assignment Description We will implement a struct int Container similar to C++’s standard template library std::vector but without using Object Oriented Programming. The Container struct will only have member variables and user-defined helper-functions that will assist with constructing and destructing instances of the Container type, and to add items to the Container, check the length of the Container, etc. See below for function prototypes and descriptions. User-Defined Helper-Functions and Container Member Variables The helper-functions that operate on the Container data type will be written as user-defined functions similar to Assignment 3. The member variables and helper-functions are listed below and have similar functionality to those in the C++ standard std::vector class. Only a subset of functionality, similar to std::vector’s member functions, will be implemented for Assignment 4. The exception handling is optional.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Assignment Description
We will implement a struct int Container similar to C++’s standard template library std::vector but without using Object Oriented Programming. The Container struct will only have member variables and user-defined helper-functions that will assist with constructing and destructing instances of the Container type, and to add items to the Container, check the length of the Container, etc. See below for function prototypes and descriptions.
User-Defined Helper-Functions and Container Member Variables
The helper-functions that operate on the Container data type will be written as user-defined functions similar to Assignment 3. The member variables and helper-functions are listed below and have similar functionality to those in the C++ standard std::vector class. Only a subset of functionality, similar to std::vector’s member functions, will be implemented for Assignment 4. The exception handling is optional.

Assignment 4 Program Requirements
1. A file called A04.cpp with a main(. ..) function and your Container implementation.
1 //
2 // Container prototypes go here
3 //
4 int main (){
Container box, bin; // create
two
Containers
7
// Test all functions for
// Examples output shown on page 4 & 5
// Tests do not need to be exactly as shown,
// example driver
8
correctness.
10
11
12
on page 3
13
return 0;
14 }
15
16 // Container function definitions go here
2. A struct Container with member variables size, capacity, and data.
1
struct Container{
2
int
size
= 0;
int capacity
0;
%3!
4
int *data
= nullptr;
5 };
1
3. Implement the Container Functions shown below.
1 // Construct
Container
c with a size s
and initial value val.
a
2 // Defaults
are zero.
3 void construct_container ( Container & c,
int s = 0, int val= 0 );
4
5 // Destroy Container c and return memory to the freestore (heap).
6 void destroy_container ( Container& c );
7
8 // Returns pointer to
9 int* data ( const Container& c );
the first element
in Container c.
10
11 // Returns the number of elements in Container c.
12 int size ( const
Container & c);
13
at location i
14 // Returns
15 // (optional) Throws std::string exception if
16 int & at
17
a reference to the element
in Container v.
out of bounds
( Container& c, int i);
18 // Returns a reference
19 // (optional) Throws std:: out_of_range exception if Container is empty
20 int & back ( const Container& c );
to the last element in Container c.
21
22 // Returns the allocated storage for Container c.
23 int capacity ( const Container& c );
24
but does not change capacity.
25 // Erases the elements of Container c
26 void clear ( Container& c );
27
28 // If Container
29 bool empty ( const Container& c);
30
c is empty return true, else false.
31 // Returns a reference to the first element in the Container.
32 // (optional) Throws exception if Container is empty.
33 int & front ( const Container& c );
34
35 // Deletes the last element of Container c.
36 void pop_back ( Container & c );
37
38 // Add element
39 void push_back ( Container & c, int element);
to the end of the Container
C.
40
41 // Search for a key in Container c,
42 //return index of key or -1 if not found
43 int find_key (Container &c, int key);
Transcribed Image Text:Assignment 4 Program Requirements 1. A file called A04.cpp with a main(. ..) function and your Container implementation. 1 // 2 // Container prototypes go here 3 // 4 int main (){ Container box, bin; // create two Containers 7 // Test all functions for // Examples output shown on page 4 & 5 // Tests do not need to be exactly as shown, // example driver 8 correctness. 10 11 12 on page 3 13 return 0; 14 } 15 16 // Container function definitions go here 2. A struct Container with member variables size, capacity, and data. 1 struct Container{ 2 int size = 0; int capacity 0; %3! 4 int *data = nullptr; 5 }; 1 3. Implement the Container Functions shown below. 1 // Construct Container c with a size s and initial value val. a 2 // Defaults are zero. 3 void construct_container ( Container & c, int s = 0, int val= 0 ); 4 5 // Destroy Container c and return memory to the freestore (heap). 6 void destroy_container ( Container& c ); 7 8 // Returns pointer to 9 int* data ( const Container& c ); the first element in Container c. 10 11 // Returns the number of elements in Container c. 12 int size ( const Container & c); 13 at location i 14 // Returns 15 // (optional) Throws std::string exception if 16 int & at 17 a reference to the element in Container v. out of bounds ( Container& c, int i); 18 // Returns a reference 19 // (optional) Throws std:: out_of_range exception if Container is empty 20 int & back ( const Container& c ); to the last element in Container c. 21 22 // Returns the allocated storage for Container c. 23 int capacity ( const Container& c ); 24 but does not change capacity. 25 // Erases the elements of Container c 26 void clear ( Container& c ); 27 28 // If Container 29 bool empty ( const Container& c); 30 c is empty return true, else false. 31 // Returns a reference to the first element in the Container. 32 // (optional) Throws exception if Container is empty. 33 int & front ( const Container& c ); 34 35 // Deletes the last element of Container c. 36 void pop_back ( Container & c ); 37 38 // Add element 39 void push_back ( Container & c, int element); to the end of the Container C. 40 41 // Search for a key in Container c, 42 //return index of key or -1 if not found 43 int find_key (Container &c, int key);
Example driver code to test functions with Container a (e.g., at(a,i), construct.container(a), at, de-
stroy.container(a), front(a), back(a), etc.):
int main () {
Container a, b;
std::cout << "Container a, b;\n\n";
std::cout
<< "construct_container (a, 5 ,0); ";
construct_container (a, 5, 0);
std::cout << "\na = ":
for (int i = 0; i < size (a); i++)
std::cout << at (a, i) << " ";
std:: cout <« "\nsize (a) is " << size (a);
std::cout << "\ncapacity (a) is "
<« capacity (a);
std::cout « "\n\n// add elements to a\n";
at (a, 0)
at (a, 3) = 5;
std::cout « "at (a,0) = 10;\n";
std::cout « "at (a, 3) = 5; ";
std::cout << "\na = ";
for (int i = 0; i < size (a); i++)
std::cout << at (a, i) << " ";
= 10;
int& cFront = front (a);
std::cout << "\n\nfront (a) =
" << cFront;
int& cBack = back (a) ;
std::cout << "\nback (a) =
<« cBack << "\n";
destroy_container (a);
std:: cout << "\n//destroy container a\n";
std::cout « "destroy _container (a);";
std::cout << "\n\n0ptional exception handling : ";
try {
std::cout << "\nfront (a) =
std::cout << front (a);
";
}
catch (std : : out_of_range e) { std::cerr
<< e.what (); }
try {
std:: cout <« "\nat (a , 9)
= ";
std::cout
<« at (a, 9);
}
catch (std:: string msg){std::cerr <« "\n" << msg << std:: endl;}
std::cout <<
"\n\n-
// Create confidence tests
for Container b as shown below
}
3
Example test output excluding //comments. See driver code on page 3:
Container a, b;
//Container a
construct_container(a,5,0);
a = 0 00 00
size(a) is 5; capacity (a) is 5
//add elements to a
at (a,0) = 10;
at (a,3) = 5;
a = 10 0 0 5 0
front (a) = 10
back (a) - O
//destroy container a
destroy_container (a);
Optional exception handling:
front (a) =
Error to Access 0 Element Container.
at (a,9) =
index 9 out-of-bounds
// Container b
construct_container (b);
size (b) is 0; capacity (b) is 0
//add elements to b
push_back (b,0); push_back (b, 1);
push_back (b,2); push_back (b,3);
b = 0 1 2 3
size (b) is 4; capacity(b) is 4
at (b,1) is 1
//use at to assign new value to 2nd element
at (b, 1) = 88;
b = 0 88 2 3
//remove last element
pop_back (b)
b = 0 88 2
//add two more elements
push_back (b,55); push_back (b,75);
b = 0 88 2 55 75
//clear the container
clear (b);
size (b) is 0; capacity(b) is 8
empty (b) is true
push_back (b, 10); push_back (b,20); push_back (b,30);
push_back (b,40); push_back (b,50); push_back (b,60);
size (b) is 6; capacity(b) is 8
// Continue Container b testing
int i = find (b, 30), i = 2
int i = find (b, 999), i = -1
//traverse data with pointer
int ptr = data (b);
b = 10 20 30 40 50 60
//destroy the Container
destroy_container (b);
size (b) = 0; capacity (b) is 0
Transcribed Image Text:Example driver code to test functions with Container a (e.g., at(a,i), construct.container(a), at, de- stroy.container(a), front(a), back(a), etc.): int main () { Container a, b; std::cout << "Container a, b;\n\n"; std::cout << "construct_container (a, 5 ,0); "; construct_container (a, 5, 0); std::cout << "\na = ": for (int i = 0; i < size (a); i++) std::cout << at (a, i) << " "; std:: cout <« "\nsize (a) is " << size (a); std::cout << "\ncapacity (a) is " <« capacity (a); std::cout « "\n\n// add elements to a\n"; at (a, 0) at (a, 3) = 5; std::cout « "at (a,0) = 10;\n"; std::cout « "at (a, 3) = 5; "; std::cout << "\na = "; for (int i = 0; i < size (a); i++) std::cout << at (a, i) << " "; = 10; int& cFront = front (a); std::cout << "\n\nfront (a) = " << cFront; int& cBack = back (a) ; std::cout << "\nback (a) = <« cBack << "\n"; destroy_container (a); std:: cout << "\n//destroy container a\n"; std::cout « "destroy _container (a);"; std::cout << "\n\n0ptional exception handling : "; try { std::cout << "\nfront (a) = std::cout << front (a); "; } catch (std : : out_of_range e) { std::cerr << e.what (); } try { std:: cout <« "\nat (a , 9) = "; std::cout <« at (a, 9); } catch (std:: string msg){std::cerr <« "\n" << msg << std:: endl;} std::cout << "\n\n- // Create confidence tests for Container b as shown below } 3 Example test output excluding //comments. See driver code on page 3: Container a, b; //Container a construct_container(a,5,0); a = 0 00 00 size(a) is 5; capacity (a) is 5 //add elements to a at (a,0) = 10; at (a,3) = 5; a = 10 0 0 5 0 front (a) = 10 back (a) - O //destroy container a destroy_container (a); Optional exception handling: front (a) = Error to Access 0 Element Container. at (a,9) = index 9 out-of-bounds // Container b construct_container (b); size (b) is 0; capacity (b) is 0 //add elements to b push_back (b,0); push_back (b, 1); push_back (b,2); push_back (b,3); b = 0 1 2 3 size (b) is 4; capacity(b) is 4 at (b,1) is 1 //use at to assign new value to 2nd element at (b, 1) = 88; b = 0 88 2 3 //remove last element pop_back (b) b = 0 88 2 //add two more elements push_back (b,55); push_back (b,75); b = 0 88 2 55 75 //clear the container clear (b); size (b) is 0; capacity(b) is 8 empty (b) is true push_back (b, 10); push_back (b,20); push_back (b,30); push_back (b,40); push_back (b,50); push_back (b,60); size (b) is 6; capacity(b) is 8 // Continue Container b testing int i = find (b, 30), i = 2 int i = find (b, 999), i = -1 //traverse data with pointer int ptr = data (b); b = 10 20 30 40 50 60 //destroy the Container destroy_container (b); size (b) = 0; capacity (b) is 0
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education