Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

C++ Programming Sections 1776, 1777 & 4086

Programming Assignment 4 (65 Points): An Integer Container Type

2022

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 (){

5

6 Container box , bin ; // create two Containers

7

8  // Test all functions for correctness .

9 // Examples output shown on page 4 & 5

10 // Tests do not need to be exactly as shown ,

11 // example driver on page 3

12

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;

3 int capacity = 0;

4 int * data = nullptr ;

5 };

3. Implement the Container Functions shown below.

1 // Construct a Container c with a size s and initial value val .

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 the first element in Container c.

9 int * data ( const Container & c );

10

11 // Returns the number of elements in Container c.

12 int size ( const Container & c );

13

14 // Returns a reference to the element at location i in Container v.

15 // ( optional ) Throws std :: string exception if out of bounds

16 int & at ( Container & c , int i );

17

18 // Returns a reference to the last element in Container c.

19 // ( optional ) Throws std :: out_of_range exception if Container is empty

20 int & back ( const Container & c );

21

22 // Returns the allocated storage for Container c.

23 int capacity ( const Container & c );

24

25 // Erases the elements of Container c but does not change capacity .

26 void clear ( Container & c );

27

28 // If Container c is empty return true , else false .

29 bool empty ( const Container & c );

30

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 to the end of the Container c.

39 void push_back ( Container & c , int element );

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.):

1 int main () {

2 Container a , b ;

3 std :: cout << " Container a, b;\n\n";

4 std :: cout << " construct_container (a ,5 ,0);";

5 construct_container (a , 5 , 0);

6 std :: cout << "\na = ";

7 for (int i = 0; i < size ( a ); i ++)

8 std :: cout << at (a , i ) << " ";

9

10 std :: cout << "\ nsize (a) is " << size ( a );

11 std :: cout << "\ ncapacity (a) is " << capacity ( a );

12

13 std :: cout << "\n\n// add elements to a\n";

14 at (a , 0) = 10;

15 at (a , 3) = 5;

16 std :: cout << "at(a ,0) = 10;\ n";

17 std :: cout << "at(a ,3) = 5;";

18 std :: cout << "\na = ";

19 for (int i = 0; i < size ( a ); i ++)

20 std :: cout << at (a , i ) << " ";

21

22 int & cFront = front ( a );

23 std :: cout << "\n\ nfront (a) = " << cFront ;

24 int & cBack = back ( a );

25 std :: cout << "\ nback (a) = " << cBack << "\n";

26

27 destroy_container ( a );

28 std :: cout << "\n// destroy container a\n";

29 std :: cout << " destroy_container (a);";

30

31 std :: cout << "\n\ nOptional exception handling :";

32 try {

33 std :: cout << "\ nfront (a) = ";

34 std :: cout << front ( a );

35 }

36 catch ( std :: out_of_range e ) { std :: cerr << e . what (); }

37

38 try {

39 std :: cout << "\nat(a ,9) = ";

40 std :: cout << at (a , 9);

41 }

42 catch ( std :: string msg ){ std :: cerr << "\n" << msg << std :: endl ;}

43

44 std :: cout <<

45 "\n\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -";

46

47 // Create confidence tests for Container b as shown below

48 }

Example test output excluding //comments. See driver code on page 3:

Container a, b;

//Container a

construct_container(a,5,0);

a = 0 0 0 0 0

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) = 0

//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

Where to do the assignment

You can do this assignment on your own computer, or using the SMC Virtual labs with Citrix. In either case, ensure the code compiles and runs on Windows. Submit one .cpp fifile named A04.cpp. Do not use any other name or else points will be deducted.

Submitting the Assignment

Include your name, your student id, the assignment number, the submission date, and a program description in comments at the top of your files, and use the CS52 Programming Guide for coding style guidance. Submit the assignment on Canvas (https://online.smc.edu) by uploading your .cpp file to the Assignment 4 entry as an attachment. Do not cut-and-paste your program into a text window. Do not hand in a screenshot of your program’s output. Do not hand in a text file containing the output of your program.

Saving your work

Save your work often on a flash-drive or to the cloud (e.g., GoogleDrive, Microsoft OneDrive, Canvas, etc.). Always save a personal copy of your files (e.g. .cpp, .h, etc.). Do not store files on the virtual lab computers.

Do your own work

Do not distribute this handout. Do not upload to chegg, coursehero, or any other online platform. Do not pay someone to write the code and submit their code as your solution. You are expected to do your own work. Turning in code that is not your own work will result in a referral to Student Judicial Affairs.

References:

STL vector implementation details can be found online at the following websites:

❼ https://docs.microsoft.com/en-us/cpp/standard-library/vector-class

MSDN STL vector docs.

❼ http://www.cplusplus.com/reference/vector/vector/

cplusplus.com vector docs