Introduction按照题目要求完成两道程序第二题为第一题的模板类版本CMPSC 122.2February 15, 2018 Homework 2Due: March 1, 2018OverviewA vector is a container that can be treated like a normal array (created without using the new keyword)but also allows for dynamic resizing if the vector runs out of capacity to store new data. How is thispossible? A vector is just an abstraction that performs dynamic memory allocation behind the scenes, butis presented in a way that is familiar to using an array, as well as providing additional functionality.When instantiated, a vector will create a dynamically allocated array of the requested type. The size of thearray is typically passed by the user, or handled with default values. Vectors allow for the use of arraynotation with braces [] as well as with an at function to access array elements. You can also use the frontand back functions to retrieve the appropriate elements.The push_back and insert functions should be used to put new information inside the vector, andincrementally increase the size of the vector. at and [] should only be used to reassign the value of anelement that has been pushed into the function with push_back.To allow for reassignment as well as access, the above functions return references to the requestedelement, they do not simply return the value at that index. There are other functions that vector providethat will be discussed in the sections below.Program 1. (80 points) Integer VectorWrite a program (called integer_vector.cpp) that does the following:1. Implement a class called BasicVector that:• Contains private member fields for:o data : integer pointer that is used to point to a dynamically allocated 1Dinteger arrayo vector_size : the current number of items in datao vector_capacity : the maximum number of items that can be contained indata before it needs to be resized• Contains the private member function:o resize : No return type (void), no parameters. Dynamically creates a new 1Darray with twice the capacity of the existing array. Copy all of the existingelements over into the new array, update vector_capacity, use a temporaryvariable store the current address of data and delete [] to free the old array.Point data to the new array.• Contains public member functions:o at : returns an int reference to the array element specified. Has conditionallogic that prevents using an index larger than the vector_size of theBasicVector object by calling exit 1 to terminate the program. Accepts asingle parameter, int index.o operator[] : returns an int reference to the array element specified. Hasconditional logic that prevents using an index larger than the vector_size ofCMPSC 122.2February 15, 2018 Homework 2Due: March 1, 2018the BasicVector object by calling exit 1 to terminate the program. Accepts asingle parameter, int index. Do not use the friend keyword, implement as aclass member functiono front : returns an int reference to the first array element. Calling front on anempty array is undefined behavior, so you do not need to account for it. Justassume it shouldn’t be done.o back : returns an int reference to the last array element. Calling back on anempty array is undefined behavior, so you do not need to account for it. Justassume it shouldn’t be done.o push_back : accepts an int and puts it in the first open index at the back of thearray. No return type (returns void) If the vector_size of the array matchesthe vector_capacity before the new element is inserted, you will need to callthe resize function, and then insert the new value. vector_size needs to beincremented after the element is inserted.o insert : accepts two int, an index to insert at, and the value to be inserted. Inorder for a value to be inserted, all of the elements from the given index tothe end of the array need to be shifted once to the right to make room for theinserted value. Be sure to check if this will cause the vector_size to overtakevector_capacity, and call resize before any elements are shifted. No returntype (void). Do not allow insertion past the last element in the vector.Increment vector_size after successful element insertion.o pop_back : no parameters, no return type. Removes the current last elementfrom the array by setting element to 0 and decrementing vector_size. Shoulddo nothing if array is empty.o size : returns vector_size fieldo capacity : returns vector_capacity fieldo print : no return type, no parameters. Outputs the vector_size and contents ofdata on a single line, use the following format:elements(5): 2 -3 16 7 0• Contains a non-default constructor (you do not need a default constructor):o Accepts one parameter for capacity. If the capacity is less than 16, use 16 asthe capacity of data. Otherwise, set vector_capacity to the next largestpower of 2, and create data with that new capacity.• Contains a destructor:o That sets all the filled elements in data to 0, and discards with array properlywith delete []CMPSC 122.2February 15, 2018 Homework 2Due: March 1, 20182. Ensure that the member functions of BasicVector adhere to certain limits on asymptoticcomplexity as specified below, with the assumption that our n (input) is the size of the arraystored in our vector class:• resize, and any functions that potentially invoke resize have a complexity of O(n)• All other member functions have a complexity of O(1)3. Create an interactive “command” driven prompt system that parses “commands” from theuser that are used to invoke the appropriate functions:• Initially prompt the user for the starting capacity of the vector and create a vectorobject by invoking the non-default constructor of BasicVector• Enter a looping prompt that parses the following commands with the appropriateparameters, and uses the vector created in the previous step to invoke the appropriatemember functions:o at - Invoke the at function, passing the index as its parameter, print resulto get - Invoke the operator[] function, passing index as its parameter, printresulto front- Invoke the front function, print the resulto back- Invoke the back function, print the resulto insert - Invoke the insert function, passing index and value as its twoparameterso push - Invoke the push_back function, passing value its parametero pop- Invoke the pop_back function.o size- Invoke the size function, print the returned valueo capacity- Invoke the capacity function, print the returned valueo print- Invoke the print commando quit- Break out of loop, exit program normally.CMPSC 122.2February 15, 2018 Homework 2Due: March 1, 2018Expected prompt/input with example inputs:Enter starting capacity of double vector: 10Now accepting commands (quit to exit program):> push 10.4> size1> capacity16> printelements(1): 10.4> quitExiting Program.Important: The commands in the list above need to be implemented exactly as written. Points will betaken off if you modify the syntax/naming convention.Program 2. (20 points) Generic VectorTake the code from the integer_vector.cpp program, and modify it to create a program (calledgeneric_vector.cpp) that does that following:1. Uses templates in order to allow the BasicVector class to not be limited to just containingintegers• The template will allow you to substitute a placeholder label in the following parts ofthe code:o The data type of the data member fieldo The value parameter in the push_back and insert functionso The return type of the at, front, back, and [] functionso Logic in the resize and the constructors that dynamically allocate data2. Modify the interactive prompt to use templates in the following way:• Before asking for the starting capacity, prompt the user to specify what data type theywant the vector to store in datao 1 for into 2 for floato 3 for doubleo 4 for stringo 5 for boolExpected prompt/input with sample outputSpecify what data type to store in vector:1) int2) floatCMPSC 122.2February 15, 2018 Homework 2Due: March 1, 20183) double4) string5) bool> 3Enter starting capacity of double vector: 10Now accepting commands (quit to exit program):> push 10.4> size1> capacity16> printelements(1): 10.4> quitExiting program.Template Program FilesTwo template files (integer_vector.cpp and generic_vector.cpp) will be published on Canvas, and will beavailable for copying (cp) on the Sunlab computer from the following directory:/home/cmpsc122/s18/hw2/Compiling the Program with g++Use the following command to compile your classes. This is the command I personally use on the Sunlabmachines to compile and grade your programs:g++ -Wall -o Example:g++ -Wall -o matrix matrix.cppRemember: Your code must successfully compile without any errors, or a zero will be given for theassignment.Following InstructionsYou are expected to follow the directives laid out in this assignment and the course syllabus. Points willbe deducted for incorrectly named files, missing/incorrectly filled out banner comments, not supplyinghardcopy submissions in a pocket folder with the appropriate information, and failing to implementfeatures/functionality specified in the assignment. It is expected that you will utilize the provided templatefiles for this assignment, and that you do not modify the names of class members/functions that areprovided in the template.If you have questions about any portions of the assignment or what is expected, contact me forclarification.CMPSC 122.2February 15, 2018 Homework 2Due: March 1, 2018Submission• Electronic Submission (Due: One minute before midnight, 11:59 PM, February 28, 2018)- Your two source code files (integer_vector.cpp, template_vector.cpp)- Submitted using the mail122 command on the Sunlab machines• Hardcopy Submission (Due: Beginning of class, 6:00 PM, March 1, 2018)- Printed hardcopies of each of the two source code files- The pages of each program should be stapled together- Submitted in a pocket folder with your name, the course, and the section number onthe front本团队核心人员组成主要包括硅谷工程师、BAT一线工程师,精通德英语!我们主要业务范围是代做编程大作业、课程设计等等。我们的方向领域:window编程 数值算法 AI人工智能 金融统计 计量分析 大数据 网络编程 WEB编程 通讯编程 游戏编程多媒体linux 外挂编程 程序API图像处理 嵌入式/单片机 数据库编程 控制台 进程与线程 网络安全 汇编语言 硬件编程 软件设计 工程标准规等。其中代写编程、代写程序、代写留学生程序作业语言或工具包括但不限于以下范围:C/C++/C#代写Java代写IT代写Python代写辅导编程作业Matlab代写Haskell代写Processing代写Linux环境搭建Rust代写Data Structure Assginment 数据结构代写MIPS代写Machine Learning 作业 代写Oracle/SQL/PostgreSQL/Pig 数据库代写/代做/辅导Web开发、网站开发、网站作业ASP.NET网站开发Finance Insurace Statistics统计、回归、迭代Prolog代写Computer Computational method代做因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com 微信:codehelp
网友评论