” C+BASICS CS246编程 写作、C/C++程序语言 辅导ASSIGNMENT #2: C++ BASICS CS246, WINTER 2021Assignment #2: C++ BasicsDue Date 1: Friday, February 5, 2021, 5:00 pm ESTDue Date 2: Friday, February 12, 2021, 5:00 pm ESTOnline Quiz: Wednesday, February 10, 2021, 5:00 pm ESTTopics that must have been completed before starting Due Date 1:1. Software Testing2. produceOutputs and runSuite from A1Topics that must have Been completed before starting Due Date 2:1. C++: Introduction to C++2. Preprocessing and CompilationLearning objectives: C++ I/O (standard, file streams) and output formatting C++ Struct C++ strings and stringstreams C++ references, pointers, and dynamic memory allocation Questions 1, 2a, 3a 4a are due on Due Date 1; questions 2b, 3b, and 4b are due on Due Date 2. Youmust submit the online quiz on Learn by the Quiz date. On this and subsequent assignments, you will take responsibility for your own testing. This assignment isdesigned to get you into the habit of thinking about testing before you start writing your program. For eachquestion you will be given a Compiled executable program which is a program representing a solution toeach question. You should use these provided executables to help you write your test cases, as they canshow you the resultant output for given inputs. If you look at the deliverables and their due dates, you willnotice that there is no C++ code due on Due Date 1. Instead, you will be asked to submit test suites for C++programs that you will later submit by Due Date 2.Test suites will be in a format compatible with that of the latter questions of Assignment 1, so if you did agood job writing your runSuite script, that experience will serve you well here. Design your test suites With care; they are your primary tool for verifying the correctness of your code. Notethat test suite submission zip files are restricted to contain a maximum of 40 tests. The size of each input(.in) file is also restricted to 300 bytes, and each output file (.out) is restricted to 1,000 bytes. This isto encourage you not to combine all of your testing eggs in one basket. You must use the standard C++ I/O streaming and memory management (MM) facilities on this assignment;you may not use C-style I/O or MM. More concretely, you may #include the following C++ libraries(and no others!) for the current assignment: iostream, fstream, sstream, iomanip, and string.Marmoset will be setup to reject submissions that use C-style I/O or MM, or libraries other than the onesspecified above. We will manually check that you follow a reasonable standard of documentation and style, and to verifyany assignment requirements that are not automatically enforced by Marmoset. Code to a standardthat you would expect from someone else if you had to maintain their code. Further comments on codingguidelines can be Found here: httpss://www.student.cs.uwaterloo.ca/cs246/W21/codingguidelines.shtmlPage 1 of 5ASSIGNMENT #2: C++ BASICS CS246, WINTER 2021 We have provided some code and sample executables in the subdirectory codeForStudents under theappropriate subdirectories. These executables have been compiled in the CS student environment andwill not run anywhere else. You may not ask public questions on Piazza about what the programs that make up the assignmentare supposed to do. A major part of this assignment involves designing test cases, and questions that askwhat the programs should do in one case or another will give away potential test cases to the rest of theclass. Questions found in violation of this rule will be marked private or deleted; repeat offences could besubject to discipline.Note: We suggest creating the directory ~/cs246/w21/a2 and creating all the assignment solutions in this directory.Coding AssessmentQuestions 2 to 4 are part of the coding assessment, and may be publicly discussed on Piazza so long as solutions are neitherdiscussed nor revealed.Question 11. Note: there is no coding associated with this problem. Testing question.You are given a non-empty array a[0..n 1], containing n integers. The program maxSum determines the indices i andj, i j, for which Pjk=ia[k] is maximized And reports the maximum value of Pjk=ia[k]. Note that since i j, thesum always contains at least one array element. For example, if the input is-3 4 5 -1 3 -9then maxSum prints11(Output is printed on a single complete line with no padding.) Your task is not to write this program, but to design atest suite for this program. Your test suite must be such that a correct implementation of this program passes all of yourtests, but a buggy implementation will fail at least one of your tests. Marmoset will use a correct implementation andseveral buggy implementations to evaluate your test suite in the manner just described.Your test suite should take the form described in A1: each test should provide its input in the file testname.in,and its expected output in the file testname.out. The collection of all testnames should be contained in the filesuiteq1.txt.Zip up all of the files that make up your test suite into the file a2q1.zip, and submit to Marmoset.Question 22. In this problem, you will write a program called change that makes change for any countrys monetary system (realor fictional). This program accepts, from standard input, the coin denominations that make up the monetary system,and the total value. It then prints a report of the combination of coins needed to make up the total. For example if aparticular country has Coins with values 1, 3, and 8, and you have 13 units of money, then the input would be as follows:ASSIGNMENT #2: C++ BASICS CS246, WINTER 2021The initial 3 means that there are 3 coin types. The next three values are the coin denominations, in some order. Thelast value is the total. For this input, the output should be1 x 81 x 32 x 1Notes: Most coin systems have the property that you can make change by starting at the highest coin value, taking asmany of those as possible, and then moving on to the next coin value, and so on. Although not all combinations ofcoin denominations have this property, you may assume that the input for change will always have this property. The Canadian government has recently abolished the penny. Consequently, once the remaining pennies work theirway out of circulation, it will be impossible to construct coin totals not divisible by 5. Similarly, in whateversystem of denominations you are given, it may not be possible to construct the given total. If that happens, outputImpossible (and nothing else) to standard output. You may assume that the number of denominations is at most 10. You may assume that no denomination will be listed twice. If a given coin is used 0 times for the given total, do not print it out; your output should contain only thosedenominations that were actually used, in decreasing order of size.(a) Due on Due Date 1: Design a test suite for this program. Call your suite file suiteq2.txt. Zip your suite file,together with the associated .in and .out files, into the file a2q2.zip.(b) Due on Due Date 2: Write the program in C++. Call your program a2q2.cc.Question 33. A prettyprinter is a tool that takes program source code as input and outputs the same code, nicely formatted forreadability. In this problem, you will write a prettyprinter for a C-like language.The input for your program will be a sequence of words, spanning one or more lines. The words denote tokens, thepieces that make up a program. The words will be separated from each other by one or more whitespace characters(space, tab, newline). Your program will take these tokens and arrange them nicely, according to the following rules: Initially, the code is Flush to the left margin (i.e., not indented); If the word is ;, print the word and go to the next line; If the word is {, print the word, go to the next line, and the following lines will be indented by one more spacethan previously; If the word is }, it should be printed on its own line, indented one character to the left of the lines between it andits matching { (i.e., the indentation level will be the same as the indentation level of the line that contained thematching {), and the following lines are indented to the same level as this word; If the word is //, then the rest of the current line of input is considered a comment, and must be printed exactlyas it is, including spacing; Except for comments, all of the tokens on a line should be separated from one another by exactly one space.Sample input:int f ( int x ) { // This is my functionint y = x ; y = y + 1 ; return y ; } // This is the END of my functionint main () { int n = 0 ; while ( n 10 ) { n = f ( n ) ; } }Page 3 of 5ASSIGNMENT #2: C++ BASICS CS246, WINTER 2021Corresponding Output:int f ( int x ) {// This is my functionint y = x ;y = y + 1 ;return y ;}// This is the END of my functionint main () {int n = 0 ;while ( n 10 ) {n = f ( n ) ;}}Your solution must not print any extra whitespace at the end of the line (exception: if a comment ends with spaces, thenyou must keep those spaces in your output). However, if trailing space is the only thing wrong with your program, youcan receive partial credit.You may assume: That all tokens are separated by whitespace. In particular, the special words {, }, ;, and // will notbe attached to other tokens, as they can be in C.You may not assume: That the input language is actually C. All you are told is that the input language uses bracebrackets, semicolons, and // comments in a way similar to C, but subject to those constraints, the input could beanything. So do not assume that any properties of the C language, beyond what you have been told, will be true for theinput.(a) Due on Due Date 1: Design a test suite for this program. Call your suite file suiteq3.txt. Zip your suite file,together with the associated .in and .out files, into the file a2q3.zip.(b) Due on Due Date 2: Write the program in C++. Save your solution in a2q3.cc.Question 44. We typically use arrays to store collections of items (say, integers). We can allow for limited growth of a collectionby allocating more space than typically needed, and then keeping track of how much space was actually used. We canallow for unlimited growth of the array by allocating the array on the heap and resizing as necessary. The followingstructure encapsulates a partially-filled array:struct IntArray {int size; // number Of elements the array currently holdsint capacity; // number of elements the array could hold, given current// memory allocation to contentsint *contents;}; Write the function readIntArray which returns an IntArray structure, and whose signature is as follows:IntArray readIntArray();Page 4 of 5ASSIGNMENT #2: C++ BASICS CS246, WINTER 2021The function readIntArray consumes as many integers from cin as are available, populates an IntArraystructure in order with these, and then returns the structure. If a token that cannot be parsed as an integer isencountered before the structure is full, then readIntArray fills as much of the array as needed, leaving therest unfilled. If a non-integer is encountered, the first offending character should be removed from the inputstream (i.e., call cin.ignore once with no arguments). In all circumstances, the field size should accuratelyrepresent the number of elements actually stored in the array and capacity should represent the amount ofstorage currently allocated to the array. Write the function addToIntArray, which takes a reference to an intArray structure and adds as manyintegers to the Structure as are available on cin. The behaviour is identical to readIntArray, except thatintegers are being added to the end of an existing intArray. The signature is as follows:void addToIntArray(IntArray); Write the function printIntArray, which takes a reference to an IntArray structure, and whose signatureis as follows:void printIntArray(const IntArray);The function printIntArray(a) prints the contents of a (as many elements as are actually present) to cout,on the same line, separated by spaces, and followed by a newline. There should be a space after each element inthe array (including the last element), and not before the first element.It is not valid to print or add to an array that has not previously been read, because its fields may not beproperly set. You should not test this.For memory allocation, you must follow this allocation scheme: every IntArray structure begins with a capacity of0. The first time data is stored in an IntArray structure, it is given a capacity of 5 and space allocated accordingly.If at any point, this capacity proves to be not enough, you must double the capacity (so capacities will go from 5 to 10to 20 to 40 …). Note that there is no realloc in C++, so doubling the size of an array necessitates allocating a newarray and copying items over. Your program must not leak memory.A test harness is Available in the starter file a2q4.cc, which you will find in your cs246/1211/a2 directory. Makesure you read and understand this test harness, as you will need to know what it does in order to structure yourtest suite. Note that we may use a different test harness to evaluate the code you submit on Due Date 2 (if your functionswork properly, it should not matter what test harness we use).(a) Due on Due Date 1: Design a test suite for this program, using the main function provided in the test harness.Call your suite file suiteq4.txt. Zip your suite file, together with the associated .in and .out files, into thefile a2q4.zip.(b) Due on Due Date 2: Write the program in C++. Call your solution a2q4.cc.SubmissionThe following files are due at Due Date 1: a2q1.zip, a2q2.zip, a2q3.zip, a2q4.zip.The Following files are due at Due Date 2: a2q2.cc, a2q3.cc, a2q4.cc.如有需要,请加QQ:99515681 或WX:codehelp
“
添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导。