联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2021-02-01 10:43

ASSIGNMENT #2: C++ BASICS CS246, WINTER 2021

Assignment #2: C++ Basics

Due Date 1: Friday, February 5, 2021, 5:00 pm EST

Due Date 2: Friday, February 12, 2021, 5:00 pm EST

Online Quiz: Wednesday, February 10, 2021, 5:00 pm EST

Topics that must have been completed before starting Due Date 1:

1. Software Testing

2. produceOutputs and runSuite from A1

Topics that must have been completed before starting Due Date 2:

1. C++: Introduction to C++

2. Preprocessing and Compilation

Learning 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. You

must 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 is

designed to get you into the habit of thinking about testing before you start writing your program. For each

question you will be given a compiled executable program which is a program representing a solution to

each question. You should use these provided executables to help you write your test cases, as they can

show you the resultant output for given inputs. If you look at the deliverables and their due dates, you will

notice 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 a

good 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. Note

that 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 is

to 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 ones

specified above.

? We will manually check that you follow a reasonable standard of documentation and style, and to verify

any assignment requirements that are not automatically enforced by Marmoset. Code to a standard

that you would expect from someone else if you had to maintain their code. Further comments on coding

guidelines can be found here: https://www.student.cs.uwaterloo.ca/?cs246/W21/

codingguidelines.shtml

Page 1 of 5

ASSIGNMENT #2: C++ BASICS CS246, WINTER 2021

? We have provided some code and sample executables in the subdirectory codeForStudents under the

appropriate subdirectories. These executables have been compiled in the CS student environment and

will not run anywhere else.

? You may not ask public questions on Piazza about what the programs that make up the assignment

are supposed to do. A major part of this assignment involves designing test cases, and questions that ask

what the programs should do in one case or another will give away potential test cases to the rest of the

class. Questions found in violation of this rule will be marked private or deleted; repeat offences could be

subject to discipline.

Note: We suggest creating the directory ~/cs246/w21/a2 and creating all the assignment solutions in this directory.

Coding Assessment

Questions 2 to 4 are part of the coding assessment, and may be publicly discussed on Piazza so long as solutions are neither

discussed nor revealed.

Question 1

1. 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 and

j, i ≤ j, for which Pj

k=i

a[k] is maximized and reports the maximum value of Pj

k=i

a[k]. Note that since i ≤ j, the

sum always contains at least one array element. For example, if the input is

-3 4 5 -1 3 -9

then maxSum prints

11

(Output is printed on a single complete line with no padding.) Your task is not to write this program, but to design a

test suite for this program. Your test suite must be such that a correct implementation of this program passes all of your

tests, but a buggy implementation will fail at least one of your tests. Marmoset will use a correct implementation and

several 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 file

suiteq1.txt.

Zip up all of the files that make up your test suite into the file a2q1.zip, and submit to Marmoset.

Question 2

2. In this problem, you will write a program called change that makes change for any country’s monetary system (real

or 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 a

particular 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 2021

The initial 3 means that there are 3 coin types. The next three values are the coin denominations, in some order. The

last value is the total. For this input, the output should be

1 x 8

1 x 3

2 x 1

Notes:

? Most coin systems have the property that you can make change by starting at the highest coin value, taking as

many of those as possible, and then moving on to the next coin value, and so on. Although not all combinations of

coin 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 their

way out of circulation, it will be impossible to construct coin totals not divisible by 5. Similarly, in whatever

system of denominations you are given, it may not be possible to construct the given total. If that happens, output

Impossible (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 those

denominations 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 3

3. A prettyprinter is a tool that takes program source code as input and outputs the same code, nicely formatted for

readability. 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, the

“pieces” 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 space

than previously;

? If the word is }, it should be printed on its own line, indented one character to the left of the lines between it and

its matching { (i.e., the indentation level will be the same as the indentation level of the line that contained the

matching {), 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 exactly

as 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 function

int y = x ; y = y + 1 ; return y ; } // This is the END of my function

int main () { int n = 0 ; while ( n < 10 ) { n = f ( n ) ; } }

Page 3 of 5

ASSIGNMENT #2: C++ BASICS CS246, WINTER 2021

Corresponding output:

int f ( int x ) {

// This is my function

int y = x ;

y = y + 1 ;

return y ;

}

// This is the END of my function

int 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, then

you must keep those spaces in your output). However, if trailing space is the only thing wrong with your program, you

can receive partial credit.

You may assume: That all tokens are separated by whitespace. In particular, the special words {, }, ;, and // will not

be “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 brace

brackets, semicolons, and // comments in a way similar to C, but subject to those constraints, the input could be

anything. So do not assume that any properties of the C language, beyond what you have been told, will be true for the

input.

(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 4

4. We typically use arrays to store collections of items (say, integers). We can allow for limited growth of a collection

by allocating more space than typically needed, and then keeping track of how much space was actually used. We can

allow for unlimited growth of the array by allocating the array on the heap and resizing as necessary. The following

structure encapsulates a partially-filled array:

struct IntArray {

int size; // number of elements the array currently holds

int capacity; // number of elements the array could hold, given current

// memory allocation to contents

int *contents;

};

? Write the function readIntArray which returns an IntArray structure, and whose signature is as follows:

IntArray readIntArray();

Page 4 of 5

ASSIGNMENT #2: C++ BASICS CS246, WINTER 2021

The function readIntArray consumes as many integers from cin as are available, populates an IntArray

structure in order with these, and then returns the structure. If a token that cannot be parsed as an integer is

encountered before the structure is full, then readIntArray fills as much of the array as needed, leaving the

rest unfilled. If a non-integer is encountered, the first offending character should be removed from the input

stream (i.e., call cin.ignore once with no arguments). In all circumstances, the field size should accurately

represent the number of elements actually stored in the array and capacity should represent the amount of

storage currently allocated to the array.

? Write the function addToIntArray, which takes a reference to an intArray structure and adds as many

integers to the structure as are available on cin. The behaviour is identical to readIntArray, except that

integers 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 signature

is 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 in

the 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 be

properly set. You should not test this.

For memory allocation, you must follow this allocation scheme: every IntArray structure begins with a capacity of

0. 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 10

to 20 to 40 ...). Note that there is no realloc in C++, so doubling the size of an array necessitates allocating a new

array 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. Make

sure you read and understand this test harness, as you will need to know what it does in order to structure your

test suite. Note that we may use a different test harness to evaluate the code you submit on Due Date 2 (if your functions

work 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 the

file a2q4.zip.

(b) Due on Due Date 2: Write the program in C++. Call your solution a2q4.cc.

Submission

The 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.

Page 5 of 5


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp